Re:Type hints cheat sheet (Python 3)
by IgorPartola on Wednesday December 12, 2018 @12:11AM
(#18661549)
So maybe this is because of the type of projects I work on, or maybe my use of the language, but type errors are really rare in my code and show up almost instantly. The main source of them is data from external APIs where a None was returned where an object was expected. Second biggest source is when parsing XML and a None results from some node not having the expected children. I feel like type checking would give me almost no benefit aside from the very rare instance where I might make a quick code correction without testing it and have it blow up because I mixed up the order of parameters. What am I missing?
[ Parent
| on HN ]
Re:Type hints cheat sheet (Python 3)
by noobiemcfoob on Wednesday December 12, 2018 @12:19AM
(#18661577)
I can't answer what you're missing as I'm missing it too. Type annotations have seemed to me as useful documentation. If they're strictly enforced, it might make it easier for a new coder to interface with unfamiliar code...but if it was designed well in the first place, duck typing should handle it and raise exceptions as appropriate.
Re:Type hints cheat sheet (Python 3)
by kungtotte on Wednesday December 12, 2018 @03:29AM
(#18662291)
Type hints are more about proving correctness and catching errors early. If your code expects ints and all of a sudden it gets floats there is probably something wrong somewhere else, and the type checker tells you about it since Python would probably just chug along with floats instead of ints.