4 ms·
This is a such a strange concept - optional static typing as an external add-on. All the headaches of needing another tool, none of the guarantees of the compil
by Animats 7y ago
This is a such a strange concept - optional static typing as an external add-on. All the headaches of needing another tool, none of the guarantees of the compiler doing the checking, and no optimization benefit from knowing the type at compile time. Plus the strangest feature of all - a mechanism for turning off the optional type checking so old code will still compile, even if it won't work right.
Optional type declarations for function parameters, checked and used by the compiler, would make sense. PyPy could use them to optimize basic numeric types. Within a function, you can usually infer types. From an optimization perspective, the only types worth distinguishing are probably int32, int64, slotted classes without inheritance, and everything else. "Everything else" needs the current dynamic dispatch mechanism; the other cases can have hard machine code generated.
But this optional stuff? That looks like a solution to a political problem, probably avoiding it being totally ignored. The Python designers used up their goodwill budget in the 2 to 3 fiasco.
- joshuamorton 7y agoWhat guarantees do you get from a compiler than you don't get from a third party tool? > Plus the strangest feature of all - a mechanism for turning off the optional type checking so old code will still compile, even if it won't work right. Most languages have this, in some form or another. There are ways to get around type checking when the compiler misunderstands something. How is this unique to python?
- choppaface 7y agoIn a lot of contexts for Python, static types don't add anything. A great example is the Norvig spell checker: https://norvig.com/spell-correct.html https://norvig.com/spell-correct.html Furthermore, the duck typing paradigm further warrants Python's position here. In the above example, the spell checker code can work with a variety of string types, or even lists, or even perhaps the Tensor-like things found in Tensorflow and Pytorch. Without duck typing, the author has to use generics and the user has to play a type game that can become obscene-- see Java. So that's a dominant mindset that sets the context here; things no doubt look strange from a different perspective. I agree it's odd that the interpreter doesn't enforce static type declarations by default. I've seen now two professional code bases where the authors used type hints everywhere and I found the code actually disobeyed the hints. (I guess nobody ran mypy regularly). That's probably what the Python committee anticipated: a few people would use this feature correctly, and hopefully build polished linting solutions as well. So they gave them an affordance for type hints. But most people would not want this at all, so it's entirely opt-in until it's something polished.
- Too 7y agoSorry but why wouldn't type hints add anything to that example? Type hints are usually there to help the user of the function, not to help with internal implementation. Just add word: str to all the functions and it will make it impossible to accidentally call any function with an invalid type. Especially the known(words) function is easy to misuse by providing a single string instead of a Iterable[str]. Iterable is a generic btw and will support both sets, lists and other similar types without any obscene type game. mypy also supports static duck typing with Protocols.
- phailhaus 7y ago> Furthermore, the duck typing paradigm further warrants Python's position here. Going to have to disagree with you here. Have you ever used Typescript? You can have duck typing and a proper static type system at the same time without devolving into Java. Furthermore, you can write Typescript without using types at all! Simply have everything default to `any`, and it reduces back down to Javascript. Python unfortunately decided to go in a direction that is the worst of both worlds. You can add type hints, but they do nothing without a 3rd party tool (?!). This means I can enter a code base, see type hints, and still have no guarantee that the types are what they claim to be. Instead, we have to rely on company-specific build processes to give us that static guarantee. And if you're not lucky enough to work in a codebase that runs mypy before merging, you might as well throw those type hints out the window.
- choppaface 7y agoI’ve used Scala which offers a similar experience. You still fall into the type game for generics, for example when using the just the Visitor pattern. Dealing with JSON is also a pain in any language with static types. I don’t believe you can really have duck typing and static at the same time unless you throw out the static checks and mark things as “Any” (or the equivalent). The beauty of Pythonic duck typing is that interfaces need not have any type declaration and furthermore the user can use dicts with complex structure (and declaring that structure with type annotations wouldn’t add any value over examples and/or unit tests). What makes Scala and modern C++ great is they offer nice interfaces for expedient use of the static type system. But there’s still plenty of room for the Pythonic approach of no static types at all.