Skip to content

Commit

Permalink
Add None with type NoneType during builtin PostWalk (microsoft#447)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
jakebailey committed Dec 3, 2018
1 parent 0fc3302 commit 85c1ed7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Analyzer/ExpressionEvaluator.cs
Expand Up @@ -150,7 +150,7 @@ internal class ExpressionEvaluator {
res = refs.Types;
} else {
// ... warn the user
warn = !(node is ConstantExpression); // Don't warn when None.
warn = true;
}
}
}
Expand Down
Expand Up @@ -90,22 +90,21 @@ public AstBuiltinsPythonModule(PythonLanguageVersion version)
#endif

var walker = new AstAnalysisWalker(
interpreter, ast, this, filePath, null, _members,
includeLocations,
warnAboutUndefinedValues: true,
interpreter, ast, this, filePath, null, _members,
includeLocations,
warnAboutUndefinedValues: true,
suppressBuiltinLookup: true
);
walker.CreateBuiltinTypes = true;
return walker;
}

protected override void PostWalk(PythonWalker walker) {
IPythonType boolType = null;
AstPythonBuiltinType boolType = null;
AstPythonBuiltinType noneType = null;

foreach (BuiltinTypeId typeId in Enum.GetValues(typeof(BuiltinTypeId))) {
IMember m;
AstPythonBuiltinType biType;
if (_members.TryGetValue("__{0}__".FormatInvariant(typeId), out m) && (biType = m as AstPythonBuiltinType) != null) {
if (_members.TryGetValue("__{0}__".FormatInvariant(typeId), out IMember m) && m is AstPythonBuiltinType biType) {
if (typeId != BuiltinTypeId.Str &&
typeId != BuiltinTypeId.StrIterator) {
biType.TrySetTypeId(typeId);
Expand All @@ -117,7 +116,11 @@ public AstBuiltinsPythonModule(PythonLanguageVersion version)
_hiddenNames.Add("__{0}__".FormatInvariant(typeId));

if (typeId == BuiltinTypeId.Bool) {
boolType = m as IPythonType;
boolType = boolType ?? biType;
}

if (typeId == BuiltinTypeId.NoneType) {
noneType = noneType ?? biType;
}
}
}
Expand All @@ -127,6 +130,10 @@ public AstBuiltinsPythonModule(PythonLanguageVersion version)
_members["True"] = _members["False"] = new AstPythonConstant(boolType);
}

if (noneType != null) {
_members["None"] = new AstPythonConstant(noneType);
}

base.PostWalk(walker);
}

Expand Down
11 changes: 4 additions & 7 deletions src/Analysis/Engine/Test/LanguageServerTests.cs
Expand Up @@ -363,18 +363,15 @@ public class LanguageServerTests {
}
}

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

var code = @"
def f(b: None) -> None:
b: None
";

var diags = new Dictionary<Uri, PublishDiagnosticsEventArgs>();
using (var s = await CreateServer((Uri)null, null, diags)) {
var u = await s.OpenDefaultDocumentAndGetUriAsync(code);
Expand Down

0 comments on commit 85c1ed7

Please sign in to comment.