Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Add None with type NoneType during builtin PostWalk #447

Merged
merged 3 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Analyzer/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public IAnalysisSet LookupAnalysisSetByName(Node node, string name, bool addRef
res = refs.Types;
} else {
// ... warn the user
warn = !(node is ConstantExpression); // Don't warn when None.
warn = true;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,21 @@ protected override PythonWalker PrepareWalker(IPythonInterpreter interpreter, Py
#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 @@ protected override void PostWalk(PythonWalker walker) {
_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 @@ protected override void PostWalk(PythonWalker walker) {
_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
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,15 @@ public async Task DiagnosticsSettingChange() {
}
}

[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