-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
TypeVar bound is ignored in generic alias #10445
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
I would like to mention that using mypy 0.91 I see a more general version of this error where the resolved GenericAlias isn't type checked at all regardless of the bound on the TypeVar. That is: FooT = typing.TypeVar("FooT")
FooIsh = typing.Union[int, FooT]
bat2: FooIsh[float] = "passes" fails to report an error. |
I just closed #13086 as a duplicate of this issue, but @harahu notes in that issue that this bug is also present for generic type aliases that use constrained TypeVars (as well as those that use bound TypeVars): from typing import Generic, TypeVar, TypeAlias
U = TypeVar("U", bytes, str)
RestrictedTuple: TypeAlias = tuple[U]
i_am_illegal: RestrictedTuple[int] # Passes, even though this type shouldn't be possible! |
Fixes #11855 Fixes #7084 Fixes #10445 Should fix #4987 After thinking about this for some time, it looks like the best way to implement this is by switching type aliases from unbound to bound type variables. Then I can essentially simply share (or copy in one small place, to avoid cyclic imports) all the logic that currently exists for `ParamSpec` and `Concatenate` in `expand_type()` etc. This will also address a big piece of tech debt, and will get some benefits (almost) for free, such as checking bounds/values for alias type variables, and much tighter handling of unbound type variables. Note that in this PR I change logic for emitting some errors, I try to avoid showing multiple errors for the same location/reason. But this is not an essential part of this PR (it is just some test cases would otherwise fail with even more error messages), I can reconsider if there are objections.
@ilevkivskyi Are you sure this should be closed? I just made a minimal playground example out of the issue I pointed out in #13086, that @AlexWaygood mentioned above: https://mypy-play.net/?mypy=master&python=3.11&flags=strict&gist=b251ba96b0e4c9632c8ba162f81280e8 This example still fails when run with mypy from the master branch. |
@harahu, if I run mypy with the import typing
FooT = typing.TypeVar("FooT", bound=str)
FooIsh = typing.Union[int, FooT]
bat: FooIsh[float] = 1.0
FooT2 = typing.TypeVar("FooT2")
FooIsh2 = typing.Union[int, FooT]
bat2: FooIsh[float] = "fails"
U = typing.TypeVar("U", bytes, str)
RestrictedTuple: typing.TypeAlias = tuple[U]
i_am_illegal: RestrictedTuple[int] Then this is the output:
This seems like the correct output to me; I think it was correct for this issue to be closed. The "run mypy with the |
I see. You correctly pointed out where I was mistaken. I assumed playground was up to date. Sorry about the confusion. |
Bug Report
A generic alias using a TypeVar with a bound allows type arguments that don't match the bound
To Reproduce
Expected Behavior
I would expect an error saying that the value of
FooT
cannot befloat
.Found this as a result of microsoft/pyright#1839.
The text was updated successfully, but these errors were encountered: