4 ms·
If you're doing casting its probably a code smell and good indicator of something being off, or you're doing something too exotic. I'd say this applies a good a
by zo1 4y ago
If you're doing casting its probably a code smell and good indicator of something being off, or you're doing something too exotic. I'd say this applies a good amount to normal compiled languages too.
To do types well, I'd recommend constraining as much as possible. E.g. if a library gives you a union of 5 possible return types, just type it as one of them that you know you'll get and don't let that "complexity" propagate further into your code base. Likewise if you're wrapping a lib function that can take 3 levels worth of complex union or umpteen varargs, don't let that bleed through in your function signature "just in case".
And most crucial advice. Don't use Google's type checker pytypes. Or any other type checker for that matter other than Pycharms built-in one and mypy. Don't faff about with vscode either. Pycharm will make your python type hinting journey a pleasant experience (barring some generics and OOP corner case scenarios.)
- drewm1980 4y agoI agree. It is not perfect, but if pycharm can't figure out the type of an object, humans can't be expected to either.
- dragonwriter 4y ago> Or any other type checker for that matter other than Pycharms built-in one and mypy. Don’t faff about with vscode either. Eh, I’m not getting different editors for different languages (coding, documentation, and otherwise) used in the same project, and IntelliJ doesn’t have nice tooling for everything I need that VSCode does. Not sure how good Pycharm’s typechecking is, but pyright is, IME, usually better than mypy, and VSCode lets me belt-and-suspenders both of those if I choose (I did for a while, between being pure mypy and being pure pyright).
- AdamN 4y agoPyCharm will be a big upgrade over VSCode for any full sized project trying to use type hints (and really for any Python project)