114 points · by pansa2 · 5y ago · 130 comments
In general, people who follow him are going to be more "into" Python than someone who just uses it to get their job done, and thus more likely to use optional tooling with a large overhead like mypy.
That will skew the results in favor of mypy in this poll. Though the poll getting on the HN front page will balance that somewhat.
I'd consider static type checker as optional if you are playing around with small or throwaway scripts, but for any half serious project, it should be a requirement.
IME I don't consider it slow, and it's definitely not visibly slower than pylint, flake8, or similar tools for other languages.
Dropbox, which has one of the biggest Python codebases, uses it, and most mypy maintainers including Guido work there.
Guido hasn't worked at Dropbox since 2019. He retired for a bit, but is now in the Developer Division at Microsoft.
I see a lot of sort of 'programming culture' for given languages, practices, even discussion of working conditions and etc that on social media are taken as standard operating procedure. Highly active and even highly knowledgeable folks seem to portray various practices and etc are a given ... that I suspect are actually outliers.
What I would really be interested in, is do library developers use or intend to use mypy? Because if your libs are typed checked and your APIs are well constructed, a lot of that safety will flow to the clients.
Maybe those 40% would love to use it if something was changed or fixed. I think these numbers are frankly amazing, a year from now we reach an inflection point where mypy is used by the majority of some segment of the Python developer ecosystem.
I'm imagining a future 5 years down the road where some poor python developer has to use some "polyfill" solution because they want to import a library which was developed with a type-checking solution which is no longer main-stream, or because the type-checkin solution they're using has breaking changes with respect to previous versions.
They're like comments, they're only there for developer convenience and can be completely ignored.
I left python 3 or 4 years ago and not planning to go back (basically the company I was working for mandated they were not going to ever upgrade to python 3 because of the effort required and thus stopped all migration related projects).
The language is nice, and frameworks such as Django are great, but the mess the ecosystem has become is not something I miss.
* Python 2 is dead.
* pip is the most popular in the industry, conda serves a different purpose if your focus is mainly on scientific computing
* what does async vs blocking even mean in this context? all languages have the same problem, just use the libraries that are non-blocking.
* mypy is the static type checker that is the most popular (28.3k repositories, 1.9k packages), if you are using other type checkers then congrats, you will just be the CoffeeScript/Reason user of the JS world, refusing to believe that TypeScript won the mindshare
Not really. TypeScript is a JS superset (and TypeScript tooling works with unadorned JS, too), but Coffee script/Reason are incompatible languages that happen to compile to JS and have static typing. All of the Python typecheckers are more like TypeScript, except that even the type annotations and much of the type system is also standardized as part of the core language. You can literally use any of them on the same code base, and get largely the same results (some will catch some type errors that others miss, some will flag some correct code as possibly erroneous that others would leave alone, but the differences tend to be minor.)
Otoh, development of all the major type checkers is fairly collaborative. Mypy usually gets features first, but not always.
Maybe Node.js? Although it does have some sync function to do io, most of them are still unblocking, and the ecosystem is unblocking by default too. It is harder to find a blocking library than finding a non-blocking ones.
Officially yes, but in terms of actual use? Not really. Plenty of legacy codebases are still around that don't have the resources to upgrade.
Take `go fmt` or `cargo fmt`. black is the de facto autoformatter, but it's still an optional dependency. Maybe like virtualenv it'll eventually be integrated into the standard library/distro. That would help. With other stuff, the split you describe could be a real problem, although for the majority of Python users, using mypy and poetry is fine.
> basically the company I was working for mandated they were not going to ever upgrade to python 3 because of the effort required and thus stopped all migration related projects
If you're actually serious, this is intriguing to me. Business-wise, I have never seen the math shake out this way. When we've done sizings/estimated developer hours and ROI, it's always been more economic to slowly migrate to Python 3 vs a rewrite, even if the dev team is also already using and well-versed in another language. If you're able to provide more details, please do. It's so rare to have a high-quality discussion about this.
Migrating several hundred thousand lines of code bases with few people and many dependencies (some of them not maintained, not ported to 3) are not trivial.
These will stay for years still. Actually it is very sad, that significant new tools are basically useless on 2.x (e.g. vscode: lots of waiting on completion, or jumping to definition - if it works at all).
This isn't an issue in 2021. Python 2 reached its EOL over a year ago, and those who own old Python codebases have had known for literally almost 13 years now that Python 2 would be EOL'd when Python 3 came out in 2008.
Used correctly - i.e. you’re leaning on structural typing, so you invest your time in Protocols that go “quack” rather than hard-to-test, brittle, nominatively typed Ducks who quack - which incidentally is exactly the path the mypy getting started docs seems to focus on, why?! - that’s just setting people up for failure, i digress.
Mypy, pyright etc are fantastic tools in the toolbox, sure to save time but Cargo culting is a pain to deal with. See for example 2 decades of empty getters / setters in Java “in case we ever want to add some behaviour in future”.
Gradual typing of the structural variety is fantastic, you’ll know if you’re doing it right because you’ll spend surprisingly little time thinking about types, you won’t spend any time wrestling with mypy to convince it of your idea and when it does complain your reaction will be a legitimate “aha, yeah ok fair shout, you got me” not “oh ffs!” :-)
One really cool thing about gradual typing - if you get it wrong and i have to take over your code, i can at least turn it off. One of the reasons i love tests so much - I don’t have to accept yours if they’re a bear to maintain, i can easily bring along my own instead.
For data engineering, I haven't had a chance to try this due to lack of language options, but I strongly suspect that structural typing would be much more useful than nominal typing.
Code in dynamic languages typically takes advantage of the flexibility to do meta-programming, which can significantly both the amount of code required to complete a task and its complexity.
A lot of modern typed languages have got pretty decent meta programming: rust procedural macros, nim, D, crystal, Zig... Where's the equivalent of serde in python? It's mostly runtime type driven deserialization that is orders of magnitudes slower.
Type inference solves a different problem - it doesn’t help you here. For example
type Money {
amount: Amount,
currency: Currency
}
Money(USD, 10) + Money(EUR, 10) # what type should be inferred here?
A runtime exception maybe?(There’s no “best” answer here, a runtime exception might be the pragmatic choice but i like the Map<Currency, Amount> solution to this).
See, for example, the absl::Duration apis in c++.
https://en.wikipedia.org/wiki/Type_inference#Types_in_progra...
He couldn't possible mean javascript or rust. Perhaps golang because of the limited subset of types.
I've found that there is no one best typing discipline. Different approaches work better for different problem domains. And I'm currently using Python in so many different problem domains that I find it convenient that Python now allows me to choose the best typing discipline for the job.
It's true that this means that Python isn't the best at any particular way of doing typing. But that also seems appropriate. Being a jack of all trades and master of none is sort of Python's thing nowadays.
And by partial I mean only typing the stuff you end up using, so if you just use one function from some huge, untyped package, just type that one part for now.
The way I see it, you write the stub once, and then the API has less ambiguity and you get type checking.
The only problems I have with them are that they are completely ignored by Python itself - you have to run a separate type checker. That's a fairly stone-aged system. And also I with VSCode had something like Typescript's "no implicit any" so it was more obvious where you had to put types that couldn't be inferred.
What do you expect the Python interpreter to do, exactly? Type annotations are not supposed to affect execution.
In my experience, you may want different type checkers with different configurations for different projects.
Honestly, I don't like the direction Python is taking lately. It used to be easy, approachable, dynamically typed, strongly typed language. Now, syntax is getting polluted and there are definitely more than 2 ways of doing things.
For the language that’s famously supposed to have “one obvious way to do it”, that’s a significant difference of opinion on something so fundamental.
Two Python developers today may well have such enormous differences of opinion on what constitutes proper Python, they could be talking about different languages
“Python is at least three different programming languages at this point.”
https://mobile.twitter.com/dabeaz/status/1373296229711376387
So the % who refuse to use type checking is likely lower than 40%
I would love to use it for personal pytorch projects, but only if I could type-check that tensors are the correct shape, etc. Is that possible?
What I found though, is that 'typed' Python is a great language/dialect in its own right.
There's a confluence of simplicity and ergonomics that I find quite appealing, even as compared to mainstream static typed languages.
I had previously submitted to HN with my thoughts on this:
https://news.ycombinator.com/item?id=25520411
I think the primary difficulty I have seen people face is getting a buy-in from all members of the team. One or two members in every team seem to prefer to write Python the "way it was originally intended" and consider type annotations more of a nuisance. And this ends up hurting adoption.
Like OO in Python, it feels bolted on. The syntax for generics is bad, at least compared to Java. Typing also exposes how poorly thought out Python's collections are. The hierarchy and methods make sense, but some things are in typing, others in collections.abc. It also falls over when using things like functools.partial.
Then there's the frequent complaint that if you're going to go to the effort to use something typed, you should use something that can take advantage of those types during compilation to optimize the code.
Sometimes code is being plugged between two highly unstable things. It just needs to work and can't trust that any of the data it receives is valid or good.
I should really be using types here.
1. If you have a good test suite, mypy adds less value than it costs.
2. If you don't have a good test suite, you really need to get one.
How am I wrong?
> 2. If you don't have a good test suite, you really need to get one.
> How am I wrong?
You're not wrong, per-se, but there is an excluded-middle fallacy here:
If you have a good test suite (that already includes tests for the types of returned values), then mypy may indeed add less value than it costs.
If you don't have a good test suite, you do indeed need to get one.
However.
Mypy is very likely to reduce the cost of adding a good test suite, as it obviates the need for making type assertions in the tests by instead allowing those assertions of various types to be made inline with the code as type annotations.
This makes the remaining functional and unit tests shorter and a lot more readable.
> Surely there’s a real statically typed language with similar or better ergonomics?
Please, if someone here knows of one, let me know!
https://www.youtube.com/watch?v=vM2Zoy4Sxhk&t=2181s
My conclusion was that only a small fraction of projects really used them, there were a lot of projects that had type checks in their code but only in "homeopathic" doses. I started using them for some of my Python projects as well (e.g https://github.com/algoneer/algoneer) and while I find them useful I think they're not as useful as a "real" type system in a fully typed language like Golang. Still, they're very useful for discovering simple mistakes that would only show up in unit testing otherwise.
You can also "misuse" them for other purposes, at the end of the presentation I e.g. show how you can implement software contracts with them. Of course this would wreck a type checker like mypy, so don't do that in your codebase. That's probably also one of my critiques as the annotation syntax can in principle be used for anything, but mypy and other tools are not able to deal with code that does that.