microsoft / python-language-server

Microsoft Language Server for Python
Apache License 2.0
912 stars 131 forks source link

Support 'strict typing mode' (mypy compatibility) for improved performance and correctness #879

Open matangover opened 5 years ago

matangover commented 5 years ago

I use mypy to ensure type safety and avoid runtime errors. Functions are annotated and typing is enforced. However, PLS hovers and completions are based on heuristics that aren't as strict as typing rules used by mypy ( / pyright / pyre / pytype).

Some of the differences:

In short PLS is 'too smart' for me. This causes PLS to be quite slow when there are large third-party modules installed (scipy etc). Also, hovers and completions don't match the 'true' mypy types and I find myself needing reveal_type a lot.

This is a feature request to support a "mypy-compatible" strict typing mode, that will also be faster (as a side effect). Seems like pyright is on the right track for this with hovers / go to definition -- but it doesn't support code completion, find references, refactor etc -- and isn't clear whether it intends to do so.

In case you are interested in supporting this use case, I've made a POC for PLS where I've disabled some inferences and changed module resolution to only take stubs for third-party libs. Here's the diff (warning: messy poc code). The downside is that now documentation is missing (because Typeshed doesn't have docs) and navigation goes to stub files (not so bad).

Code examples for things that are 'too smart':

jakebailey commented 4 years ago

This is an old issue, but you'd be interested in checking out Pylance, which uses Pyright for type checking. Announcement here: https://devblogs.microsoft.com/python/announcing-pylance-fast-feature-rich-language-support-for-python-in-visual-studio-code/