-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mypy does not check bounds of variable type aliases #13086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
According to PEP 484, if
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 from typing import Generic, TypeVar, TypeAlias
U = TypeVar("U", complex, str)
BoundTupleU: TypeAlias = tuple[U]
i_am_illegal2: BoundTupleU[int] |
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 |
Thank you for noticing! Not sure how that issue slipped past me. |
Bug Report
When having defined a
TypeAlias
dependent on aTypeVar
,mypy
does not allays that instances of these are actually allowed. That is, that they respect the bounds or restrictions of the usedTypeVar
. This is in contrast to the behavior for generic types, which are checked.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.10&flags=strict&gist=f5763650eba54f25b51553dbdd58179f
Expected Behavior
I expect four errors instead of two from the example code above.
Actual Behavior
As can be seen, I was allowed to define types that shouldn't be allowed, given the restrictions of the type variables in use.
Your Environment
The text was updated successfully, but these errors were encountered: