microsoft / python-language-server

Microsoft Language Server for Python
Apache License 2.0
915 stars 133 forks source link

"self" is an unknown variable in a class method #1573

Open Anapo14 opened 5 years ago

Anapo14 commented 5 years ago

User from survey feedback reports that: Sometimes I get a warning that "self" is an unknown variable in a class method or constructor even though it is the first parameter. Moreover, sometimes it randomly applies only to some occurrences of "self" in the same method. Screenshot: image

joshwatson commented 5 years ago

I'm also seeing this occur with a lot of builtin keywords; super(), isinstance(), str, and int, to name a few.

bsandrow commented 5 years ago

I second @joshwatson. I have also been getting Undefined variable on builtins lately. Most of the time it disappears if I quit VS Code and re-open my workspace.

bsandrow commented 5 years ago

I was looking in the PSL log for a different issue, and saw some logging like:

Analysis of people.models (User) queued. Dependencies: datetime, enum, dateutil.relativedelta, django.conf, django.contrib.contenttypes.models, django.core.cache, django.core.exceptions, django.core.validators, django.db, django.db.models, django.db.transaction, django.utils, django.utils.timezone, django.utils.functional, django_extensions.db.models, model_utils, mptt.fields, mptt.managers, mptt.models, phonenumber_field.modelfields, simple_history.models, sorl.thumbnail, qlib.n0_qlibBasicTypes, constants, main.custom_fields, main.models_base, main.util.api, main.util.encrypted_fields, main.util.field_tracker, main.util.geo, main.util.models, main.util.overridable_model, main.util.utilities, people.models_base, qlibengine.tracking.qlib_base_ids, people.validators, accounts.models
Analysis version 8458 of 7145 entries has started.
Analysis of django.utils(Library) canceled (no AST yet).
Analysis of django.core.exceptions(Library) canceled (no AST yet).
Analysis of itertools(CompiledBuiltin) canceled (no AST yet).
Analysis of sys(CompiledBuiltin) canceled (no AST yet).
Analysis of _collections(CompiledBuiltin) canceled (no AST yet).
Analysis of time(CompiledBuiltin) canceled (no AST yet).
Analysis of django.contrib(Library) canceled (no AST yet).
Analysis of posix(CompiledBuiltin) canceled (no AST yet).
Analysis of _codecs(CompiledBuiltin) canceled (no AST yet).
Analysis of pwd(CompiledBuiltin) canceled (no AST yet).
Analysis of _warnings(CompiledBuiltin) canceled (no AST yet).
Analysis of zipimport(CompiledBuiltin) canceled (no AST yet).
Analysis of people.validators (User) on depth 0 completed in 0.26 ms.
Analysis of main.util.geo (User) on depth 0 completed in 0.26 ms.
Analysis of _weakref(CompiledBuiltin) canceled (no AST yet).
Analysis of atexit(CompiledBuiltin) canceled (no AST yet).
Analysis of _thread(CompiledBuiltin) canceled (no AST yet).
Analysis of errno(CompiledBuiltin) canceled (no AST yet).
Analysis of _string(CompiledBuiltin) canceled (no AST yet).
Analysis of _tracemalloc(CompiledBuiltin) canceled (no AST yet).
Analysis of _locale(CompiledBuiltin) canceled (no AST yet).
Analysis of _ast(CompiledBuiltin) canceled (no AST yet).
Analysis of gc(CompiledBuiltin) canceled (no AST yet).
Analysis of marshal(CompiledBuiltin) canceled (no AST yet).
Analysis of _stat(CompiledBuiltin) canceled (no AST yet).
Analysis of _sre(CompiledBuiltin) canceled (no AST yet).
Analysis of _signal(CompiledBuiltin) canceled (no AST yet).
Analysis of qlib.<snip>(Library) failed. System.NotImplementedException: The method or operation is not implemented.
   at Microsoft.Python.Analysis.Types.Collections.PythonCollectionType.CreateSpecificType(IArgumentSet typeArguments) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Types\Collections\PythonCollectionType.cs:line 70
   at Microsoft.Python.Parsing.Ast.TypeAnnotation.Walker.MakeGenericOp.Apply[T](TypeAnnotationConverter`1 converter, Stack`1 stack) in E:\A\_work\8\s\src\Parsing\Impl\Ast\TypeAnnotation.cs:line 272
   at Microsoft.Python.Parsing.Ast.TypeAnnotation.Walker.GetResult[T](TypeAnnotationConverter`1 converter) in E:\A\_work\8\s\src\Parsing\Impl\Ast\TypeAnnotation.cs:line 68
   at Microsoft.Python.Analysis.Analyzer.Evaluation.ExpressionEval.CreateFunctionParameters(IPythonClassType self, IPythonClassMember function, FunctionDefinition fd, Boolean declareVariables) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Evaluation\ExpressionEval.Callables.cs:line 352
   at Microsoft.Python.Analysis.Analyzer.Symbols.FunctionEvaluator.Evaluate() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Symbols\FunctionEvaluator.cs:line 82
   at Microsoft.Python.Analysis.Analyzer.Symbols.ModuleSymbolTable.EvaluateAll() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Symbols\ModuleSymbolTable.cs:line 67
   at Microsoft.Python.Analysis.Analyzer.ModuleWalker.Complete() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\ModuleWalker.cs:line 202
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.DoAnalyzeEntry(IDependencyChainNode`1 node, IPythonModule module, PythonAst ast, Int32 version) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 359
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.AnalyzeEntry(IDependencyChainNode`1 node, PythonAnalyzerEntry entry, IPythonModule module, PythonAst ast, Int32 version) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 337
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.Analyze(IDependencyChainNode`1 node, AsyncCountdownEvent ace, Stopwatch stopWatch) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 277

Specifically stuff like Analysis of gc(CompiledBuiltin) canceled (no AST yet). jumps out to me as possibly related to the issue of built-ins not being recognized.

MikhailArkhipov commented 5 years ago

Yes, the exception is the red flag. Analysis of gc(CompiledBuiltin) canceled (no AST yet) is normal. Analysis is multithreaded so request to analyze module may come on one of the branches of the dependency graph earlier than the module source is parsed. In this case this branch is canceled and module will be analyzed when parsing completes on another path.

MikhailArkhipov commented 5 years ago

Is qlib.<snip> proprietary?

bsandrow commented 5 years ago

Is qlib. proprietary?

It's a proprietary library. I'm not sure that the <snip>ed part really matters if I post it or not, but I'm err'ing on the side of caution. It's just the path to the module (pure python) that caused it to blow up. That said, in the version of the library that I'm running, the last change to the code for that module was December 2018. I've been using PSL instead of jedi since at least December 2018. This hints to me that it may be a regression on the PSL side. Or new functionality that was added and introduced bugs?

Yes, the exception is the red flag. Analysis of gc(CompiledBuiltin) canceled (no AST yet) is normal. Analysis is multithreaded so request to analyze module may come on one of the branches of the dependency graph earlier than the module source is parsed. In this case this branch is canceled and module will be analyzed when parsing completes on another path.

In my most recent logs, I'll have a Analysis of posix(CompiledBuiltin) canceled (no AST yet)., but then no log of completion for that builtin module before the log telling me that analysis has been completed for X entries. This seems to hint to me that the analysis may be finishing prematurely but not as successfully as the log message would have me believe.

bsandrow commented 5 years ago

As an example, here is an analysis log straight out of my current editor:

Analysis version 12151 of 7130 entries has started.
Analysis of itertools(CompiledBuiltin) canceled (no AST yet).
Analysis of _collections(CompiledBuiltin) canceled (no AST yet).
Analysis of time(CompiledBuiltin) canceled (no AST yet).
Analysis of sys(CompiledBuiltin) canceled (no AST yet).
Analysis of _thread(CompiledBuiltin) canceled (no AST yet).
Analysis of _locale(CompiledBuiltin) canceled (no AST yet).
Analysis of _weakref(CompiledBuiltin) canceled (no AST yet).
Analysis of pwd(CompiledBuiltin) canceled (no AST yet).
Analysis of errno(CompiledBuiltin) canceled (no AST yet).
Analysis of _operator(CompiledBuiltin) canceled (no AST yet).
Analysis of marshal(CompiledBuiltin) canceled (no AST yet).
Analysis of zipimport(CompiledBuiltin) canceled (no AST yet).
Analysis of atexit(CompiledBuiltin) canceled (no AST yet).
Analysis of _sre(CompiledBuiltin) canceled (no AST yet).
Analysis of _string(CompiledBuiltin) canceled (no AST yet).
Analysis of _imp(CompiledBuiltin) canceled (no AST yet).
Analysis of _codecs(CompiledBuiltin) canceled (no AST yet).
Analysis of _functools(CompiledBuiltin) canceled (no AST yet).
Analysis of gc(CompiledBuiltin) canceled (no AST yet).
Analysis of _stat(CompiledBuiltin) canceled (no AST yet).
Analysis of _io(CompiledBuiltin) canceled (no AST yet).
Analysis of _ast(CompiledBuiltin) canceled (no AST yet).
Analysis of _signal(CompiledBuiltin) canceled (no AST yet).
Analysis of _warnings(CompiledBuiltin) canceled (no AST yet).
Analysis of posix(CompiledBuiltin) canceled (no AST yet).
Analysis of _tracemalloc(CompiledBuiltin) canceled (no AST yet).
Analysis of django.core.exceptions (Library) on depth 1 completed in 2.04 ms.
Analysis of django.utils (Library) on depth 1 completed in 3.02 ms.
Analysis of qlib.<snip>(Library) failed. System.NotImplementedException: The method or operation is not implemented.
   at Microsoft.Python.Analysis.Types.Collections.PythonCollectionType.CreateSpecificType(IArgumentSet typeArguments) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Types\Collections\PythonCollectionType.cs:line 70
   at Microsoft.Python.Parsing.Ast.TypeAnnotation.Walker.MakeGenericOp.Apply[T](TypeAnnotationConverter`1 converter, Stack`1 stack) in E:\A\_work\8\s\src\Parsing\Impl\Ast\TypeAnnotation.cs:line 272
   at Microsoft.Python.Parsing.Ast.TypeAnnotation.Walker.GetResult[T](TypeAnnotationConverter`1 converter) in E:\A\_work\8\s\src\Parsing\Impl\Ast\TypeAnnotation.cs:line 68
   at Microsoft.Python.Analysis.Analyzer.Evaluation.ExpressionEval.CreateFunctionParameters(IPythonClassType self, IPythonClassMember function, FunctionDefinition fd, Boolean declareVariables) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Evaluation\ExpressionEval.Callables.cs:line 352
   at Microsoft.Python.Analysis.Analyzer.Symbols.FunctionEvaluator.Evaluate() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Symbols\FunctionEvaluator.cs:line 82
   at Microsoft.Python.Analysis.Analyzer.Symbols.ModuleSymbolTable.EvaluateAll() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\Symbols\ModuleSymbolTable.cs:line 67
   at Microsoft.Python.Analysis.Analyzer.ModuleWalker.Complete() in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\ModuleWalker.cs:line 202
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.DoAnalyzeEntry(IDependencyChainNode`1 node, IPythonModule module, PythonAst ast, Int32 version) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 359
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.AnalyzeEntry(IDependencyChainNode`1 node, PythonAnalyzerEntry entry, IPythonModule module, PythonAst ast, Int32 version) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 337
   at Microsoft.Python.Analysis.Analyzer.PythonAnalyzerSession.Analyze(IDependencyChainNode`1 node, AsyncCountdownEvent ace, Stopwatch stopWatch) in E:\A\_work\8\s\src\Analysis\Ast\Impl\Analyzer\PythonAnalyzerSession.cs:line 277
Analysis version 12151 has been completed in 15.92 ms with 29 entries analyzed and 7101 entries skipped.

Very few completions, but a lot of cancellations.

bsandrow commented 5 years ago

Here's an example of the analysis log for a file that is calling builtins errors:

Analysis version 15619 of 2471 entries has started.
Analysis of atexit(CompiledBuiltin) canceled (no AST yet).
Analysis of time(CompiledBuiltin) canceled (no AST yet).
Analysis of itertools(CompiledBuiltin) canceled (no AST yet).
Analysis of gc(CompiledBuiltin) canceled (no AST yet).
Analysis of errno(CompiledBuiltin) canceled (no AST yet).
Analysis of _collections(CompiledBuiltin) canceled (no AST yet).
Analysis of _weakref(CompiledBuiltin) canceled (no AST yet).
Analysis of pwd(CompiledBuiltin) canceled (no AST yet).
Analysis of _thread(CompiledBuiltin) canceled (no AST yet).
Analysis of _ast(CompiledBuiltin) canceled (no AST yet).
Analysis of _imp(CompiledBuiltin) canceled (no AST yet).
Analysis of zipimport(CompiledBuiltin) canceled (no AST yet).
Analysis of enum (Stub) on depth 2 completed in 1.07 ms.
Analysis of marshal(CompiledBuiltin) canceled (no AST yet).
Analysis of _io(CompiledBuiltin) canceled (no AST yet).
Analysis of numbers (Stub) on depth 9 completed in 1.33 ms.
Analysis of time (Stub) on depth 3 completed in 6.32 ms.
Analysis of builtins (Builtins) on depth 0 completed in 51.48 ms.
Analysis version 15619 has been completed in 51.81 ms with 18 entries analyzed and 2453 entries skipped.
Screen Shot 2019-10-08 at 5 25 54 PM

Version: 0.4.39.0

jakebailey commented 5 years ago

Yeah, it seems like the analysis is cutting itself off early, before the user code has its scope declared. isinstance would be undefined if the code to produce the global scope never ran.

jakebailey commented 5 years ago

@bsandrow Is this still an issue for you? Revisiting this, the logs look a lot like the hangs that happened in #1601 (which is fixed in more recent versions, or by disabling caching).

trutane commented 4 years ago

Possibly related: in addition to self I also see the __module__ attribute getting flagged as undefined within my class definitions. The __name__, __class__, __file__ attributes do not get flagged.

VSCode Version: 1.41.0, Darwin Python extension: 2019.11.50794 (ms-python.python)