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

Commit 5ef5fba

Browse files
author
Mikhail Arkhipov
committed
Handle goto definition of unknown types better (#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 (cherry picked from commit 6b11e69)
1 parent 36aacb9 commit 5ef5fba

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
@@ -686,5 +686,27 @@ def c(): ...
686686
reference.uri.AbsolutePath.Should().Contain("other.py");
687687
reference.range.Should().Be(3, 4, 3, 5);
688688
}
689+
690+
[TestMethod, Priority(0)]
691+
public async Task UnknownType() {
692+
const string code = @"
693+
A
694+
";
695+
var analysis = await GetAnalysisAsync(code);
696+
var ds = new DefinitionSource(Services);
697+
var reference = ds.FindDefinition(analysis, new SourceLocation(2, 1), out _);
698+
reference.Should().BeNull();
699+
}
700+
701+
[TestMethod, Priority(0)]
702+
public async Task UnknownImportedType() {
703+
const string code = @"
704+
from nonexistent import some
705+
";
706+
var analysis = await GetAnalysisAsync(code);
707+
var ds = new DefinitionSource(Services);
708+
var reference = ds.FindDefinition(analysis, new SourceLocation(2, 26), out _);
709+
reference.Should().BeNull();
710+
}
689711
}
690712
}

0 commit comments

Comments
 (0)