Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python type checking in VS Code [closed]

I've recently learned about typing module in Python (https://docs.python.org/3/library/typing.html) and expected to use it for static type checking and for better intellisense in VS Code, like it works with TypeScript, but I can't seem to find any tools/plugins that actually do that.

What are my options, if any?

like image 993
Alex Undefined Avatar asked Aug 23 '17 01:08

Alex Undefined


People also ask

Is Pyright open source?

A: Pyright is an open-source Python type checker and language server.

How do you turn off lint in VS Code?

You might want to disable linting by setting the vscode setting "r. lsp. diagnostics": false . Or you might want to customize the list of linters to be used by editing ~/.

Does Python do type checking?

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code. Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types.

Can we type Python in VS Code?

Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters.


1 Answers

from bash

mkdir test cd test python3 -m venv .env source .env/bin/activate python -m pip install flake8 python -m pip install flake8-mypy code ./ 

install plugin

then install this in VSCode https://marketplace.visualstudio.com/items?itemName=donjayamanne.python
and config

settings

./.vscode/settings.json

{     "python.envFile": "${workspaceRoot}/.env",     "python.pythonPath": ".env/bin/python",     "python.linting.flake8Enabled": true,     "python.linting.pylintEnabled": false,     "python.linting.mypyEnabled": true, } 

./.vscode/launch.json

{     "version": "0.2.0",     "configurations": [         {             "name": "Python",             "type": "python",             "request": "launch",             "stopOnEntry": false,             "pythonPath": "${config:python.pythonPath}",             "program": "${file}",             "cwd": "${workspaceRoot}",             "env": {},             "envFile": "${workspaceRoot}/.env",             "debugOptions": [                 "WaitOnAbnormalExit",                 "WaitOnNormalExit",                 "RedirectOutput"             ]         }     ] } 

OMG, this is Python 3 only!

https://pypi.python.org/pypi/flake8-mypy/17.3.3

Yes, so is mypy. Relax, you can run Flake8 with all popular plugins
as a tool perfectly fine under Python 3.5+ even if you want to analyze
Python 2 code. This way you’ll be able to parse all of the new syntax
supported on Python 3 but also effectively all the Python 2 syntax at
the same time.

By making the code exclusively Python 3.5+, I’m able to focus on the
quality of the checks and re-use all the nice features of the new
releases (check out pathlib) instead of wasting cycles on Unicode
compatibility, etc.

IDE & Linter Integrations

https://github.com/python/mypy#ide--linter-integrations

IDE & Linter Integrations

Mypy can be integrated into popular IDEs:

  • Vim: vim-mypy
  • Emacs: using Flycheck and Flycheck-mypy
  • Sublime Text: SublimeLinter-contrib-mypy
  • Atom: linter-mypy
  • PyCharm: PyCharm integrates its own implementation of PEP 484.

Mypy can also be integrated into Flake8 using flake8-mypy.

o ,-

  • https://github.com/python/mypy
  • https://pypi.org/project/flake8-mypy
  • https://docs.python.org/3/library/typing.html
like image 120
qwabra Avatar answered Sep 18 '22 12:09

qwabra