4 ms·
Typing in Python is, at times, at odds with the language. Said another way, patterns which were once Pythonic are now discouraged by the type system (or, mypy
by sportsracer 6y ago
Typing in Python is, at times, at odds with the language.
Said another way, patterns which were once Pythonic are now discouraged by the type system (or, mypy I suppose, since the door is still open for other type checkers to emerge). Examples:
- Duck typing. I was taught that's Pythonic, yet mypy really only accepts nominal subtyping i.e. it infers type compatibility from the type hierarchy. There is typing.Protocol, but I've never encountered that in code. Ruby's new type annotation language RBS introduces interfaces, so maybe it'll stay true to its duck typing roots.
- Lists with mixed types. I might as well bookmark this page, because this behavior of mypy always surprises me: https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance https://mypy.readthedocs.io/en/stable/common_issues.html#inv...
In both cases, mypy reminds me way more of Java's type system than of Python's, which is maybe why heavily typed Python code often ends up looking like Java.
- dragonwriter 6y ago> Said another way, patterns which were once Pythonic are now discouraged by the type system (or, mypy I suppose, since the door is still open for other type checkers to emerge). There are several typecheckers, and while they are at slightly different points, they evolving together and together with the core typing library. > Duck typing. I was taught that's Pythonic, yet mypy really only accepts nominal subtyping i.e. it infers type compatibility from the type hierarchy. There is typing.Protocol Which entirely undercuts “mypy only accepts...” > but I've never encountered that in code. That's a library author issue, not a mypy (or typing, the standard library) issue. > In both cases, mypy reminds me way more of Java's type system than of Python's Python’s type system has always supported both nominal and structural subtyping. > heavily typed Python code often ends up looking like Java. That's probably because Java (and languages with closely related type systems) are the typed languages most people writing typed Python have prior experience with, so it's where their typing instincts go.