5 ms·
I can't help but find type hints in python to be..goofy? I have a colleague who has a substantial C++ background and now working in python, the code is just lit
by curiousgal 7mo ago
I can't help but find type hints in python to be..goofy? I have a colleague who has a substantial C++ background and now working in python, the code is just littered with TypeAlias, Generic, cast, long Unions etc.. this can't be the way..
- IshKebab 7mo agoI strongly disagree. Python has actually done a decent job of adding type annotations into the language IMO. If you ignore the bit where they don't actually specify their semantics anyway. > this can't be the way.. The alternative is fragile and unmaintainable code. I know which I prefer!
- b00ty4breakfast 7mo agothe alternative should be using a real statically-typed language instead of glorified comments that don't do anything without outside tools. I understand that very large code bases have been built in python and this is a compromise to avoid making them rewrite Ks upon Ks of LoC but as it stands, Python type annotations are akin to putting a Phillip's head screwdriver on a ball peen hammer; the screwdriver is not a real screwdriver and the ergonomics of the hammer have been compromised.
- IshKebab 7mo agoWell yes I agree using Rust or whatever would be better, but if your options are Python or Python with type hints, then the latter gets you closest to proper static typing. They're really not that bad with Pyright in strict mode. Mypy is rubbish.
- tialaramex 7mo agoTyping is a relatively easy way for the human author and the machine to notice if they disagree about what's going on before problems arise. It is unfortunate that Python doesn't do a good job with types, I was reading earlier today about the mess they made of booleans - their bool type is actually just the integers again.
- nubg 7mo ago> I was reading earlier today about the mess they made of booleans Can you elaborate on that?
- IshKebab 7mo agoHe did - booleans are integers: >>> isinstance(False, int) True A related screw-up is implicitly casting everything to bool. A lot of languages made that mistake. Overall I'd say they didn't do an awful job though. The main problems with Python are the absolutely abysmal tooling (which thankfully uv fixes), the abysmal performance (which sometimes isn't an issue, but it usually becomes an issue eventually), and the community's attitude to type checking. Actually type checking code you've written yourself with Pyright in strict mode is quite a pleasant experience. But woe betide you if you want to import any third party libraries. There's at least a 50% chance they have no type annotations at all, and often it's deliberate. Typescript used to have a similar problem but the Javascript community realised a lot quicker than the Python community that type hints are a no-brainer.
- tialaramex 7mo agoBecause Python decided that (for the usual New Jersey reason, simplicity of implementation) bool should just be an integer type the Liskov criterion comes into play. If we can X an integer and we've agreed bool is an integer => we can X a bool. That's not what booleans are but hey, it's sorta close and this was easier to implement. So, can we add two bools together? Adding booleans together is nonsense, but we've said these are a kind of integer so sure, I guess True + True = 2 ? And this cascades into nonsense like ~True being a valid operation in Python and its result is true...
- jcgl 7mo ago> So, can we add two bools together? Adding booleans together is nonsense, but we've said these are a kind of integer so sure, I guess True + True = 2 ? And this cascades into nonsense like ~True being a valid operation in Python and its result is true... The bitwise negation is indeed janky and inaccurate, but True + True = 2 is absolutely a valid thing to say in boolean algebra. Addition mean "or", and multiplication means "and."
- wiseowise 7mo agoWhat is the way in your opinion?
- maleldil 7mo ago> TypeAlias, Generic This is mitigated by modern (3.12+) generic and `type` syntax, which just looks like any other static language.