6 ms·
>There should be type hints. "Hey, this variable is probably of this type. Now go on, run the code and launch a debugger the same way you would if there was no
by toomanydoubts 4y ago
>There should be type hints.
"Hey, this variable is probably of this type. Now go on, run the code and launch a debugger the same way you would if there was no hint just to make sure it's correct. Oh, and don't forget to check all the possible code paths that lead to this to make sure you've exhausted all possibilities."
Don't get me wrong, type hints are cool but they're just a toy compared to proper static typing.
- joshuamorton 4y ago> Oh, and don't forget to check all the possible code paths that lead to this to make sure you've exhausted all possibilities." This is literally what mypy does. There's no difference between a mypy-enabled codebase and a java codebase in terms of static typing (other than that mypy actually supports a generally richer type system). Like yes if you aren't validating the type hints you aren't getting the value, but if you are, you don't need to run the code! I get most of my python errors at compile time, and you can too. I promise.
- FridgeSeal 4y agoSorry, but MyPy and friends can barely hold a candle to what actual static-typing systems do - Rust, Haskell, OCaml, Typescript, even .Net (C#/F#) offer more safety and guarantees. Type hints in python are _just that_, they're hints. The whole "typed" python ecosystem relies on packages and the language using them, using them correctly, and using them consistently. In my experience so far, I've seen little widespread use, and nothing from core python, which is a huge hole IMO.
- joshuamorton 4y ago> Sorry, but MyPy and friends can barely hold a candle to what actual static-typing systems do - Rust, Haskell, OCaml, Typescript, even .Net (C#/F#) offer more safety and guarantees. Note that you don't include the most common static languages there (cpp, java, golang), you include languages known *specifically " for their extremely powerful type systems, one of which , just like mypy, uses gradual typing! This supports my argument!
- FridgeSeal 4y agoI mean sure, we can include C++, go and java in there if you want? I don’t really know what the point you’re making here is? > one of which , just like mypy, uses gradual typing! This is like arguing that because your scooter also has a petrol engine, it’s as fast and efficient as an F1 car. My argument was that MyPy doesn’t compete with the capabilities of modern statically typed languages. TS might also technically be gradually typed, but most devs I know just go straight to fully typed TS, because working in a mix/gradual environment is painful and the community uptake of strict types is far higher.
- joshuamorton 4y agoWhat features that Javas type system provides does pythons lack? I can name dozens that python has that Java doesn't. My point is that if you include cpp or java or golang the statement becomes false. Python's type system is actually generally speaking more robust than those (cpp is comparable, but I don't think python's is worse). So I'm asking, be specific what capabilities does mypy lack that cpp or java or golang provide? Keep in mind, mypy supports literal types, limited dependent types, dynamic-duck typing (protocols), limited contract types, and more. Java doesn't, go doesn't. Cpp supports some of those but it requires arcane incantations.
- mdaniel 4y ago> There's no difference between a mypy-enabled codebase and a java codebase in terms of static typing (other than that mypy actually supports a generally richer type system). that must require some advanced usage of mypy then, because my experience was that even the most simple example got the finger from mypy that would have very obviously worked in Java mkdir alpha echo 'beta = True' > alpha/models.py echo 'from .models import beta' > alpha/__init__.py echo 'import alpha; print(alpha.beta)' > doit.py mypy --strict doit.py # doit.py:1: error: Module has no attribute "beta" And look, I'm with you that I wish any codebase with an `__init__.py` anywhere in it (thus, what I think of as a project versus some ad-hoc python scripting whatever) would type-hint the absolute hell out of things, but claiming mypy or any such linting system is as strict as a strongly typed language always boils my blood
- still_grokking 4y agoIt's imho especially ridiculous to say some linter, that needs "hints" and annotations everywhere, is comparable to a static type system when looking at modern static languages, which have usually very strong type inference. The point of a sound static type system is that you get a guaranty that your program is type-safe even in case not everything is "hinted properly". With the linter approach on the other hands side it won't get better than what a dynamic language offers anyway: To be (more or less) sure everything is like intended all you can do is to run your program and pray… You can't upfront rule out for sure all kinds of errors like when using a proper static language.
- mdaniel 4y agoDid you reply to the right person? I'm with you, friend :-)
- still_grokking 4y agoI think I did not object anything you've said. :-) I only wanted to point out once more that a proper static type system is of more use than some bolted on linter. (And add the point about type inference).
- raydev 4y ago> This is literally what mypy does. Kinda silly that this is optional tooling that came from the community to solve a problem in the original implementation. Sure, having a community and general inertia, because Python is simply good enough and no alternative has a killer feature, is certainly something you can rely on. It's okay to ask for more, though.
- smabie 4y agoThe number of "# type: ignore" comments I've seen in all python codebases that use mypy seems to disagree. mypy is laughably primitive compared to advanced static typing systems: Rust, Haskell, OCaml, etc
- joshuamorton 4y ago> mypy is laughably primitive compared to advanced static typing systems I don't disagree, but that's not an argument I ever put forth! (and I actually sort of disagree a little bit, mypy has/is getting a bunch of really cool powerful features that put it in the realm of "as cool as" those languages: PEPs 544, 586, 591, 647 (this is a weak form of contract types!), 675 (~constexpr string type), 646 (variadic generic/numeric dependent types!))