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

Special case known good names for use-before-def #429

Closed
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
13 changes: 12 additions & 1 deletion src/Analysis/Engine/Impl/Analyzer/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,18 @@ 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.
// "atom" in Python grammar.
switch (name) {
case "None":
case "False":
case "True":
case "...":
case string _ when node is ConstantExpression:
break;
default:
warn = true;
break;
}
}
}
}
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