Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 85c1ed7

Browse files
authored
Add None with type NoneType during builtin PostWalk (microsoft#447)
Fixes microsoft#442. Replaces microsoft#429, which only masks the underlying True/False bug in microsoft#391. I also removed the hack in microsoft#336 which checked for `ConstantExpression`.
1 parent 0fc3302 commit 85c1ed7

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/Analysis/Engine/Impl/Analyzer/ExpressionEvaluator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public IAnalysisSet LookupAnalysisSetByName(Node node, string name, bool addRef
150150
res = refs.Types;
151151
} else {
152152
// ... warn the user
153-
warn = !(node is ConstantExpression); // Don't warn when None.
153+
warn = true;
154154
}
155155
}
156156
}

src/Analysis/Engine/Impl/Interpreter/Ast/AstBuiltinsPythonModule.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,21 @@ protected override PythonWalker PrepareWalker(IPythonInterpreter interpreter, Py
9090
#endif
9191

9292
var walker = new AstAnalysisWalker(
93-
interpreter, ast, this, filePath, null, _members,
94-
includeLocations,
95-
warnAboutUndefinedValues: true,
93+
interpreter, ast, this, filePath, null, _members,
94+
includeLocations,
95+
warnAboutUndefinedValues: true,
9696
suppressBuiltinLookup: true
9797
);
9898
walker.CreateBuiltinTypes = true;
9999
return walker;
100100
}
101101

102102
protected override void PostWalk(PythonWalker walker) {
103-
IPythonType boolType = null;
103+
AstPythonBuiltinType boolType = null;
104+
AstPythonBuiltinType noneType = null;
104105

105106
foreach (BuiltinTypeId typeId in Enum.GetValues(typeof(BuiltinTypeId))) {
106-
IMember m;
107-
AstPythonBuiltinType biType;
108-
if (_members.TryGetValue("__{0}__".FormatInvariant(typeId), out m) && (biType = m as AstPythonBuiltinType) != null) {
107+
if (_members.TryGetValue("__{0}__".FormatInvariant(typeId), out IMember m) && m is AstPythonBuiltinType biType) {
109108
if (typeId != BuiltinTypeId.Str &&
110109
typeId != BuiltinTypeId.StrIterator) {
111110
biType.TrySetTypeId(typeId);
@@ -117,7 +116,11 @@ protected override void PostWalk(PythonWalker walker) {
117116
_hiddenNames.Add("__{0}__".FormatInvariant(typeId));
118117

119118
if (typeId == BuiltinTypeId.Bool) {
120-
boolType = m as IPythonType;
119+
boolType = boolType ?? biType;
120+
}
121+
122+
if (typeId == BuiltinTypeId.NoneType) {
123+
noneType = noneType ?? biType;
121124
}
122125
}
123126
}
@@ -127,6 +130,10 @@ protected override void PostWalk(PythonWalker walker) {
127130
_members["True"] = _members["False"] = new AstPythonConstant(boolType);
128131
}
129132

133+
if (noneType != null) {
134+
_members["None"] = new AstPythonConstant(noneType);
135+
}
136+
130137
base.PostWalk(walker);
131138
}
132139

src/Analysis/Engine/Test/LanguageServerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,15 @@ public async Task DiagnosticsSettingChange() {
363363
}
364364
}
365365

366-
[TestMethod, Priority(0)]
367-
public async Task TypeHintNoneDiagnostic() {
366+
[DataRow("def f(b: None) -> None:\n b: None")]
367+
[DataRow("from typing import Generator\ndef fun() -> Generator[int, None, None]:\n yield from range(10)")]
368+
[DataTestMethod, Priority(0)]
369+
public async Task TypeHintNoneDiagnostic(string code) {
368370
if (this is LanguageServerTests_V2) {
369371
// No type hints in Python 2.
370372
return;
371373
}
372374

373-
var code = @"
374-
def f(b: None) -> None:
375-
b: None
376-
";
377-
378375
var diags = new Dictionary<Uri, PublishDiagnosticsEventArgs>();
379376
using (var s = await CreateServer((Uri)null, null, diags)) {
380377
var u = await s.OpenDefaultDocumentAndGetUriAsync(code);

0 commit comments

Comments
 (0)