47 points · by triyambakam · 2y ago · 78 comments
I just use a combination of black, flake, and mypy. Apparently ruff is quite good as well. For lsp I use Jedi, which actually worked faster on my company's code base than pyright, a year or so ago.
[1] https://github.com/microsoft/pyright/pull/7177#issuecomment-...
(In case you are wondering, I do not have the time to maintain an independent fork and I am not sure how Microsoft would react to one, now that I have heard Eric's concerns.)
"Embrace + extend + embed" makes more money than "embrace + extend + extinguish".
As long as Microsft controls the data (your vscode actions), they have no need to explicitly destroy their competitors. The data skew gives them a natural advantage. I don't trust their intentions, but I do trust the well-aligned monetary incentives.
Microsoft definitely seems to be pushing proprietary VSCode extensions where it's free to get users hooked on the product (and kill competitors) and then charge companies lots of money for the tools they got everyone hooked on because they appeared to be free.
1. Make a really great type checker.
2. Make it difficult to use in IDEs other than VSCode.
3. Everyone uses VSCode.
I don't care though. Pyright is fantastic and giving it away for free is amazing even if it is part of a plan to push people to VSCode.
So going forward my personal projects continue to be ducktyped, and if I care about types, I use rust or go.
I have ported a codebase from Python 2 to 3 by annotating it with comment-syntax annotations and it helps a ton to keep your strings and bytes separate. Basically, you annotate with the binary_type, text_type types the six package and typecheck your code on both Python 2 and Python 3.
Basically, the steps involved are: 1. set up static analysis (linting and typechecking) for python 2 and python 3 2. get code passing static analysis on Py2 and Py3 3. get code passing tests on Py2 and Py3 4. get code running in production on Py3 5. stop checking and testing on Py2
It's not trivial and it requires some strong Python experience to write code that correctly "straddles" Py2 and Py3. But the advantage of the static checkers is you can slowly dial up their strictness and once they are dialed in, you minimize new regressions (new untyped code, new code that fails typechecking).
Really it needed a huge amount of reorg and refactoring, which a test suite and pyflakes handled quite well.
Type-checking in general is good. However, Python's type-checking mechanisms are bad. The syntax for adding types is very obviously bolted on, they miss basic type errors all the time, and third-party libraries often miss type annotations anyway.
Python is fundamentally a duck-typed language, and that's how you should use it. If type-checking is important to you, use a language that is designed for it.
If there is anything complicated, you can either make a quick generic type (TypeVar) or even just throw in an Any type! I had to do that recently with a function that handled different ROS services, which apparently do not share any base class.
Static typing makes Python tolerable.
Perhaps you haven't used static typing so aren't familiar with the kinds of bugs it can prevent?
I'm moving away from Python for my large projects...
I tend to run mypy every now and then ad-hoc to check things and it sometimes finds things that need correcting but I wouldn't say it's as worth setting up as the former.
an in my pyproject I set the strict mode for mypy, and almost all rules for rust with extend-select
just be aware a lot of community packages aren't ready for typed python. if you stick to the good stuff like fastapi and pydantic, you will be fine.
But after coming back to it in 2024, I found it quite lacking in multiple areas compared to LSP based type checkers. I work on a sizable Python codebase and opening it in Pycharm everything looks great at first glance. Then you open it in an LSP editor and you see warnings and type errors everywhere that Pycharm never caught and everyone using Pycharm never sees.
So, in my opinion, yes, much better than Pycharm, unless you have an affinity for Jetbrains products, LSP type checkers are much better. Again in my opinion, others may disagree.
But pycharm had a lot going on to make up for it.
Just as a reference I use pycharm mainly but before sending a PR I will rely on vscode to fix typing issues.
Just use what you likes best.
Playing around with Ruff nowadays.
mypy was proposed early during aiosmtpd development but we decided it's not worth the headache.
Having the tool get started with Ronacher, of Flask fame, gives it some mindshare among those paying attention.
It would be better still if the PSF were endorsing, but that gets too political, I suppose.
Ruff is great, though. I now stop using flake8+black+isort and have gone all-in with Ruff.
I'm considering switching to pyright, probably by rewriting code that uses this magic.
Or running both in ci with different configurations.
If it's new code, Pyright hands down.
The most recent one I've tried was Pyright. Pyright is faster than Mypy, and can also be configured to be quite strict, both big plus points for me. Unfortunately, in practice it ended up being far too strict in weird ways, and therefore difficult to work around with libraries that don't share its idiosyncratic assumptions.
For example, in Typescript there's an unknown type, which you can't do anything with unless you check its runtime type first. It's a clever way to add a true dynamic type to the type system without losing type safety. In Pyright there's a similar Unknown type, but in strict mode the type checker simply throws an error if anything is ever unknown. For me, this turned out to be particularly painful when using library types that involved generic state. Because I wasn't using that state, I didn't set a type for it, and it defaulted to Unknown. But that meant that an object had Unknown properties (that I was never using) which meant the type checker kept on failing. Moreover, because Python's generics story is pretty miserable, I couldn't figure out how to set the type to something not Unknown. And I couldn't figure out how to disable the error because Pyright's documentation is so poor. I genuinely spent a day of work on this problem, then disabled type checking completely because that was the easiest option.
I haven't used Mypy for over a year at this point, so things may well have changed, but my memory was that where Pyright is overly pedantic, Mypy was overly lax. It was also a lot slower, and had similarly poor documentation. The main reason I switched was that I was still regularly running into runtime type errors - the very errors that the type system was meant to help me avoid.
In both cases, I found the biggest problem was that neither tool seemed to go as far as Typescript in terms of modelling real-world Python code. What I mean by that is that idiomatic Typescript usually looks a lot like idiomatic Javascript in terms of the patterns and features used, because the Typescript team have put a lot of effort in to ensure that Typescript types represent how Javascript is actually used.
On the other hand idiomatic typed Python typically seems to look very different from idiomatic untyped Python, in large part because with types, you end up limited to about 1/4 of the surface area of the language. (This is why a lot of major libraries or tools come with type checker plugins because their library's behaviour cannot be described purely in terms of types.) Typed Python then ends up looking like a weird Java variant.
If I were going to add a type checker to my Python code again, I'd probably go for Mypy because it seems more like the standard and had marginally better documentation. But right now, I have the Pylance plugin installed in my IDE and that's about it - there's no type checking in CI, I just hope that I catch all the obvious cases with good enough testing. Thankfully the Python side of my work is pretty minimal, a few hundred lines of code at most. If I had a bigger Python project I might revisit this situation, but I'd be more tempted to just avoid Python for bigger projects.
1) i'm primarily interested in data types, rather than objects, as i believe most runtime errors are the result of unexpected changes to data types.
2) i'm suggesting a type system that does away with hand annotations and rather builds them on the fly by keeping assignment history and using that to determine when statistically anomalous types appear.
But, Pycharm's still quite limited compared to pyright or even mypy. So I find myself A) doing an extra linting pass with pyright or mypy; and/or B) adding redundant annotations/overloads to help pycharm figure out what's going on.
My only issue is I occasionally need to restart Pyright when it fizzes out.
So mypy for me.
It serves my need, which is to ensure I haven't done mistakes in specifying parameters to functions.
I have no desire making Python to be statically-typed.
For which codebase size?
I enjoy typeguard for dynamic checking using the @typechecked decorator. I haven’t tried beartype, but I know lucidrains uses it.