Giter Site home page Giter Site logo

microsoft / python-language-server Goto Github PK

View Code? Open in Web Editor NEW
908.0 38.0 173.0 5.89 MB

Microsoft Language Server for Python

License: Apache License 2.0

C# 73.59% Python 26.39% PowerShell 0.01%
python language-server language-server-protocol code-analysis

python-language-server's Introduction

Microsoft Python Language Server

Microsoft Python Language Server implements the Language Server Protocol.

Its primary clients are the Python extension for VS Code and Python Tools for Visual Studio.

Feel free to file issues or ask questions on our issue tracker, and we welcome code contributions.

Build/contributing instructions

See CONTRIBUTING.md

Troubleshooting and known issues

See TROUBLESHOOTING.md.

Linting options (diagnostics)

The language server implements diagnostics (or linting), which runs on user code. The following diagnostics are supported:

Code Description
inherit-non-class Attempted to inherit something that is not a class.
too-many-function-arguments Too many arguments have been provided to a function call.
too-many-positional-arguments-before-star Too many arguments have been provided before a starred argument.
no-cls-argument First parameter in a class method must be cls
no-method-argument Method has no arguments
no-self-argument First parameter in a method must be self
parameter-already-specified A argument with this name has already been specified.
parameter-missing A required positional argument is missing.
positional-argument-after-keyword A positional argument has been provided after a keyword argument.
positional-only-named A positional-only argument (3.8+) has been named in a function call.
return-in-init Encountered an explicit return in __init__ function.
typing-generic-arguments An error occurred while constructing Generic.
typing-newtype-arguments An error occurred while constructing NewType.
typing-typevar-arguments An error occurred while constructing TypeVar.
unknown-parameter-name The keyword argument name provided is unknown.
unresolved-import An import cannot be resolved, and may be missing.
undefined-variable A variable has been used that has not yet been defined.
variable-not-defined-globally A variable is not defined in the global scope.
variable-not-defined-nonlocal A variable is not defined in non-local scopes.

A full list can be seen in the source code.

Linting can be controlled via the user configuration. In VS Code, this is settings.json, but other clients would send this via workspace/didChangeConfiguration.

If python.linting.enabled is set to false in the user configuration, then no diagnostics will be collected other than syntax errors and unresolved imports.

To control the visibility and severity of the diagnotics, there are a number of lists that can be set in the user configuration which make use of each diagnostic's error code.

Setting Description
python.analysis.errors Diagnostics which should be shown as errors.
python.analysis.warnings Diagnostics which should be shown as warnings.
python.analysis.information Diagnostics which should be shown as informational.
python.analysis.disabled Diagnotics which should not be shown at all.

An example of a user configuration which sets these options:

{
    "python.analysis.errors": ["undefined-variable"],
    "python.analysis.warnings": ["unknown-parameter-name"],
    "python.analysis.information": ["unresolved-import"],
    "python.analysis.disabled": ["too-many-function-arguments", "parameter-missing"],
}

Linting can also be controlled on an individual line basis with a generalized #noqa. Lines with #noqa will have their diagnostic output suppressed.

An example usage:

from python import language_server  # noqa will suppress the linting message for this line

Cache location

During analysis language server produces Python code from compiled modules and builtins which is similar to Python module stubs. It may also produce database files holding module analysis for faster retrieval later. Cache location is at

Windows

"%LOCALAPPDATA%\Microsoft\Python Language Server" (which is Environment.SpecialFolder.LocalApplicationData). Typically "C:\Users\\%USER_NAME%\AppData\Local\Microsoft\Python Language Server"

Linux

"$XDG_CACHE_HOME/Microsoft/Python Language Server", or if XDG_CACHE_HOME is not set, "$HOME/.cache/Microsoft/Python Language Server"

macOS

"$HOME/Library/Caches/Microsoft/Python Language Server"

python-language-server's People

Contributors

adamyoblick avatar alexandersher avatar anapo14 avatar astenman avatar brianbok avatar bschnurr avatar ctrando avatar dimbleby avatar espositofulvio avatar felixhao28 avatar gramster avatar heejaechang avatar jakebailey avatar jamesralstin avatar lostmsu avatar m8mble avatar microsoftopensource avatar msftgits avatar rahulkumaran avatar rchiodo avatar rv404674 avatar shayash22 avatar thomasgassmann avatar tinysun212 avatar ujjwal-raizada avatar vigilancer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-language-server's Issues

Document symbols should include imported modules

In the VS Code Python extension we are sending a textDocument/documentSymbol request and using the result to position code lenses. The data we get back is mostly correct. However, some symbols are missing.

For example, using the following Python code:

import unittest

class SpamTests(unittest.TestCase):
    def test_all(self):
        self.assertTrue(False)

I got the following JSON response from the language server (formatted for clarity):

[{
    "name":"SpamTests",
    "detail":"SpamTests",
    "kind":5,
    "deprecated":false,
    "range":{
        "start":{"line":2,"character":6},
        "end":{"line":2,"character":15}
    },
    "selectionRange":{
        "start":{"line":2,"character":6},
        "end":{"line":2,"character":15}
    },
    "children":[{
        "name":"test_all",
        "detail":"test_all",
        "kind":12,
        "deprecated":false,
        "range":{
            "start":{"line":3,"character":4},
            "end":{"line":4,"character":30}
         },
         "selectionRange":{
             "start":{"line":3,"character":4},
             "end":{"line":4,"character":30}
         },
         "children":[{
             "name":"self",
             "detail":"self",
             "kind":13,
             "deprecated":false,
             "range":{
                 "start":{"line":3,"character":17},
                 "end":{"line":3,"character":21}
             },
             "selectionRange":{
                 "start":{"line":3,"character":17},
                 "end":{"line":3,"character":21}
             },
            "children":[],
            "_functionKind":""
        }],
        "_functionKind":"function"
    },
    {
        "name":"assertTrue",
        "detail":"assertTrue",
        "kind":13,
        "deprecated":false,
        "range":{
            "start":{"line":0,"character":0},
            "end":{"line":0,"character":0}
        },
        "selectionRange":{
            "start":{"line":0,"character":0},
            "end":{"line":0,"character":0}
        },
        "children":[],
        "_functionKind":""
    }],
    "_functionKind":"class"
}]

The symbol for the "unittest" module is missing.

Error detection feature request for new language server

You already have function signatures because you autocomplete them. So take it a step further and show that following code would raiseTypeError.

def f(foo):
    pass

f(bar='some')  # Note that 'bar' is not a valid part of the signature.

Support Django model objects in IntelliSense

This case is similar to microsoft/vscode-python#1072

Environment data

  • VS Code version: 1.25.1
  • Extension version (available under the Extensions sidebar): 2018.7.1
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda Python 3.6.5
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): Conda
  • Relevant/affected Python packages and their versions: Django 2.0.6

Actual behavior

Intellisense/pylint do not offer autocompletion for a member variable inherited from a base class and shows it as a problem: E1101:Class 'Question' has no 'objects' member

The code runs exactly as intended without errors.

I am actually following the Django tutorial from the official site.

Expected behavior

The object's member should be shown in intellisense and it shouldn't log it as a problem

Steps to reproduce:

It happens when following the tutorial from djangoproject.org:
https://docs.djangoproject.com/en/2.0/intro/tutorial03/

in section: Write views that actually do something:

from django.http import HttpResponse
from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Everything runs ok. there is actually nothing in output.

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

System check identified no issues (0 silenced).
July 28, 2018 - 10:47:36
Django version 2.0.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[28/Jul/2018 10:47:43] "GET /polls/ HTTP/1.1" 200 11

Output from 'Problems'

{
	"resource": "/c:/Users/Ramon Leon/Documents/DjangoProjects/mysite/polls/views.py",
	"owner": "python",
	"code": "E1101",
	"severity": 8,
	"message": "E1101:Class 'Question' has no 'objects' member",
	"source": "pylint",
	"startLineNumber": 9,
	"startColumn": 28,
	"endLineNumber": 9,
	"endColumn": 28
}

Have hover popups for class instantiation also show constructor docstring

Environment data

  • VS Code version: 1.25.1
  • Extension version (available under the Extensions sidebar): 2018.6.0
  • OS and version: Ubuntu 16.04
  • Python version (& distribution if applicable, e.g. Anaconda): Anaconda, 3.6.6
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Relevant/affected Python packages and their versions: N/A

Actual behavior

Here's some screenshots:

screenshot from 2018-07-13 14-53-55
screenshot from 2018-07-13 14-54-09

Note that in both screenshots, the hover popup only shows the Class docstring.

Note: If there is no class docstring, the hover popup has no docstring info.

Expected (requested?) behavior

Hover popup would include constructor docstring. This is how e.g., IPython behaves.

Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import test

In [2]: test.TestClass?
Init signature: test.TestClass(my_param)
Docstring:      Class docstring
Init docstring: Constructor docstring
File:           ~/Code/test/test.py
Type:           type

Steps to reproduce:

N/A

Thanks for the work on the great extension.

No completion in namespace packages

.
├── projecta
│   └── foo
│       ├── bar
│       │   └── __init__.py
│       └── __init__.py
└── projectb
    └── foo
        ├── baz
        │   └── __init__.py
        └── __init__.py

test.py:

import projectA.foo
import projectA.foo.bar
import projectB.foo
import projectB.foo.baz

projectA.

image

Jedi:
image

PyCharm:
image

Symbols returned with wrong kind.

In the VS Code Python extension we are sending a textDocument/documentSymbol request and using the result to position code lenses. The data we get back is mostly correct. However, the "kind" (i.e. VS Code's SymbolKind) is consistently off by one. For example, using the following Python code:

import unittest

class SpamTests(unittest.TestCase):
    def test_all(self):
        self.assertTrue(False)

I got the following JSON response from the language server (formatted for clarity):

[{
    "name":"SpamTests",
    "detail":"SpamTests",
    "kind":5,
    "deprecated":false,
    "range":{
        "start":{"line":2,"character":6},
        "end":{"line":2,"character":15}
    },
    "selectionRange":{
        "start":{"line":2,"character":6},
        "end":{"line":2,"character":15}
    },
    "children":[{
        "name":"test_all",
        "detail":"test_all",
        "kind":12,
        "deprecated":false,
        "range":{
            "start":{"line":3,"character":4},
            "end":{"line":4,"character":30}
         },
         "selectionRange":{
             "start":{"line":3,"character":4},
             "end":{"line":4,"character":30}
         },
         "children":[{
             "name":"self",
             "detail":"self",
             "kind":13,
             "deprecated":false,
             "range":{
                 "start":{"line":3,"character":17},
                 "end":{"line":3,"character":21}
             },
             "selectionRange":{
                 "start":{"line":3,"character":17},
                 "end":{"line":3,"character":21}
             },
            "children":[],
            "_functionKind":""
        }],
        "_functionKind":"function"
    },
    {
        "name":"assertTrue",
        "detail":"assertTrue",
        "kind":13,
        "deprecated":false,
        "range":{
            "start":{"line":0,"character":0},
            "end":{"line":0,"character":0}
        },
        "selectionRange":{
            "start":{"line":0,"character":0},
            "end":{"line":0,"character":0}
        },
        "children":[],
        "_functionKind":""
    }],
    "_functionKind":"class"
}]

Each of the "kind" fields is 1 more than it should be:

SpamTests -> 5; should be 4 (SymbolKind.Class)
test_all -> 12; should be 11 (SymbolKind.Function)
self -> 13; should be 12 (SymbolKind.Variable)
assertTrue -> 13; should be 12 (SymbolKind.Variable)

FWIW, the Jedi-related code in the Python extension gets it right.

MutatingReferences test fails

Looks like we are only looking into variables in a single file. We are not inspecting all files that may be referencing types from the file the user is is working with. I.e. FindReferences request comes with specific URI but we don't look into other files.

Error logged in Python output window -Error: command 'completion/itemSelected' already exists

Environment data

  • VS Code version: Insiders
  • Extension version (available under the Extensions sidebar): Dev
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): N/A
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): N/A
  • Relevant/affected Python packages and their versions: N/A

Actual behavior

Error in Python Extension - Error: command 'completion/itemSelected' already exists

Expected behavior

No Errors.

Steps to reproduce:

  1. Open VSC
  2. Open a Python file

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Starting Microsoft Python language server.
[Error - 1:46:13 PM] Server initialization failed.
Error: command 'completion/itemSelected' already exists
	at e.registerCommand (c:\Program Files\Microsoft VS Code Insiders\resources\app\out\vs\workbench\node\extensionHostProcess.js:689:906)
	at Object.registerCommand (c:\Program Files\Microsoft VS Code Insiders\resources\app\out\vs\workbench\node\extensionHostProcess.js:762:949)
	at ExecuteCommandFeature.register (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:1501:53)
	at ExecuteCommandFeature.initialize (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:1491:14)
	at LanguageClient.initializeFeatures (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:2168:21)
	at connection.initialize.then (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:1888:18)
	at <anonymous>
Initializing for C:\ProgramData\Anaconda3\python.exe
Initializing for C:\ProgramData\Anaconda3\python.exe
[Error - 1:46:20 PM] System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.PythonTools.Interpreter.Ast.AstPythonInterpreter.ImportModule(String name)
   at Microsoft.PythonTools.Analysis.ModuleTable.TryImport(String name, ModuleReference& res)
   at Microsoft.PythonTools.Analysis.Analyzer.DDG.TryImportModule(String modName, Boolean forceAbsolute, ModuleReference& moduleRef, IReadOnlyList`1& remainingParts)
   at Microsoft.PythonTools.Analysis.Analyzer.DDG.Walk(FromImportStatement node)
   at Microsoft.PythonTools.Parsing.Ast.FromImportStatement.Walk(PythonWalker walker)
   at Microsoft.PythonTools.Analysis.Analyzer.DDG.Walk(SuiteStatement node)
   at Microsoft.PythonTools.Parsing.Ast.SuiteStatement.Walk(PythonWalker walker)
   at Microsoft.PythonTools.Parsing.Ast.PythonAst.Walk(PythonWalker walker)
   at Microsoft.PythonTools.Analysis.AnalysisUnit.AnalyzeWorker(DDG ddg, CancellationToken cancel)
   at Microsoft.PythonTools.Analysis.Analyzer.DDG.Analyze(Deque`1 queue, CancellationToken cancel, Action`1 reportQueueSize, Int32 reportQueueInterval)
   at Microsoft.PythonTools.Analysis.PythonAnalyzer.AnalyzeQueuedEntries(CancellationToken cancel)
   at Microsoft.PythonTools.Intellisense.AnalysisQueue.GroupAnalysis.Analyze(CancellationToken cancel)
   at Microsoft.PythonTools.Intellisense.AnalysisQueue.HandleAnalyzable(IAnalyzable item, AnalysisPriority priority, CancellationToken cancellationToken)
   at Microsoft.PythonTools.Intellisense.AnalysisQueue.ConsumerLoop()
Reloading modules...
Reloading modules...

Unhandled Exception: System.ObjectDisposedException: PriorityProducerConsumer`1 instance is disposed
Object name: 'PriorityProducerConsumer`1'.
   at Microsoft.PythonTools.Analysis.Infrastructure.DisposeToken.ThrowIfDisposed()
   at Microsoft.PythonTools.Analysis.Infrastructure.PriorityProducerConsumer`1.Produce(T value, Int32 priority)
   at Microsoft.PythonTools.Intellisense.AnalysisQueue.Enqueue(IAnalyzable item, AnalysisPriority priority)
   at Microsoft.PythonTools.Analysis.LanguageServer.Server.ReloadModulesAsync(CancellationToken token)
   at Microsoft.PythonTools.Analysis.Infrastructure.TaskExtensions.DoNotWait(Task task)
   at Microsoft.Python.LanguageServer.Implementation.LanguageServer.<DidChangeConfiguration>b__29_0()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
[Info  - 1:46:22 PM] Connection to server got closed. Server will restart.
[Error - 1:46:22 PM] Request textDocument/documentSymbol failed.
Error: Connection got disposed.
	at Object.dispose (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\main.js:825:25)
	at Object.dispose (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:57:35)
	at LanguageClient.handleConnectionClosed (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:2036:42)
	at LanguageClient.handleConnectionClosed (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\main.js:127:15)
	at closeHandler (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-languageclient\lib\client.js:2023:18)
	at CallbackList.invoke (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\events.js:71:39)
	at Emitter.fire (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\events.js:135:36)
	at closeHandler (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\main.js:221:26)
	at CallbackList.invoke (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\events.js:71:39)
	at Emitter.fire (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\events.js:135:36)
	at StreamMessageReader.AbstractMessageReader.fireClose (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\messageReader.js:135:27)
	at Socket.<anonymous> (C:\Users\dojayama.REDMOND\.vscode-insiders\extensions\pythonVSCode\node_modules\vscode-jsonrpc\lib\messageReader.js:188:62)
	at emitOne (events.js:101:20)
	at Socket.emit (events.js:191:7)
	at Pipe._handle.close [as _onclose] (net.js:510:12)
Initializing for C:\ProgramData\Anaconda3\python.exe
Reloading modules...
Reloading modules...
Reloading modules...

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

XXX

'Go to Definition' for relative imports not resolving directly to the appropriate module

Let's say I have pkg and pkg.subpkg, both packages. I also have pkg.mod and pkg.subpkg.submod, both modules. If I do from .. import mod from within pkg.subpkg.submode I get asked which package to resolve to -- pkg/__init__.py or pkg/mod.py -- even though pkg.__init__ is empty. I would expect the lack of ambiguity to not ask me which package to resolve to.

Attached are the test files I was using.
pkg.zip

Symbols never reported for new files

In VS Code

  1. Add new Python file
  2. Type or paste basic class definition

Document outline never populates even if you edit the file. Close and reopen file - now it works.

Intellisense tooltip applies text formatting on source code

microsoft/vscode-python#2225

Environment data

  • VS Code version: 1.25.1
  • Extension version (available under the Extensions sidebar): 2018.7.0
  • OS and version: Windows 10
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.0
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: NA

Actual behavior

The tooltip applies text formatting to the displayed code, e.g. wrapping text in underscores italicizes it.
image

Expected behavior

The tooltip shows the code as it is written in the source.

Steps to reproduce:

  1. Use the mypy linter
  2. Define a variable in a class, using self, with underscores in its name
  3. Assign a type to the variable with a type hint
  4. Hover over it to see the Intellisense tooltip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.