12 ms·
Adoption of Mypy for Python type checking: 45% already use it, 40% don't plan to
- cosmic_quanta 6y agoFor my part, I'm waiting for a convenient way to combine mypy and numpy. Numpy 1.20 started using type annotations in the most trivial way, but it's a start.
- sonthonax 6y agoIt’s something I’m really keen to see. MyPyC can produce decent LLVM IR from basic python classes, to the point where accessing a builtin type attribute on an enum is just simple pointer without indirection. However that’s only one side of the python performance story, if people got into the habit of using Numpy arrays with MyPyC, python might actually be fast.
- smitty1e 6y agoIt's excellent that Python lets one light off a REPL and work on solving the problem, then annotate the answer later for all of those good reasons.
- djohnston 6y agoThat seems totally reasonable. Progressive type safety is one of the major wins of Python IMO. I don't need type annotations to write some codemod script, certainly do to write a robust web service.
- lambda_obrien 6y agoWhen my company upgrades to 3.8 or higher, I'll be happy to include as much typing as possible. I can't wait until I can refactor Python more easily, it's a huge pain right now.
- IshKebab 6y agoGood progress. I think you'd be mad to not use type hints, even for small scripts they're hugely advantageous. It's especially nice now that VSCode supports them well. 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.
- incrudible 6y ago> The only problems I have with them are that they are completely ignored by Python itself - you have to run a separate type checker. 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.
- IshKebab 6y agoI expect it to report type errors by default with an option to ignore them. Same as Typescript.
- pansa2 6y agoIt’s interesting how close together those percentages are. Python developers may well be split 50:50 or 60:40 on the issue of whether or not to use static typing. 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.
- toxik 6y agoI’m what I would say part of the old guard. Type annotations and checking was weird to me, but okay, then walrus assignment := came and I felt that’s maybe worth it (makes some loop idioms a lot cleaner!)... then the match thing, I’m really starting to feel like the shine has worn off. Python is a noncohesive mess design wise. Two Python developers today may well have such enormous differences of opinion on what constitutes proper Python, they could be talking about different languages
- pansa2 6y agoDavid Beazley agrees with your last paragraph in a recent tweet: “Python is at least three different programming languages at this point.” https://mobile.twitter.com/dabeaz/status/1373296229711376387 https://mobile.twitter.com/dabeaz/status/1373296229711376387
- geofft 6y agoIn the old days we called that "multiparadigm" and said it was a good thing.
- danjac 6y agoTBH once you start using a fair number of dependencies (say, your average Flask or Django app) you end up with so many workarounds and flags like ignore_missing_imports that the benefit of mypy is marginal compared with other tools like flake and unit tests. I tend to use it more with libraries and less with applications.
- junon 6y agoNot to mention the performance. Perhaps it's improved since, but just a year ago it took an upwards of 10-20 seconds to type check a trivial repository.
- robbyt 6y agoCompared to the 10-20 hours I could spend debugging some stupid problem with passing objects to a function in the wrong order...
- incrudible 6y agoI've had a similar problems, pyright scales much better in my experience.
- emptysea 6y agoIn my experience it’s been worth the trouble of writing partial type stubs for your project’s dependencies. 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.
- redsymbol 6y agoImportant to realize glyph's twitter followers are not representative of all Python users. 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.
- croh 6y agoYup agree. People who follow him are more "into" python async stuff.
- watermelon0 6y agoI'm just using it to get my job done, and really couldn't imagine working with Python without it. 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.
- you_are_naive 6y agoThe biggest problem with mypy is that it's written in python. It's slow. It needs to be rewritten in rust or go to make it faster like how js ecosystem is moving in that direction. All the tooling is moved to faster compiled languages.
- crazypython 6y agomypy has implementations in other languages – PyCharm's is in Java, Facebook's Pyre's is in OCaML https://github.com/facebook/pyre-check https://github.com/facebook/pyre-check
- MapleWalnut 6y ago
- CraigJPerry 6y agoThis seems very positive to me - specifically that a sizeable population recognise they really don't need it. 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.
- skohan 6y agoI find it hard to imagine cases where gradual typing would have any advantages over a language with decent, modern type inference. In the latter case, you're still not typing type specifiers very often, but you get unambiguous function signatures, and can eliminate whole classes of runtime errors.
- pansa2 6y agoWhat might “Python, but with modern type inference” look like? Are there other existing languages that come close to matching that description?
- ipaddr 6y agoPossible referring to php and the auto type casting? He couldn't possible mean javascript or rust. Perhaps golang because of the limited subset of types.
- skohan 6y agoWhen I read the twitter replies, it doesn't make me optimistic for the future of typed python. I don't work with Python often, so I don't know what the "truth on the ground" is, but it seems like having a language divided between several different type-checking implementations is going to be a nightmare for things like interoperability of frameworks. 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.
- deleted 6y ago[deleted]
- philwelch 6y agoPython 3 type annotations are part of the language spec. Whether you use mypy or a different library for type checking shouldn’t necessarily make a difference to the code.
- ali_m 6y agoType annotations may be part of the language spec, but the way they are enforced is not. Different type checking libraries have made different design choices (leniency vs strictness, type inference vs gradual typing etc.). Although these differences don't matter at runtime, they do mean that code which passes one particular type checker might fail with another.
- davnicwil 6y agoIs the solution here just to skip type checking the code of dependencies, and just check the types at the interface with your own code using your tool of choice? Or is it more complex than that?
- jraph 6y agoI think so. When typing checking a project, its dependencies don't get type checked as well as far as I know. Type errors in dependencies are kind of not relevant for you, and many dependencies are not type-annotated anyway.
- stephenroller 6y agoI run a fairly large open source project (https://github.com/facebookresearch/ParlAI/ https://github.com/facebookresearch/ParlAI/) and we use mypy. Our experience has been that it can be quite difficult to placate, so we usually treat it only as a warning. However, having our code annotated with types in many places has significantly improved developer productivity, just from having less ambiguity with what you're dealing with.
- hsaliak 6y agoThe survey may not represent sentiment toward type checking in Python. Mypy is only one of the checkers out there. Out of the 40% who don’t use mypy, many could be invested into other type checkers such as Pylance. This is certainly what many comments on the Twitter thread are about. So the % who refuse to use type checking is likely lower than 40%
- bravura 6y agoI love mypy and use it in my job. 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?
- ali_m 6y agoVariadic generics should make this possible in future: https://www.python.org/dev/peps/pep-0646/ https://www.python.org/dev/peps/pep-0646/
- galimaufry 6y agoI was just thinking about this. Seems like it could also be great if the type system could make a distinction between vectors and dual vectors, alleviating "you got the wrong axis" errors. But maybe for something like that you really need vector space objects like in Sage.
- super_mario 6y agoI personally hate typing in Python. Python now feels like Typescript, and when using pyright, you actually get something that looks very much like type file for JS when you step into library code, instead of library source code itself. 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.
- dehrmann 6y agoTypescript at least has the excuse that you have to run JS, so the approach is more sensible. With types in Python, for the most part, you could choose a different language.
- ExtraE 6y agoFrom a twitter reply: > Surely there’s a real statically typed language with similar or better ergonomics? Please, if someone here knows of one, let me know!
- hannofcart 6y agoThere are people in both Twitter and HN comments saying "if you want types, just use a typesafe language". 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 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.
- 127 6y agoI found myself writing this kind of code recently: https://pastebin.com/fpQzRypp https://pastebin.com/fpQzRypp 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.
- hannofcart 6y agoIndeed, you'll find that type annotations and enforcing them with a type checker will eliminate all those `assert`s for `not None`.
- zo1 6y agoIt won't eliminate it, but... At the very least it'll "push it back" quite close to the point in your code where someone made a not-so-well-thought out decision to accept nullability into a variable and let it propagate that uncertainty into the rest of the system.
- zo1 6y agoAs an anecdote: I've encountered, in production, a large order of magnitude more "null reference" errors in statically compiled languages than I have in python. But, people that propagate inconsistent state into their application exist on both sides, and having a statically-typed language doesn't necessarily shield you from the consequences.
- BurningFrog 6y agoHere is my thought on mypy: 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?
- webmaven 6y ago> 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? 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.
- ThePhysicist 6y agoI gave a presentation on type annotations at the EuroPython 2017 where I also investigated empirically how many open-source Python projects really use type checking, I present the results at the end (33 minutes in): https://www.youtube.com/watch?v=vM2Zoy4Sxhk&t=2181s 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 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.
- heavyset_go 6y agoI just use the typing module from the standard library, and only use Mypy via an LSP client in my editor.
- bmitc 6y agoI’ve never seen anyone use Mypy.