Giter Club home page Giter Club logo

pyls-mypy's Introduction

Mypy plugin for PYLS

image

image

This is a plugin for the Palantir's Python Language Server (https://github.com/palantir/python-language-server)

It, like mypy, requires Python 3.2 or newer.

Installation

Install into the same virtualenv as pyls itself.

pip install pyls-mypy

Configuration

live_mode (default is True) provides type checking as you type.

As mypy is unaware of what file path is being checked, there are limitations with live_mode
  • Imports cannot be followed correctly
  • Stub files are not validated correctly

Turning off live_mode means you must save your changes for mypy diagnostics to update correctly.

Depending on your editor, the configuration should be roughly like this:

"pyls":
{
    "plugins":
    {
        "pyls_mypy":
        {
            "enabled": true,
            "live_mode": false
        }
    }
}

pyls-mypy's People

Contributors

amjadhd avatar e-kwsm avatar eliwe avatar kolanich avatar mic92 avatar singulared avatar st4lk avatar tomv564 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

pyls-mypy's Issues

Spawns way to many python processes.

Not sure if its a bug. Tried python-language-server and it spawned only 1 process, but after doing a pip install and starting to use pyls-mypy, pyls-mypy spawned 10+

Cannot perform relative import

Some relative imports cause errors. Here's an example:

Directory structure:

foo/
├── bar.py
├── baz.py
└── __init__.py

bar.py:

a = 17

baz.py:

from . import bar

a = bar.a

Runtime test:

> python
Python 3.8.0 (default, Oct 23 2019, 18:51:26) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.baz
>>> foo.baz.a
17

Mypy test:

> mypy foo
Success: no issues found in 3 source files

But when pyls-mypy is installed, the language server complains about the relative import in baz.py:

No parent module -- cannot perform relative import

If I uninstall this extension, the error disappears and the rest of the language server works as expected but without Mypy messages.

Appears to be reporting diagnostics for all open files

I'm running Atom with ide-python and after some fidling managed to get it do debug stuff.

Basically pyls is reporting mypy errors on the cuurent file; even when they are actually on another open tab. Here, my __main__.py's line 18(I'm guessing lines are zero indexed) is @click_log.simple_verbosity_option(logger).

Received notification 'textDocument/publishDiagnostics'. Params: {
    "uri": "file:///home/opal/piwik3/piwik-release-build/matomo_dl/__main__.py",
    "diagnostics": [
        {
            "source": "mypy",
            "range": {
                "start": {
                    "line": 17,
                    "character": 20
                },
                "end": {
                    "line": 17,
                    "character": 21
                }
            },
            "message": "\"Version\" has no attribute \"version\"",
            "severity": 1
        },
... (many more errors)

Skipping analyzing error

I never got this right in sublime text:

Screenshot from 2020-04-12 18-19-58

How do I fix this missing imports error? I'm using pyls-mypy version 0.1.8

My project settings are as follows:

{
	"folders":
	[
		{
			"path": "."
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/LSP-eslint"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/metals-sublime"
		},
		{
			"path": "/home/raoul/Documents/Programming/tonic"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/User"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/lsp_utils"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/LSP-json"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/UnitTesting"
		},
		{
			"path": "/home/raoul/.config/sublime-text-3/Packages/LSP-vue"
		},
		{
			"path": "../LSP-css"
		},
		{
			"path": "../LSP-html"
		},
		{
			"path": "../LSP-intelephense"
		},
		{
			"path": "../LSP-typescript"
		},
		{
			"path": "../LSP-elm"
		}
	],
	"settings":
	{
		"rulers":
		[
			120
		],
		"tab_size": 4,
		"translate_tabs_to_spaces": true,
		"lsp_format_on_save": true,
		"LSP":
		{
			"pyls":
			{
				"command": ["pyls"],
				"scopes": ["source.python"],
				"syntaxes": ["Packages/Python/Python.sublime-syntax"],
				"languageId": "python",
				"env":
				{
					"PYTHONPATH": "/opt/sublime_text/Lib/python33",
					"MYPYPATH": "/opt/sublime_text/Lib/python33"
				},
				"enabled": true,
			}
		}
	}
}

I Also tried this env:

				"env":
				{
					"PYTHONPATH": "/opt/sublime_text/Lib/python33",
					"MYPYPATH": "/home/raoul/.config/sublime-text-3/Packages/LSP/plugin"
				},

But it doesn't seem to have any effect. I also tried this:

				"env":
				{
					"PYTHONPATH": "/opt/sublime_text/Lib/python33:/home/raoul/.config/sublime-text-3/Packages/LSP/plugin"
				},

But to no avail.

mypy and workspace

Afaiu pyls-mypy runs mypy on a specific file.

Maybe it is possible to run it on the whole workspace?
Latest versions of mypy work amazingly with the whole project setups and i think it will be a huge benefit.

Overloads signatures

@overload
def add(x: int, y:int) -> int: ...

@overload
def add(x: int, y: float) -> float: ...

def add(x, y):
    return x + y

can pyls-mypy be modified send multiple signatures in this case ?

mypy errors are not captured and end up directly in the process stdout

While debugging an error appearing in Atom IDE-Python plugin, we found that some errors reported by mypy sometimes end up appearing directly in the process stdout (outside on the JSON body) which breaks the protocol:

Content-Length: 43
Content-Type: application/vscode-jsonrpc; charset=utf8

{"jsonrpc": "2.0", "id": 5, "result": null}
:1:1: error: Cannot find module named 'cryptography.hazmat.primitives'
:1:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
:2:1: error: Cannot find module named 'cryptography.hazmat.primitives.kdf.hkdf'
:3:1: error: Cannot find module named 'cryptography.hazmat.backends.openssl'
:4:1: error: Cannot find module named 'cryptography.hazmat.primitives.ciphers'
:11:1: error: Cannot find module named 'filetype'

You can easily check the output of pyls with sysdig:

sysdig -s6000 'proc.cmdline="python -m pyls"' -c stdout

AFAIU, this happens because mypy API currently works by temporarily overriding sys.stdout and sys.stderr which is not thread-safe:

def _run(f: Callable[[], None]) -> Tuple[str, str, int]:
    old_stdout = sys.stdout
    new_stdout = StringIO()
    sys.stdout = new_stdout

    old_stderr = sys.stderr
    new_stderr = StringIO()
    sys.stderr = new_stderr

    try:
        f()
        exit_status = 0
    except SystemExit as system_exit:
        exit_status = system_exit.code
    finally:
        sys.stdout = old_stdout
        sys.stderr = old_stderr

    return new_stdout.getvalue(), new_stderr.getvalue(), exit_status

def run(args: List[str]) -> Tuple[str, str, int]:
    # Lazy import to avoid needing to import all of mypy to call run_dmypy
    from mypy.main import main
    return _run(lambda: main(None, args=args))

The linter tasks are handled in separate threads because of the debouncing feature of python-language-server:

def debounce(interval_s, keyed_by=None):
    """Debounce calls to this function until interval_s seconds have passed."""
    def wrapper(func):
        timers = {}
        lock = threading.Lock()

        @functools.wraps(func)
        def debounced(*args, **kwargs):
            call_args = inspect.getcallargs(func, *args, **kwargs)
            key = call_args[keyed_by] if keyed_by else None

            def run():
                with lock:
                    del timers[key]
                return func(*args, **kwargs)

            with lock:
                old_timer = timers.get(key)
                if old_timer:
                    old_timer.cancel()

                timer = threading.Timer(interval_s, run)
                timers[key] = timer
                timer.start()
        return debounced
    return wrapper

Because stdout overriding is not thread safe, sys.stout may not be correctly restored and some errors are actually reported in the real/original/system stdout instead of the StringIO one.

Missing error?

I'm using pyls-mypy via lsp-mode in emacs, and noticed a line that should error but didn't. On digging, this error was found by mypy on the command line as well as by directly enabling flycheck python-mypy. Any ideas why this might be happening?

Configuration to point out a .mypy.ini

Excuse me if it already exists, by from my searching and reading the README I come to conclusion it isn't.

The usecase I'm trying to solve is that I have a file hierarchy as follows:

my_module.py
.mypy.ini
subdir/sub_module.py

When I open sub_module.py which imports my_module.py, I get many Cannot find implementation or library stub for module named 'my_module' for all modules that are in the parent dir. As I understand, the problem comes down to pyls-mypy needing some config with the list of files, which is probably .mypy.ini. So probably some option is needed, so users could point out pyls-mypy to that configuration file.

pyls-mypy should use the :rootUri parameter to find the configuration file

lsp-mode for emacs runs pyls in the directory of the first file visited in a project. If this directory is not the root of the project mypy will fail to find the configuration file. Using the :rootUri parameter we could either change the current working directory before running mypy or try to find the configuration file and pass it as an argument to mypy.

I also reported this in lsp-mode emacs-lsp/lsp-mode#319

Update plugin to use python-lsp-server instead of python-language-server

Hi,

Given that Palantir removed all the community maintenance for both python-language-server (pyls) and python-jsonrpc-server (pyls_jsonrpc), we decided to create a separate organization that now maintains a community-based fork for the aforementioned packages, which can be found at: https://github.com/python-lsp/python-lsp-server and https://github.com/python-lsp/python-lsp-jsonrpc.

Our recommendation is to migrate this plugin to be compatible with the community server instead of the Palantir one. In order to do so, here is a migration guide that should help:

  1. The plugin should replace python-language-server by python-lsp-server on its requirements.
  2. The pyls namespace is now pylsp, which means that all pyls imports should be replaced by pylsp.
  3. The pyls_* hookspecs are now pylsp_* hookspecs.
  4. The plugin can preserve the pyls_ prefix on its namespace in order to prevent further administration and release issues that may arise from renaming it.

Feel free to ask any question about the migration process or about the new community organization for the server.

error in process filter: Wrong type argument: hash-table-p, nil

I installed pyls-mypy and now I get error in process filter: lsp--on-diagnostics: Wrong type argument: hash-table-p, nil when starting up and error in process filter: Wrong type argument: hash-table-p, nil upon moving the cursor. Uninstalling pyls-mypy did not solve the problem. Any advice on this?

Pip install error

First off, thanks for working on this! I use VS Code daily, so it is quite handy.

When I pip install pyls-mypy I get

ModuleNotFoundError: No module named 'versioneer'

Thought I'd mention this.

I got around it by installing via git.

Is it possible to set --custom-typeshed option?

I have typeshed installed from the openSUSE package python-typeshed which is in /usr/share/typeshed. Is it possible somehow to set option --custom-typeshed-dir for the mypy, so I can use it?

Run the mypy daemon?

Would be more performant, there is a risk with process management (does pyls provide a shutdown hook?), and the daemon doesn't support follow_imports?

Error: Header must provide a Content-Length property.

whenever I pull pyls-mypy in my python environment coc.nvim warns of a language server error:

|| [coc.nvim] error: Uncaught exception: Error: Header must provide a Content-Length property.
||     at StreamMessageReader.onData (/nix/store/nmpby0f5ryb09iri83xhbhrcsn9247hv-vimplugin-coc-nvim-2019-05-20/share/vim-plugins/coc-nvim/build/index.js:38575:27)
||     at Socket.readable.on (/nix/store/nmpby0f5ryb09iri83xhbhrcsn9247hv-vimplugin-coc-nvim-2019-05-20/share/vim-plugins/coc-nvim/build/index.js:38560:18)
||     at emitOne (events.js:116:13)
||     at Socket.emit (events.js:211:7)
||     at addChunk (_stream_readable.js:263:12)
||     at readableAddChunk (_stream_readable.js:250:11)
||     at Socket.Readable.push (_stream_readable.js:208:10)
||     at Pipe.onread (net.js:601:20)
|| [coc.nvim] error: Uncaught exception: Error: Header must provide a Content-Length property.
||     at StreamMessageReader.onData (/nix/store/nmpby0f5ryb09iri83xhbhrcsn9247hv-vimplugin-coc-nvim-2019-05-20/share/vim-plugins/coc-nvim/build/index.js:38575:27)
||     at Socket.readable.on (/nix/store/nmpby0f5ryb09iri83xhbhrcsn9247hv-vimplugin-coc-nvim-2019-05-20/share/vim-plugins/coc-nvim/build/index.js:38560:18)
||     at emitOne (events.js:116:13)
||     at Socket.emit (events.js:211:7)
||     at addChunk (_stream_readable.js:263:12)
||     at readableAddChunk (_stream_readable.js:250:11)
||     at Socket.Readable.push (_stream_readable.js:208:10)
||     at Pipe.onread (net.js:601:20)
|| [coc.nvim] error: Uncaught exception: Error: Header must provide a Content-Length property.
||     at StreamMessageReader.onData (/nix/store/nmpby0f5ryb09iri83xhbhrcsn9247hv-vimplugin-coc-nvim-2019-05-20/share/vim-plugins/coc-nvim/build/index.js:38575:27)

How to disable live_mode?

For this reason: python/mypy#6070

I need to turn off live_mode when I use it in the emacs lsp-mode.

Normally, when I need to turn off pycodestyle, according to the settings here https://github.com/palantir/python-language-server/blob/develop/vscode-client/package.json . I just need to set pyls.plugins.pycodestyle.enabled to false.

I have tried setting both pyls.plugins.pyls_mypy.live_mode and pyls.pyls_mypy.live_mode to false, but it still in live_mode.

Did I miss something that led to the failure of the config? Or is the problem on the lsp-mode?

Language server error when pyls-mypy is installed

Hi -

when I use pyls and pyls-mypy on a python file in Emacs everything works fine for a while. Documentation pops up, function signatures are shown inline, etc. Then, after perhaps a few dozen interactions I get the following (repeatable) error:

Error from the Language Server: jedi.api.environment.InvalidPythonEnvironment: Could not get version information for ’/Users/dkatz/.local/share/virtualenvs/lsp_example_project-cOuqdw3J/bin/python3.7’: OSError(24, ’Too many open files’) (Invalid Parameters) [16 times]

Once that error is raised, I see it every time the cursor hits any interesting piece of code.

If I uninstall pyls-mypy from my python environment, the problem goes away.

I can trigger this behavior even with a completely trivial file like this:

import sys

by just moving the cursor on and off of sys several times (which I assume triggers communication with the server each time as it pops up the module information).

If I watch the number of python processes as a function of how I interact with the server I see something like this (taken from moving the cursor on and off of sys in the trivial file mentioned above):

event effect python processes
Open python file in Emacs 5
Put cursor on sys pop up module info 13
Move cursor off sys module info popup goes away 13
Put cursor back on sys pop up module info 18
Move cursor off sys module info goes away 19
Put cursor back on sys pop up module info 23
Move cursor off sys module info goes away 24
Put cursor back on sys pop up module info 28
Move cursor off sys module info goes away 29
Put cursor back on sys pop up module info 34
Move cursor off sys module info goes away 35
Put cursor back on sys pop up module info 39
Move cursor off sys module info goes away 40
Put cursor back on sys pop up module info 44
Move cursor off sys module info goes away 45
Put cursor back on sys pop up module info 49
Move cursor off sys module info goes away 50

and so on until I got the error with 81 python processes running. The number 81 seems to be pretty consistent for me - I get that number of processes with different files and different interactions (e.g. module info, function info, displaying function signatures, etc.)

Any ideas?

Thanks.

Dan


\

Setup:

OS: macOS 10.13.6

Python: 3.7.0
Python package installer: pipenv
Python packages:

  • python-language-server 0.21.2 [all]
  • pyls-mypy 0.1.3

An overloaded function outside a stub file must have an implementation

I'm not sure whether this is pyls-mypy, pyls or mypy's issue:
I have two files:
mod.py:

def add(x, y):
    return x + y

and mod.pyi:

from typing import overload


@overload
def add(x: int, y: int) -> int: ...


@overload
def add(x: int, y: float) -> float: ...

in mod.pyi i get this error on the first overload:
mypy

but when I check in the command line, I get no errors:

Microsoft Windows [version 10.0.17763.678]
(c) 2018 Microsoft Corporation. Tous droits réservés.

D:\Amjad\test_mypy>mypy mod.pyi

D:\Amjad\test_mypy>mypy mod.py

D:\Amjad\test_mypy>

Doesn't seem to work

Here is my pyls config:

lspconfig.pyls.setup {
  cmd = {"pyls"},
  filetypes = {"python"},
  settings = {
    pyls = {
      configurationSources = {"flake8"},
      plugins = {
        jedi_completion = {enabled = true},
        jedi_hover = {enabled = true},
        jedi_references = {enabled = true},
        jedi_signature_help = {enabled = true},
        jedi_symbols = {enabled = true, all_scopes = true},
        pycodestyle = {enabled = false},
        flake8 = {
          enabled = true,
          ignore = {},
          maxLineLength = 160
        },
        pyls_mypy = {enabled = true, live_mode = true},
        pyls_isort = {enabled = true},
        yapf = {enabled = false},
        pylint = {enabled = false},
        pydocstyle = {enabled = false},
        mccabe = {enabled = false},
        preload = {enabled = false},
        rope_completion = {enabled = false}
      }
    }
  },
  on_attach = on_attach
}

No errors from pyls_mypy
image

Running mypy from shell show an error:

mypy bla.py
bla.py:8: error: Argument 1 to "bla" has incompatible type "int"; expected "List[str]"
Found 1 error in 1 file (checked 1 source file)

Using with emacs

This is not an actual issue. I'm just trying to get this plugin work with lsp-mode for emacs. I have mypy workon fine on the commandline and I have lsp-mode working fine in emacs.

When I add pyls-mypy to my virtualenv I get mypy messages in my buffers but it doesn't seem to be reading my mypy.ini

Any pointers on how to remedy that would be most welcome.

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.