Skip to content

Commit 6b11e69

Browse files
author
Mikhail Arkhipov
authored
Handle goto definition of unknown types better (microsoft#1831)
* Remove stale reference * Don't suppress LHS diagnostics on augmented assign * Revert "Don't suppress LHS diagnostics on augmented assign" This reverts commit 6109ac7. * Handle gotodef on unknown types * PR feedback
1 parent 398a75c commit 6b11e69

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/LanguageServer/Impl/Sources/DefinitionSource.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ private Reference HandleFromImport(IDocumentAnalysis analysis, SourceLocation lo
123123
case ImplicitPackageImport packageImport:
124124
module = mres.GetImportedModule(packageImport.FullName);
125125
break;
126+
case ImportNotFound _:
127+
return null;
126128
}
127129

128130
// Are we in the module name (i.e. A in 'from A import B')?
@@ -262,7 +264,8 @@ private Reference TryFromVariable(string name, IDocumentAnalysis analysis, Sourc
262264
definingMember = null;
263265

264266
var m = analysis.ExpressionEvaluator.LookupNameInScopes(name, out var scope, LookupOptions.All);
265-
if (m == null || scope.Module.ModuleType == ModuleType.Builtins || !(scope.Variables[name] is IVariable v)) {
267+
var v = scope?.Variables[name];
268+
if (m == null || scope == null || scope.Module.ModuleType == ModuleType.Builtins || v.IsUnknown()) {
266269
return null;
267270
}
268271

src/LanguageServer/Test/GoToDefinitionTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,5 +688,27 @@ def c(): ...
688688
reference.uri.AbsolutePath.Should().Contain("other.py");
689689
reference.range.Should().Be(3, 4, 3, 5);
690690
}
691+
692+
[TestMethod, Priority(0)]
693+
public async Task UnknownType() {
694+
const string code = @"
695+
A
696+
";
697+
var analysis = await GetAnalysisAsync(code);
698+
var ds = new DefinitionSource(Services);
699+
var reference = ds.FindDefinition(analysis, new SourceLocation(2, 1), out _);
700+
reference.Should().BeNull();
701+
}
702+
703+
[TestMethod, Priority(0)]
704+
public async Task UnknownImportedType() {
705+
const string code = @"
706+
from nonexistent import some
707+
";
708+
var analysis = await GetAnalysisAsync(code);
709+
var ds = new DefinitionSource(Services);
710+
var reference = ds.FindDefinition(analysis, new SourceLocation(2, 26), out _);
711+
reference.Should().BeNull();
712+
}
691713
}
692714
}

0 commit comments

Comments
 (0)