Replies: 1 comment
-
|
So, at runtime a single constraint is rejected (TypeVar("T", int) → TypeError), but repeated ones are fine, like: type INT = int
T = TypeVar("T", int, int, INT) # ths works; __constraints__ == (int, int, INT)Type checkers accept it too. Duplicate constraints basically collapse to the same type, so "T" behaves like its constrained to int. On f(True) → int: bool is a subclass of int in Python, so checkers treat True as compatible with an int constraint. The spec says you need at least two constraints and cant have just one. int, int, INT satisfies that literally, but its really the same as a single int constraint, which the spec says isnt allowed. This looks more like a spec edge case than a checker bug. Maybe this just needs a typing spec clarification. It works today, both at runtime and in type checkers, but I dont think that this is a checker bug. Prolly more of a edge case where the spec doenst say whether repeated || equivalent constraints should count separately |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The spec currently says:
To my surprise, no type-checker seems to reject this, and allows usage of this type variable:
This was found when changing one of the constraints from a
NewType("MyInt", int)to aTypeAliasType("MyInt", int).Beta Was this translation helpful? Give feedback.
All reactions