5 ms·
Anyone considering using type hints should listen to this talk: https://www.infoq.com/presentations/dynamic-static-typing/ https://www.infoq.com/presentations/d
by globular-toast 6y ago
Anyone considering using type hints should listen to this talk: https://www.infoq.com/presentations/dynamic-static-typing/ https://www.infoq.com/presentations/dynamic-static-typing/
I personally think it's mostly a waste of time to do type hinting for the purpose of catching errors in a strongly typed language like Python. Type errors just aren't practically a problem in dynamic languages. Doing types for performance is a great reason to do it, though.
- mumblemumble 6y agoI agree with you that the benefits of static typing are sometimes greatly exaggerated. But I also think that using type hints and type checkers in Python isn't quite the same thing as static typing. For starters, type checking can't actually guarantee you won't have type errors at run time. Because, unlike in a statically typed language, in Python, anyone can always choose to just not use type hinting. Whenever that happens, as far as the type checker is concerned, anything goes. But type hints are very useful as hints. They help with editor tooling, which can make it easier to navigate an unfamiliar codebase. They provide extra information that makes the code easier to read. And they give me an opt-in form of type linting that allows me to set up regions of code where I don't have to take quite so much personal responsibility for ensuring that arguments are compatible with parameters. In short, it's not a correctness prover; it's an energy saver.
- flowerlad 6y ago> I agree with you that the benefits of static typing are sometimes greatly exaggerated. For large, multi-developer projects, it is impossible to exaggerate the benefits of static typing.
- deleted 6y ago[deleted]
- nickm12 6y agoI don't like the term "type hints" because typecheckers like mypy are genuinely proving type correctness. Yes, if you don't annotate part of your program then you can get a type error in that part of the program, likewise, if you pass a value of the wrong type, you can get a type error. But if you are using a typechecker, you are guaranteed that if your inputs are well-typed, then you won't have a type error, which is a stronger guarantee than "hints" implies.
- mumblemumble 6y agoI like the term "type hint" because that really is exactly what they are. Typecheckers like mypy are (1) optional, and (2) are only as good as the type hints themselves. Unlike with a language that's actually statically typed, the relation of the type annotations to the actual types of the data operates largely on the honor system, so the type checking is more a validation of internal consistency than full-on static type safety. Which is fine, and well within Python's "We're all adults here" ethos. It's just that it's still not anywhere near the level of rigor you get out of the type checker in a language like OCaml.
- nickm12 6y agoIt's really not the honor system. If you verify with a type checker, you are proving that various type errors are impossible, provided that the caller respects the type system. The rigor is exactly the same, it's just that the type system allows for "gradual typing" so that it doesn't need to be strict as in a fully-typed language.
- nickm12 6y agoI've been on teams at multiple companies that have used type annotations for Python. Developers have generally loved adopting type annotations because it (1) makes the code easier to understand (2) makes the code easier to refactor (3) improves integration with IDEs and (4) catches real bugs. I disagree that type errors "aren't practically a problem". NoneType errors and AttributeErrors turn up all the time in Python.
- globular-toast 6y agoClearly didn't watch the talk then. He found that about 1-2% of errors in real world projects were type-related errors.
- joshuamorton 6y agoFwiw this absolutely doesn't match my experience with pytype, both on my own work and, as I understand it, "at scale".
- globular-toast 6y agoAh yes... good old anecdotal evidence.
- joshuamorton 6y agoThe org I'm in regularly publishes peer reviewed papers on engineering productivity (including on this particular subject!). It's far less anecdotal than a random talk by a random dude.
- globular-toast 6y agoCould you kindly link to one of those papers?
- joshuamorton 6y ago
- bobbylarrybobby 6y agoIt's not just about type errors. (Good) autocomplete and documentation are only available when types are either explicitly labeled or correctly inferred. It's important that I be able to 1. type in just the first few letters of a method name and press <tab> to complete it, and 2. hover over a method's name and get docs for the method. Both of these are only reliable if the type of the caller is known.
- globular-toast 6y agoBut we're commenting on a type checker which implies this is being done to catch errors.
- mvzvm 6y agoIt's a problem the moment it has to interact with the outside world, especially DBs/serialization/... really any kind of I/O.
- globular-toast 6y agoSo Python can't do IO? Django doesn't exist? What are you talking about exactly?
- mvzvm 6y agoI'm talking about type safety. Types matter, a lot, and python is a PITA to use without a system like pyre.