5 ms·
something that's always had me a bit confused: the type hinting in Python is just that -- hinting, right? It's for IDEs like PyCharm to use to generate better
by 2bitencryption 11y ago
something that's always had me a bit confused:
the type hinting in Python is just that -- hinting, right?
It's for IDEs like PyCharm to use to generate better docs and to be more intelligent about suggestions when writing code. But what it does NOT do is inform the interpreter/compiler about type information, correct?
I.e., Python3 code with perfectly implemented type hints would only make it easier on the developer, and not the interpreter/compiler, yeah?
- Siecje 11y agoCorrect.
- kevin_thibedeau 11y agoYou could add a decorator or metaclass that dynamically checks the hints at runtime.
- rspeer 11y agoSo far I've been using Obiwan [1] to check types during testing. It's definitely not something you want to do all the time, because it has a huge runtime cost. [1] https://github.com/williame/obiwan https://github.com/williame/obiwan What I do is I have my test runner look for the PYTHON_TYPECHECK environment variable, and if it's set, it imports and enables Obiwan. I've caught a few subtle bugs this way.
- techdragon 11y agoThis is a job for the little used -O flag. Are there a any good libraries to turn on/off type checking using if debug as part of the decoration logic? If so then running with -O in production would solve some of the problem with this.
- bpicolo 11y agoUntil an interpreter/compiler supports otherwise, anyway ;)
- ubernostrum 11y agoThe CPython interpreter does not enforce any errors/warnings/etc. based on annotations. It does programmatically expose annotations if you want to inspect them at runtime, but the feature is mostly meant for other tools to optionally hook into.
- jsmeaton 11y agoYes, but there's nothing to say that an interpreter couldn't use the type hinting. Imagine a pypy-hinting interpreter that analysed the code and kept the hinting, it could theoretically JIT it better.
- rplnt 11y agoOh, I was somehow hoping this would allow you to have multiple function signatures. i.e. two functions foo, both accepting one variable named bar, but one would implement str and one int, for example.
- zsiciarz 11y agoYou can have that since 3.4, thanks to PEP 443 (https://www.python.org/dev/peps/pep-0443/ https://www.python.org/dev/peps/pep-0443/). Here's a (slightly contrived) example: https://gist.github.com/zsiciarz/e15199d9be1b0e07faf6 https://gist.github.com/zsiciarz/e15199d9be1b0e07faf6
- rootlocus 11y agoTo help with the phrasing: this is called function overloading. Type hinting is ignored by the interpreter so it doesn't add any new functionality, only documentation.
- coldtea 11y ago>I.e., Python3 code with perfectly implemented type hints would only make it easier on the developer, and not the interpreter/compiler, yeah? At the moment no. But there's nothing to say that it couldn't happen in 3.8 or 3.11 or 4.x, since the groundwork is there now.