9 ms·
PEP 0484 – Type Hints is accepted
- ilevkivskyi 11y agoSee also acceptance letter https://mail.python.org/pipermail/python-dev/2015-May/140104.html https://mail.python.org/pipermail/python-dev/2015-May/140104... and the discussion on Reddit http://redd.it/36yuk8 http://redd.it/36yuk8
- wcdolphin 11y agoThis seems exciting. I'm not familiar with the availability of new Python features. When can I use this in my day to day Python?
- pas 11y agohttps://github.com/JukkaL/mypy https://github.com/JukkaL/mypy is the/one implementation of a static checker that conforms to PEP 484. (Naturally, as far as I know, JukkaL with mypy almost singlehandedly inspired the PEP.)
- IshKebab 11y agoI guess this will be in Python 3, so probably never! :P
- Nemcue 11y agoYou can use it today if you want to, there is already language support for adding your own type notation. And if you don't want to create your own you could use https://pypi.python.org/pypi/obiwan https://pypi.python.org/pypi/obiwan
- tveita 11y agoSimple function annotations like def foo(x: int) -> str: ... have been available since 3.0, and editors like PyCharm already use them for typechecking. For new PEP 0484 features like generics, MyPy supports Python 3.2 and up, but PyCharm support seems to be work in progress: https://youtrack.jetbrains.com/issue/PY-15206 https://youtrack.jetbrains.com/issue/PY-15206
- TazeTSchnitzel 11y agoSo now two popular dynamic languages have type hinting of a kind: Python (unenforced annotations) and PHP (declarations). Great!
- gaius 11y agoI encourage all use of the type system to ensure program correctness, but once you've had Hindley-Milner inference, it's hard to muddle along with something like this.
- TazeTSchnitzel 11y agoIt really is. Since using Haskell, the type systems in the mainstream languages I've used have seemed sorely lacking.
- Dewie3 11y agoThough make the type system sufficiently more expressive than Haskell's and you have to give up on type inference again... Though maybe you could restrict how expressive the type system is on something like a module (Haskell module) level, so that you can have type inference if you give up on some power? I don't know.
- Kutta 11y agoType inference doesn't somehow fail if we go beyond Haskell's type system. The inferable subset of the language remains inferable. Haskell already has inferable (Hindley-Milner) and uninferable (higher-rank polymorphism, GADTs) parts. If we go beyond Haskell's type system, it actually becomes crucial for productivity to have powerful type (and term) inference. Agda and Coq both have much more powerful inference than Haskell; it's just that don't bother with using it to realize Hindley-Milner-style annotation-free bindings, because there's little use for it in proof assistants.
- gamegoblin 11y agoHaving used Haskell a lot, I still feel pretty fine about C++ or C# type systems. It's Java's that drives me absolutely insane. Type erasure makes me want to pull my hair out.
- bkeroack 11y agoOne of the reasons why I use Python 3.x exclusively is for the new function annotations, which allow use of this: https://github.com/prechelt/typecheck-decorator https://github.com/prechelt/typecheck-decorator This allows me to write Python in a design-by-contract style with full, rich runtime type checks. My bug count has gone down dramatically, and when I do find a bug it's virtually always via a typecheck exception.
- reeboo 11y agoYeah, isn't that amazing? I really think this "type" idea is a good one. I wonder if other languages have thought about following Python's lead and adding a type system.
- rattray 11y agoWould you like to reconsider your tone?
- reeboo 11y agoNo, but let me clarify. Bolting on a form of type checking to a weakly typed language seems orthogonal to the nature of a language like Python. If you find yourself saying "I wish I had a type system in this code base, its getting pretty big and a compiler that does type checking at compile time sure would make my life easier" maybe you should think about using a more appropriate tool. I use Python a lot and it has its uses, but adding this type hint stuff just seems like an enabler for causing myself pain and denial when I should be using a more strongly typed language.
- swhipple 11y agoStatic typing is considered unpythonic, but the typecheck library is closer to a sanity-checking model (like using assert). The advantage is that instead of the checks being in the body, they can be moved to the function header. It supports things such as checking ranges or regex matches, rather than just object types. Even if people are only using it as a shim for what could be static typechecking, I think there's something to say about the practicality of it existing, if only for the reason that their ecosystem might already be Python-based.
- commentard 11y agoI got fed up when python told me I could type more spaces until it was compliant with the whitespace dictatorship. C was much more friendly. And faster too!
- Eleutheria 11y agoSo python becomes nim now? I wish they kept separate ways. Python for simple and powerful hobbyist programs and nim for the enterprise. If we have two languages doing the same thing, we will abandon one group of enthusiasts. Then another language will come to fill the gap.
- Dewie3 11y agoNim the language[1] will probably still win with regards to mechanical sympathy. And perhaps also metaprogramming? [1] As opposed to implementations... because I don't know what kind of crazy compiler (JIT or AOT) they do nowadays to make "slow" languages fast. So for all I know Python can still be fast, given enough manpower.
- Shizka 11y agoI see your point in hobbyist vs. enterprise, but Python is already used in multiple big corporations and I don't think I have seen any result of it being a problem. If anything, the language gets more and more attention due to its spreading popularity on all levels. Isn't that just for the good of the language? As I mentioned it is already used in several big corporations. From your point of view - is it a problem right now?
- monkmartinez 11y agoPretty sure Python is very "enterprise"[1] worthy. [1] - https://www.paypal-engineering.com/2014/12/10/10-myths-of-enterprise-python/ https://www.paypal-engineering.com/2014/12/10/10-myths-of-en...
- dikaiosune 11y agoI remain on the fence myself, but that article mostly argues that it is useable and used in the enterprise, not that it's worthy of use at that project size and scale. I think that article, rather than being a strong technical piece, is a pretty good example of: http://en.wikipedia.org/wiki/Argumentum_ad_populum http://en.wikipedia.org/wiki/Argumentum_ad_populum Also, it doesn't take much google-fu to find a counterargument from someone apparently reputable: http://www.quora.com/Why-does-Google-prefer-the-Java-stack-for-its-products-instead-of-Python/answer/Robert-Love-1 http://www.quora.com/Why-does-Google-prefer-the-Java-stack-f...
- hyperpape 11y agoInteresting to me that they write "Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention." Having no experience with using gradual typing, I wonder if there's any stable equilibrium here between "thoroughly dynamic" and "type declarations everywhere". In any case, as someone who started with Python and started preferring static types later on, I really hope this gets used.
- dimino 11y agoThis is a tragedy, as it forces comments to contain syntactic value.
- deckiedan 11y agoNot quite. The PEP is (to me) quite explicit that the typing is never mandatory. So a comment-hint will never change the way the code actually runs, nor will lack of a comment or an unintentional comment cause a program to act strangely. Already, anyone seriously using pylint or other "normal" python linting tools uses comments like that to give extra hints to the linter: def blah(): # pylint: ignoresomething type of thing. Yes, it's a bit weird, and yes, it does teeter right on the edge of the comments as syntactic value precipice, but I think lands just on the alright side.
- ta0967 11y agothe `type: x` comments substitute explicit syntax in places where i'd want type inference, so it's no big deal.
- rectangletangle 11y agoI like it, let the developer choose where typing is most appropriate. Also runtime type assertions really help with identifying bugs, especially NoneType issues.
- brikis98 11y agoNice. It really seems like static typing and functional programming are winning [1]. For the most part, I think that's very a good thing, and may help move the software industry forward. [1] http://www.ybrikman.com/writing/2015/02/06/are-static-typing-and-functional/ http://www.ybrikman.com/writing/2015/02/06/are-static-typing...
- CrLf 11y agoI mostly like Python for its clean syntax but the examples contained in this PEP turn unreadable really fast. I don't look forward to this and I don't really see the point of it. I can see how it can be useful in theory but, in practice, I just see a symptom of language bloat. The way I see it, if you find yourself in the situation where explicit type checking is important, there's probably something wrong with your process or team. Either your are not testing properly, or your team's mindset is more adjusted to another language. The danger I see with this becoming widespread is Python becoming somewhat like C++ where it's very easy to come across code one can't easily read because it uses a different subset of features than you're used to. This sort of "language accents" so common in human languages is something that should be avoided in computer languages, IMHO.
- jghn 11y agoOne could argue that you'd need many fewer lines of test code if your compiler can prove a lot of stuff is correct at compile time. It's a personal preference thing but I prefer this situation and don't view it as having poor testing
- prodigal_erik 11y agoThis. Every test just explores one special case of the invariant I would prefer to enforce but cannot with today's tooling.
- coldnebo 11y agoIt seems more like Scala syntax, which is much cleaner than C++ IMHO. But with Java becoming dynamic (and C++ techniques for same), is the future a smear of shared language features, like Bladerunner's cityspeak, or are these just intermediate forms as a new protolanguage capable of unifying these features comes into existence?
- jghn 11y agoI suspect the latter. The static typing crowd is moving towards type inference - you mentioned the obvious examples already. Now we're seeing dynamic languages evolving towards static typing with inference. I think that most people if they really think deeply about it would say that all things being equal that static typing is a good thing. The problem is that all things are not equal and that the cost of extra syntax, boilerplate, etc can completely outweigh the benefits of of that static typing (as the GP post clearly believes). I was a huge dynamic language fan until I started using Scala full time and since then I've gone completely the other direction to being a static fan. I suspect as more people start finding their way to tools which provide static typing guarantees without the syntax getting in their way so much that we'll find a nice middle ground.