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

Report unresolved imports #593

Merged
merged 292 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
292 commits
Select commit Hold shift + click to select a range
98067f1
Part 7
Dec 4, 2018
c12df18
Buildable
Dec 5, 2018
500e969
PR feedback
Dec 5, 2018
dd2f114
Merge master
Dec 5, 2018
b829969
Merge conflict
Dec 5, 2018
780757a
Fix #446
Dec 5, 2018
7bf1f38
Fix #446
Dec 5, 2018
710fc18
Merge branch 'analysis2' into analysis5
Dec 5, 2018
6fb8ef6
Part 8
Dec 6, 2018
32616d5
Part 9
Dec 6, 2018
aca9aea
Buildable
Dec 6, 2018
60bc841
Part 10
Dec 7, 2018
f3ac892
Part 11
Dec 7, 2018
6c62582
Part 12
Dec 8, 2018
4de6d73
Buildable
Dec 8, 2018
9f21d05
Part 14
Dec 8, 2018
542a469
First passing test
Dec 8, 2018
7a53a17
Simplify configuration
Dec 8, 2018
8badc5e
Style
Dec 8, 2018
e6d1e69
Fix test and move code to folders
Dec 9, 2018
f714cda
Builtins import
Dec 9, 2018
9ce186a
Fix #470
Dec 10, 2018
bba86a6
Fluents
Dec 10, 2018
e107d9c
Add search path
Dec 10, 2018
5815db9
Import analysis, part I
Dec 11, 2018
cccc6d8
Simplify builtins handling
Dec 11, 2018
281c293
Remove IMember
Dec 11, 2018
f42a5ce
Handle import specific
Dec 11, 2018
97092b5
More tests
Dec 11, 2018
f3df3fc
Add typeshed
Dec 12, 2018
cd3101b
Renames
Dec 12, 2018
a3c99bd
Make sure lazy modules are loaded
Dec 12, 2018
e9b737f
Renames
Dec 12, 2018
4c05da9
Move/rename
Dec 12, 2018
b29dd46
Rework importing
Dec 12, 2018
f4d4ea9
Derivation rework
Dec 13, 2018
fc27d94
Part 2
Dec 13, 2018
93fc881
Part 3
Dec 14, 2018
ec14152
Buildable
Dec 14, 2018
fdb79eb
Module members
Dec 15, 2018
fa180b7
Async walk
Dec 15, 2018
bcafc30
Imports test pass
Dec 15, 2018
dfafb94
Remove lazy types
Dec 15, 2018
39a396a
Fix from import
Dec 16, 2018
75bd509
Stubs
Dec 16, 2018
81d412a
Double overloads
Dec 17, 2018
1e1ce6a
Fix datetime test
Dec 17, 2018
fa08d5d
Merge master
Dec 17, 2018
6f80ebe
Couple more tests + fluents
Dec 17, 2018
4d0f002
Few more tests
Dec 17, 2018
d297e0f
Additionl test + union type
Dec 18, 2018
36fec2b
Built-in scrape tests
Dec 18, 2018
e6fd49e
Full stdlib scrape test
Dec 18, 2018
8673af0
Complete async AST walker
Dec 18, 2018
f6c1ce0
Conditional defines test + variable loc cleanup
Dec 19, 2018
4e30b36
More stub tests
Dec 19, 2018
d7c0672
Add some (broken mostly) tests from DDG
Dec 20, 2018
b615659
Move document tests
Dec 20, 2018
7a1d6e2
Function arg eval, part I
Dec 20, 2018
d3c8ec2
Instance/factory
Dec 21, 2018
84e8d78
Builds
Dec 22, 2018
dfe6c15
Test fixes
Dec 22, 2018
7cc74a2
Fix static and instance call eval
Dec 23, 2018
b6cb3dd
More tests
Dec 23, 2018
d3be9a6
More ported tests
Dec 24, 2018
55654f1
Specialize builtin functions
Dec 24, 2018
27c0336
Make walkers common and handle nested functions
Dec 25, 2018
55c4703
Moar tests
Dec 26, 2018
a5b104f
Parser fixes + more tests
Dec 26, 2018
078e675
Handle negative numbers
Dec 26, 2018
9137e79
Fix null ref
Dec 26, 2018
dc69e23
Basic list support
Dec 26, 2018
6941a64
Few more list tests
Dec 27, 2018
d2b22f8
Basic iterators
Dec 27, 2018
f65af0d
Support __iter__
Dec 27, 2018
051d422
Iterators
Dec 27, 2018
013b8f5
Fix couple of tests
Dec 28, 2018
fb5a3a5
Add decorator test
Dec 28, 2018
e4b02ad
Generics, part I
Dec 29, 2018
bd5948e
Generics, part 2
Dec 29, 2018
18a7021
Generics, part 3
Dec 30, 2018
2d0223c
Basic TypeVar test
Dec 30, 2018
9415e03
Typings, part 4
Dec 31, 2018
18565ec
Fix test
Dec 31, 2018
0083480
Generics, part 6
Dec 31, 2018
9a096de
Generics, part 7
Jan 1, 2019
534deca
More tests (failing)
Jan 2, 2019
adc81a8
Forward ref fixes
Jan 4, 2019
33d9f8e
Reorg
Jan 4, 2019
8688486
Improve symbol resolution + test fixes
Jan 5, 2019
d44790d
Test fixes
Jan 5, 2019
0678d18
Dictionary, part I
Jan 7, 2019
b83cbe0
Part 11
Jan 8, 2019
11eb84f
Fix test
Jan 8, 2019
4992fbc
Tests
Jan 8, 2019
4e38f55
Tests
Jan 8, 2019
d925a8b
More dict work
Jan 8, 2019
1223b58
List ctor
Jan 8, 2019
16b0788
Skip some tests for now
Jan 8, 2019
ab2f494
Fix iterators
Jan 9, 2019
d595653
Tuple slicing
Jan 9, 2019
549eb9c
Polish type comparo in return types
Jan 9, 2019
25152cd
Add Mapping and tests
Jan 9, 2019
141ef2b
Add Iterable
Jan 9, 2019
0cb9925
Fix typo
Jan 9, 2019
63d6669
Add Iterator[T] + test
Jan 9, 2019
dd1efa5
Simplify typing types
Jan 10, 2019
3a8bf43
Class reduction
Jan 10, 2019
eeddb85
Fix tests
Jan 10, 2019
11be2ea
Test fix
Jan 11, 2019
aeac9e1
Handle 'with' statement
Jan 11, 2019
2c9a0f6
Handle try-except
Jan 11, 2019
9f412a8
Class method inheritance + NewType
Jan 12, 2019
2c8583c
Container types
Jan 12, 2019
828e476
Containers test
Jan 12, 2019
8bbad5f
Tests
Jan 12, 2019
43dec18
Handle generic type alias
Jan 12, 2019
3603e12
Named tuple
Jan 12, 2019
449a36b
Global/non-local
Jan 12, 2019
4455a4c
Handle tuples in for
Jan 13, 2019
eeb6859
Basic generator
Jan 13, 2019
099c551
Any/AnyStr
Jan 13, 2019
dce9a26
Test fixes
Jan 13, 2019
a946ca5
Type/Optional/etc handling
Jan 14, 2019
4b7639a
Proper doc population
Jan 14, 2019
df69a4a
Tests + range
Jan 14, 2019
04eb76f
Argument match
Jan 15, 2019
cc05729
Basic argset and diagnostics
Jan 15, 2019
bef6ac7
Argset tests
Jan 15, 2019
f915b11
Exclude WIP
Jan 15, 2019
a6930c3
Exclude WIP
Jan 15, 2019
d021f5a
Arg eval
Jan 15, 2019
acad306
Arg match, part 2
Jan 16, 2019
c5d2492
Tests and generic arg comparisons
Jan 17, 2019
12f3b6e
Function eval with arguments
Jan 17, 2019
1836cb3
Baselines
Jan 17, 2019
89ba050
Fix test
Jan 17, 2019
7aa1228
Undo AST formatting change and update baseline
Jan 17, 2019
8034b36
LS cleanup 1
Jan 18, 2019
2489857
Fix list ctor argument unpacking
Jan 18, 2019
0271eec
Cleanup 2
Jan 18, 2019
e4abc92
Merge branch 'analysis5' into ls
Jan 18, 2019
6c83c40
Builds
Jan 18, 2019
d39659e
Partial completions
Jan 18, 2019
f56b29c
Partial
Jan 21, 2019
31ee0cb
Partial
Jan 21, 2019
7723b24
Simple test
Jan 22, 2019
4ba0d3e
Tests
Jan 22, 2019
16f902f
Basic startup
Jan 22, 2019
4a13bfe
Merge master
Jan 22, 2019
b91a6f5
Clean up a bit
Jan 22, 2019
429c229
Remove debug code
Jan 22, 2019
f7f8192
Port formatter tests
Jan 22, 2019
326c366
Fix tokenizer crash
Jan 22, 2019
52216fe
Async fixes
Jan 24, 2019
3479b0f
Hover
Jan 24, 2019
2ec0841
Basic hover
Jan 24, 2019
15bc660
Adjust expression options
Jan 24, 2019
637c459
Basic signature help
Jan 24, 2019
e9b029a
Fix class/instance
Jan 25, 2019
e52a55e
Update test
Jan 25, 2019
38897d6
Fix builtin creation exception
Jan 25, 2019
72c1e1f
Fix tests
Jan 25, 2019
c5ec4a7
Actually provide declared module
Jan 25, 2019
3695c27
Completion test (partial)
Jan 25, 2019
15815be
Undo
Jan 25, 2019
658d22e
Fix null await
Jan 25, 2019
696633a
Exports filtering
Jan 25, 2019
8a55917
Filter variables & exports
Jan 25, 2019
c34669f
Ported tests
Jan 26, 2019
4bf5eef
Test fixes
Jan 26, 2019
b7e7871
More ported tests
Jan 26, 2019
4b21373
Fix exception completions
Jan 26, 2019
c895721
Import completions
Jan 27, 2019
47adf4a
Scope completions
Jan 27, 2019
376d8fd
With completions
Jan 27, 2019
28b6966
Test fixes
Jan 27, 2019
4555555
Test fix
Jan 27, 2019
9dd1082
Better arg match
Jan 28, 2019
368c108
Temp disable WIP
Jan 28, 2019
d20e668
WIP
Jan 27, 2019
d81d2ad
First cut
Jan 28, 2019
bbfba92
Fix type leak
Jan 28, 2019
4551b72
Merge branch 'ls2' into genclass
Jan 28, 2019
658ea03
WIP
Jan 28, 2019
9745c2e
Remove ConfigureAwait and handle canceled and failed in the analysis …
Jan 28, 2019
cee54c6
Merge branch 'ls' into genclass
Jan 28, 2019
aa8b564
WIP
Jan 28, 2019
3b88852
Generic class base
Jan 29, 2019
f003ed2
Merge branch 'ls2' into ls
Jan 29, 2019
6631dca
Generic forward reference resolution
Jan 29, 2019
1323bca
Suppress completion in strings + test
Jan 29, 2019
f8e6155
Prevent recursion on generic resolution
Jan 29, 2019
bf36c69
Merge branch 'genclass' into ls
Jan 29, 2019
e1b16e6
Handle call expression in generics
Jan 30, 2019
28e9cb7
Merge branch 'genclass' into ls
Jan 30, 2019
b4ce645
Relax condition as it happens in tensorflow
Jan 30, 2019
4421368
Fix typeshed version search
Jan 30, 2019
23baa1f
Hover tests
Jan 30, 2019
e50ac3e
Fix prom import hover
Jan 31, 2019
53faa6e
Hover tests
Jan 31, 2019
04044aa
Synchronize test cache writing
Jan 31, 2019
4ba8adf
First cut
Jan 31, 2019
d445554
Test
Jan 31, 2019
7eac7b3
Fixes
Jan 31, 2019
0b66f68
Add tests for os.path
Jan 31, 2019
295ec2d
Fix cache check
Jan 31, 2019
fd43b3a
Improve resolution of builtins and typing in stubs
Jan 31, 2019
db42ae2
Merge tests
Jan 31, 2019
c04fea9
Merge branch 'ls' into gotodef
Feb 1, 2019
b81c730
Add ntst for requests
Feb 1, 2019
4aca77f
Handle typeshed better
Feb 1, 2019
22d0cd0
Fix custom stub handling
Feb 1, 2019
ef58112
Better sync
Feb 1, 2019
79f9213
Merge branch 'ls' into gotodef
Feb 1, 2019
44c504c
Move files
Feb 1, 2019
db33d86
Fix parameter locations
Feb 2, 2019
4a28d0a
Hover improvement
Feb 3, 2019
99550ba
PEP hints
Feb 4, 2019
a929764
One more test for PEP hints
Feb 4, 2019
2259ac3
Better handle hover over import as
Feb 5, 2019
79f911c
Text based generic constraints
Feb 6, 2019
b7cd4c6
Handle with better with generic stubs
Feb 6, 2019
df7533c
Undo debug
Feb 6, 2019
5c34ceb
Handle non-binary open()
Feb 6, 2019
927b73d
Merge branch 'master' of https://github.com/Microsoft/python-language…
Feb 6, 2019
a7a49ab
Output syntax errors
Feb 6, 2019
094d2a0
Properly clear
Feb 7, 2019
c8ef668
- Fix async issue with analysis completion
Feb 7, 2019
41cd358
Use proper scope when analyzing module
Feb 7, 2019
d7a8b42
Add publishing test
Feb 7, 2019
08df23b
Unused var
Feb 7, 2019
f8f51ae
Test forced publish on close
Feb 7, 2019
7370e2d
Fix typo
Feb 7, 2019
a728657
Update test framework
Feb 7, 2019
bbb042f
Merge master
Feb 7, 2019
64a26b8
Import location
Feb 7, 2019
627eddf
Merge branch 'master' into imprep
Feb 7, 2019
d1f9b73
Severity mapping and reporting
Feb 7, 2019
77aa79b
Remove incorrect reference
Feb 8, 2019
0b7f8b5
Merge branch 'master' into imprep
Feb 8, 2019
63660d8
Diagnostic severity mapping test
Feb 8, 2019
9c0b724
Minor fixes
Feb 8, 2019
2efd0d8
Move interface to the main class part
Feb 9, 2019
8daa58c
Merge master
Feb 10, 2019
67790ce
Flicker reduction
Feb 10, 2019
d245be8
Merge master
Feb 10, 2019
606d8b2
- Correct reported unresolved import name
Feb 12, 2019
f6b054a
Merge branch 'master' of https://github.com/Microsoft/python-language…
Feb 13, 2019
c7822d0
PR feedback
Feb 13, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public interface IExpressionEvaluator {
IPythonModule Module { get; }
IPythonInterpreter Interpreter { get; }
IServiceContainer Services { get; }

void ReportDiagnostics(Uri documentUri, DiagnosticsEntry entry);
IEnumerable<DiagnosticsEntry> Diagnostics { get; }
}
}
9 changes: 5 additions & 4 deletions src/Analysis/Ast/Impl/Analyzer/DocumentAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
using System.IO;
using System.Linq;
using Microsoft.Python.Analysis.Analyzer.Evaluation;
using Microsoft.Python.Analysis.Analyzer.Expressions;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Diagnostics;
using Microsoft.Python.Core.Text;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Ast;

Expand Down Expand Up @@ -67,6 +64,11 @@ public DocumentAnalysis(IDocument document, int version, IGlobalScope globalScop
/// Expression evaluator used in the analysis.
/// </summary>
public IExpressionEvaluator ExpressionEvaluator { get; }

/// <summary>
/// Analysis diagnostics.
/// </summary>
public IEnumerable<DiagnosticsEntry> Diagnostics => ExpressionEvaluator.Diagnostics;
#endregion
}

Expand All @@ -88,5 +90,4 @@ public EmptyAnalysis(IServiceContainer services, IDocument document) {
public IExpressionEvaluator ExpressionEvaluator { get; }
public IEnumerable<DiagnosticsEntry> Diagnostics => Enumerable.Empty<DiagnosticsEntry>();
}

}
6 changes: 3 additions & 3 deletions src/Analysis/Ast/Impl/Analyzer/Evaluation/ExpressionEval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Microsoft.Python.Analysis.Analyzer.Evaluation {
/// Helper class that provides methods for looking up variables
/// and types in a chain of scopes during analysis.
/// </summary>
internal sealed partial class ExpressionEval : IExpressionEvaluator {
internal sealed partial class ExpressionEval: IExpressionEvaluator {
private readonly Stack<Scope> _openScopes = new Stack<Scope>();
private readonly List<DiagnosticsEntry> _diagnostics = new List<DiagnosticsEntry>();
private readonly object _lock = new object();
Expand Down Expand Up @@ -229,11 +229,11 @@ private async Task<IMember> GetValueFromConditionalAsync(ConditionalExpression e
return trueValue ?? falseValue;
}

private void ReportDiagnostics(Uri documentUri, IEnumerable<DiagnosticsEntry> entries) {
public void ReportDiagnostics(Uri documentUri, DiagnosticsEntry entry) {
// Do not add if module is library, etc. Only handle user code.
if (Module.ModuleType == ModuleType.User) {
lock (_lock) {
_diagnostics.AddRange(entries);
_diagnostics.Add(entry);
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ public async Task<bool> HandleFromImportAsync(FromImportStatement node, Cancella
}

var rootNames = node.Root.Names;
IImportSearchResult imports = null;
if (rootNames.Count == 1) {
var rootName = rootNames[0].Name;
if (rootName.EqualsOrdinal("__future__")) {
return false;
}
}

imports = ModuleResolution.CurrentPathResolver.FindImports(Module.FilePath, node);
var imports = ModuleResolution.CurrentPathResolver.FindImports(Module.FilePath, node);
// If we are processing stub, ignore imports of the original module.
// For example, typeshed stub for sys imports sys.
if (Module.ModuleType == ModuleType.Stub && imports is ModuleImport mi && mi.Name == Module.Name) {
Expand All @@ -51,19 +50,21 @@ public async Task<bool> HandleFromImportAsync(FromImportStatement node, Cancella
switch (imports) {
case ModuleImport moduleImport when moduleImport.FullName == Module.Name:
ImportMembersFromSelf(node);
return false;
break;
case ModuleImport moduleImport:
await ImportMembersFromModuleAsync(node, moduleImport.FullName, cancellationToken);
return false;
break;
case PossibleModuleImport possibleModuleImport:
await HandlePossibleImportAsync(node, possibleModuleImport, cancellationToken);
return false;
await HandlePossibleImportAsync(possibleModuleImport, possibleModuleImport.PossibleModuleFullName, Eval.GetLoc(node.Root), cancellationToken);
break;
case PackageImport packageImports:
await ImportMembersFromPackageAsync(node, packageImports, cancellationToken);
return false;
default:
return false;
break;
case ImportNotFound notFound:
MakeUnresolvedImport(null, notFound.FullName, Eval.GetLoc(node.Root));
break;
}
return false;
}

private void ImportMembersFromSelf(FromImportStatement node) {
Expand Down
33 changes: 22 additions & 11 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/ImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Analysis.Core.DependencyResolution;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
using Microsoft.Python.Parsing;
using Microsoft.Python.Parsing.Ast;
using ErrorCodes = Microsoft.Python.Analysis.Diagnostics.ErrorCodes;

namespace Microsoft.Python.Analysis.Analyzer.Handlers {
internal sealed partial class ImportHandler: StatementHandler {
internal sealed partial class ImportHandler : StatementHandler {
public ImportHandler(AnalysisWalker walker) : base(walker) { }

public async Task<bool> HandleImportAsync(ImportStatement node, CancellationToken cancellationToken = default) {
Expand All @@ -48,20 +52,21 @@ public async Task<bool> HandleImportAsync(ImportStatement node, CancellationToke
}

var location = Eval.GetLoc(moduleImportExpression);
var moduleName = moduleImportExpression.MakeString();
IPythonModule module = null;
switch (imports) {
case ModuleImport moduleImport when moduleImport.FullName == Module.Name:
Eval.DeclareVariable(memberName, Module, VariableSource.Declaration, location);
break;
case ModuleImport moduleImport:
module = await HandleImportAsync(node, moduleImport, cancellationToken);
module = await HandleImportAsync(moduleImport, location, cancellationToken);
break;
case PossibleModuleImport possibleModuleImport:
module = await HandlePossibleImportAsync(node, possibleModuleImport, cancellationToken);
module = await HandlePossibleImportAsync(possibleModuleImport, moduleName, location, cancellationToken);
break;
default:
// TODO: Package import?
MakeUnresolvedImport(memberName, moduleImportExpression);
MakeUnresolvedImport(memberName, moduleName, Eval.GetLoc(moduleImportExpression));
break;
}

Expand All @@ -72,20 +77,21 @@ public async Task<bool> HandleImportAsync(ImportStatement node, CancellationToke
return false;
}

private async Task<IPythonModule> HandleImportAsync(ImportStatement node, ModuleImport moduleImport, CancellationToken cancellationToken) {
private async Task<IPythonModule> HandleImportAsync(ModuleImport moduleImport, LocationInfo location, CancellationToken cancellationToken) {
var module = await ModuleResolution.ImportModuleAsync(moduleImport.FullName, cancellationToken);
if (module == null) {
MakeUnresolvedImport(moduleImport.FullName, node);
MakeUnresolvedImport(moduleImport.FullName, moduleImport.FullName, location);
return null;
}
return module;
}

private async Task<IPythonModule> HandlePossibleImportAsync(Node node, PossibleModuleImport possibleModuleImport, CancellationToken cancellationToken) {
private async Task<IPythonModule> HandlePossibleImportAsync(
PossibleModuleImport possibleModuleImport, string moduleName, LocationInfo location, CancellationToken cancellationToken) {
var fullName = possibleModuleImport.PrecedingModuleFullName;
var module = await ModuleResolution.ImportModuleAsync(possibleModuleImport.PrecedingModuleFullName, cancellationToken);
if (module == null) {
MakeUnresolvedImport(possibleModuleImport.PrecedingModuleFullName, node);
MakeUnresolvedImport(possibleModuleImport.PrecedingModuleFullName, moduleName, location);
return null;
}

Expand All @@ -95,7 +101,7 @@ private async Task<IPythonModule> HandlePossibleImportAsync(Node node, PossibleM
var childModule = module.GetMember<IPythonModule>(namePart);
if (childModule == null) {
var unresolvedModuleName = string.Join(".", nameParts.Take(i + 1).Prepend(fullName));
MakeUnresolvedImport(unresolvedModuleName, node);
MakeUnresolvedImport(unresolvedModuleName, moduleName, location);
return null;
}
module = childModule;
Expand Down Expand Up @@ -143,7 +149,12 @@ private void AssignImportedVariables(IPythonModule module, DottedName moduleImpo
}
}

private void MakeUnresolvedImport(string name, Node node)
=> Eval.DeclareVariable(name, new SentinelModule(name, Eval.Services), VariableSource.Import, Eval.GetLoc(node));
private void MakeUnresolvedImport(string variableName, string moduleName, LocationInfo location) {
if (!string.IsNullOrEmpty(variableName)) {
Eval.DeclareVariable(variableName, new SentinelModule(moduleName, Eval.Services), VariableSource.Import, location);
}
Eval.ReportDiagnostics(Eval.Module.Uri, new DiagnosticsEntry(
Resources.ErrorUnresolvedImport.FormatInvariant(moduleName), location.Span, ErrorCodes.UnresolvedImport, Severity.Warning));
}
}
}
7 changes: 7 additions & 0 deletions src/Analysis/Ast/Impl/Definitions/IDocumentAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using Microsoft.Python.Analysis.Analyzer;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Documents;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Parsing.Ast;
Expand Down Expand Up @@ -49,5 +51,10 @@ public interface IDocumentAnalysis {
/// Expression evaluator used in the analysis.
/// </summary>
IExpressionEvaluator ExpressionEvaluator { get; }

/// <summary>
/// Analysis diagnostics.
/// </summary>
IEnumerable<DiagnosticsEntry> Diagnostics { get; }
}
}
45 changes: 45 additions & 0 deletions src/Analysis/Ast/Impl/Diagnostics/DiagnosticsSeverityMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.

using System.Collections.Generic;
using Microsoft.Python.Core;
using Microsoft.Python.Parsing;

namespace Microsoft.Python.Analysis.Diagnostics {
public sealed class DiagnosticsSeverityMap {
private readonly Dictionary<string, Severity> _map = new Dictionary<string, Severity>();

public DiagnosticsSeverityMap() { }

public DiagnosticsSeverityMap(string[] errors, string[] warnings, string[] information, string[] disabled) {
_map.Clear();
// disabled > error > warning > information
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it compatible with VSC?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Same code as before

foreach (var x in information.MaybeEnumerate()) {
_map[x] = Severity.Information;
}
foreach (var x in warnings.MaybeEnumerate()) {
_map[x] = Severity.Warning;
}
foreach (var x in errors.MaybeEnumerate()) {
_map[x] = Severity.Error;
}
foreach (var x in disabled.MaybeEnumerate()) {
_map[x] = Severity.Suppressed;
}
}
public Severity GetEffectiveSeverity(string code, Severity defaultSeverity)
=> _map.TryGetValue(code, out var severity) ? severity : defaultSeverity;
}
}
1 change: 1 addition & 0 deletions src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public static class ErrorCodes {
public const string UnknownParameterName = "unknown-parameter-name";
public const string ParameterAlreadySpecified = "parameter-already-specified";
public const string ParameterMissing = "parameter-missing";
public const string UnresolvedImport = "unresolved-import";
}
}
7 changes: 7 additions & 0 deletions src/Analysis/Ast/Impl/Diagnostics/IDiagnosticsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,12 @@ public interface IDiagnosticsService {
/// the diagnostic publishing to the client.
/// </summary>
int PublishingDelay { get; set; }

/// <summary>
/// Provides map of error codes to severity when user wants
/// to override default severity settings or suppress particular
/// diagnostics completely.
/// </summary>
DiagnosticsSeverityMap DiagnosticsSeverityMap { get; set; }
}
}
7 changes: 6 additions & 1 deletion src/Analysis/Ast/Impl/Modules/PythonModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ private void Parse(CancellationToken cancellationToken) {

// Do not report issues with libraries or stubs
if (sink != null) {
_diagnosticsService?.Replace(Uri, _parseErrors);
_diagnosticsService?.Replace(Uri, _parseErrors.Concat(Analysis.Diagnostics));
}

_parsingTask = null;
Expand Down Expand Up @@ -460,6 +460,11 @@ public virtual bool NotifyAnalysisComplete(IDocumentAnalysis analysis) {
OnAnalysisComplete();
ContentState = State.Analyzed;

// Do not report issues with libraries or stubs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if user has opened library file explicitly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see: #584 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don’t report issues in libraries, this is not something actionable to the user. Something like resharper does not produce issues in disassembled code...

if (ModuleType == ModuleType.User) {
_diagnosticsService?.Replace(Uri, _parseErrors.Concat(Analysis.Diagnostics));
}

var tcs = _analysisTcs;
_analysisTcs = null;
tcs.TrySetResult(analysis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal async Task InitializeAsync(CancellationToken cancellationToken = defaul

// Initialize built-in
var moduleName = BuiltinTypeId.Unknown.GetModuleName(_interpreter.LanguageVersion);
var modulePath = ModuleCache.GetCacheFilePath(_interpreter.Configuration.InterpreterPath ?? "python.exe");
var modulePath = ModuleCache.GetCacheFilePath(_interpreter.Configuration.InterpreterPath);

var b = new BuiltinsPythonModule(moduleName, modulePath, _services);
_modules[BuiltinModuleName] = BuiltinsModule = b;
Expand Down
3 changes: 3 additions & 0 deletions src/Analysis/Ast/Test/FluentAssertions/AssertionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ internal static class AssertionsFactory {
public static VariableAssertions Should(this IVariable v) => new VariableAssertions(v);

public static RangeAssertions Should(this Range? range) => new RangeAssertions(range);

public static SourceSpanAssertions Should(this SourceSpan span) => new SourceSpanAssertions(span);
public static SourceSpanAssertions Should(this SourceSpan? span) => new SourceSpanAssertions(span.Value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
using Microsoft.Python.Core.Text;
using static Microsoft.Python.Analysis.Tests.FluentAssertions.AssertionsUtilities;

namespace Microsoft.Python.LanguageServer.Tests.FluentAssertions {
internal sealed class SourceSpanAssertions {
namespace Microsoft.Python.Analysis.Tests.FluentAssertions {
public sealed class SourceSpanAssertions {
public SourceSpan? Subject { get; }

public SourceSpanAssertions(SourceSpan? span) {
Expand Down
Loading