You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mypy does not check bounds of variable type aliases #13086
When having defined a TypeAlias dependent on a TypeVar, mypy does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the used TypeVar. This is in contrast to the behavior for generic types, which are checked.
To Reproduce
fromtypingimportGeneric, TypeVar, TypeAliasT=TypeVar("T", bound=bytes|str)
U=TypeVar("U", bytes, str)
BoundedTuple: TypeAlias=tuple[T]
RestrictedTuple: TypeAlias=tuple[U]
i_am_illegal1: BoundedTuple[int] # Passes, even though this type shouldn't be possible!i_am_illegal2: RestrictedTuple[int] # Passes, even though this type shouldn't be possible!classBounded(Generic[T]):
passclassRestricted(Generic[U]):
passi_am_illegal3: Bounded[int] # Behaves as expected, resulting in `error: Type argument "int" of "Bounded" must be a subtype of "Union[bytes, str]"`i_am_illegal4: Restricted[int] # Behaves as expected, resulting in `error: Value of type variable "U" of "Restricted" cannot be "int"`
According to PEP 484, if T = TypeVar("T", bound=complex | str), T can be solved as being int where necessary, since PEP 484 states that type checkers should consider int and float to be implicit subtypes of complex. In "The Numeric Tower", the PEP states:
when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable.
This explains why mypy does not emit an error for your first and third example.
Your second example does seem to be buggy to me, however. ConstrainedTypeVars should only be solveable with one of the exact constraints, i.e., subtypes should not be permitted. So I believe you're correct when you say that mypy should be emitting an error for the following:
[...] This explains why mypy does not emit an error for your first and third example.
Good catch! Appreciate it. Changed the example, but the first example also remains faulty, so it seems this is an issue for variable type aliases, but not for generics. Please let me know whether the issue is worded more clearly (and correctly) now.
Oh yikes — yup, your revised first example definitely looks like a bug to me!
... But it's a duplicate of #10445. Your example using the constrained TypeVar is new, but I'm closing this so that we can keep all the discussion in one place, since it's the same bug :)
... But it's a duplicate of #10445. Your example using the constrained TypeVar is new, but I'm closing this so that we can keep all the discussion in one place, since it's the same bug :)
Thank you for noticing! Not sure how that issue slipped past me.
Activity
AlexWaygood commentedon Jul 7, 2022
According to PEP 484, if
T = TypeVar("T", bound=complex | str),Tcan be solved as beingintwhere necessary, since PEP 484 states that type checkers should considerintandfloatto be implicit subtypes ofcomplex. In "The Numeric Tower", the PEP states:This explains why mypy does not emit an error for your first and third example.
Your second example does seem to be buggy to me, however. Constrained
TypeVars should only be solveable with one of the exact constraints, i.e., subtypes should not be permitted. So I believe you're correct when you say that mypy should be emitting an error for the following:[-]Mypy does not check bounds of all generic types[/-][+]Mypy does not check bounds of variable type aliases[/+]harahu commentedon Jul 7, 2022
Good catch! Appreciate it. Changed the example, but the first example also remains faulty, so it seems this is an issue for variable type aliases, but not for generics. Please let me know whether the issue is worded more clearly (and correctly) now.
AlexWaygood commentedon Jul 7, 2022
Oh yikes — yup, your revised first example definitely looks like a bug to me!
AlexWaygood commentedon Jul 7, 2022
... But it's a duplicate of #10445. Your example using the constrained
TypeVaris new, but I'm closing this so that we can keep all the discussion in one place, since it's the same bug :)harahu commentedon Jul 7, 2022
Thank you for noticing! Not sure how that issue slipped past me.