Pytype – A static type analyzer for Python code
github.com
github.com
https://news.ycombinator.com/item?id=19473631
People tend to do that on HN.
So, for those of us who don’t use Python type checking, which of the 4 checkers do we choose? Google, Facebook, Dropbox, or the new one: Pyright?
I have been using "type checked" python for a year and let me tell you, it is not even 10% as good as having a real compiler. Python type checkers are not even close to 100% accuracy.
If you're going to use a python type checker, use the one that has the most dev resources behind it. Which probably means, whichever company has the most internal python code, which is probably Google. So pytype.
But bear in mind, these things are only sensible when you've written so much python code that it's too late to rewrite in a better language. If you're in that situation, even though these checkers are far from perfect, they are a massive improvement over plain python.
In theory there's no difference between theory and practice, but in practice there is.
So, in practice which is the best current Python type checker?
As I mentioned in a comment to that post, I used pyright for a few minutes and was impressed. You get red-squigglies under unexpected-type & no-such method errors, and I saw green squiggly under a "value may not be set" in a function like:
def foo(x: int):
if x > 4:
y = 1
return y
The immediate visual feedback is great. The dependency on typescript is not.Searching VSCode, I see no plugins for mypy & PyType, and the pyre-vscode plugin requires pyre-check to be installed, and a .pyre_configuration file in the directory? All I know is I couldn't get it to do anything, but pyright worked out of the box with no effort.
I haven't used this PyType project, but it looks like it competes with mypy. I like that it, too, uses your setup.cfg file for configuration (I'm all about single-file configuration projects). Someone else will have to speak to the differences between them. I'm sure they have the same functionality, so the differences will likely be in the specifics like what the error messages look like and how easy they are to configure. If you contribute to various python projects, I supposed it wouldn't hurt to have them both installed.
As for which to choose, mypy is Guido's project (or at least the one he's associated with) and until another is clearly superior, you can be pretty certain it will track changes in the python language closely for a while. I use mypy & flake8 for "static checking".
My question is: are there any plugins that show error-indicator squigglies for pep8 violations?
Many projects need node, but not all; I don't know any which require docker.
I want to be clear: with pyright, it's not bad! I think it's the right way to do the LSP plugin: no dependency on the current python version (am I using the python in the venv? or ~/.local/bin? or /usr/bin/?) and great integration with the editor.
If it's a large project that will benefit from "5x faster" typechecking than mypy, then yeah, it's probably worth the dependencies.
If you have a 5-file python package I found on pypi that I want to contribute to, but there's nonstandard hoops to jump through to get `python setup.py test` working properly, I would have preferred you made a different call in this regard.
I'm glad we have choices and it's not mono-culture.
Or set flake8 as your linter if you'd prefer to have PEP violations displayed instead.
Actually it looks like I can get flake8 & pyright simultaneously; that's actually the ideal situation I was looking for.
Pytype started with larger goals: It focused on static analysis and type inference; much more so than any of the other Python type checkers today do.
PyType, like MyPy, is also capable of analyzing Python 2.7 code because existing codebases have a ton of that and understanding types can help when porting it to 3. A couple years from now will anyone care? We hope not!
Performance is a problem for dynamic language type analyzers. Particularly so for Python where CPython is slow yet analyzers want to be self hosted in the language they're written to analyze. Very interesting, though not wholly surprising, to see Pyre and Pyright choose to implement in other faster languages. MyPy also has MyPyC internally which is doing a very Cython-esque translation of some of their performance hot spots into CPython API C code for a speedup.
Interesting times.
I think a lot of it really comes down to how much domain knowledge you have with respect to the problem you're trying to solve. I tend to have to process a lot of data from unknown sources, with unknown and inconsistent encoding.
Most errors either show up immediately in Pycharm when I'm coding, within the first few moments of running the program, or two weeks later when it encounters a bizarre batch of records in the 15 millionth row of the database I'm processing or some strange network error that I neglected to foresee.
I personally don't see how compile time typechecking would make any difference to that latter set of errors (which are solely due to my lack of foresight on all the weird ways people find to handle data), and the first two sets of errors are usually resolved in the testing phase of initial program debugging.
MyPy itself _runs_ under Python 3.