-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Narrowing doesn't work on a typevar with a union bound #15631
Comments
By the way, the expected behavior is not correct: Imagine from typing import TypeVar
T = TypeVar("T", bound=int | str)
def foo(x: T) -> T:
if not isinstance(x, int):
return "h"
else:
# whatever
return x But that's not the case! The main problem here is typevar Mind clarifying a bit? |
Good point, my example is not 100% correct. Let me clarify and also add a second problem case.
from typing import TypeVar
T = TypeVar("T", bound=int | str)
def foo(x: T) -> T:
if isinstance(x, int):
reveal_type(x)
return x
else:
return x This produces:
The type of
from typing import TypeVar
T = TypeVar("T", bound=int | str)
def foo(x: T) -> None:
if not isinstance(x, int):
bar(x)
def bar(x: str) -> None:
pass This produces
but we know the type should be (a subclass of) To make it work in both cases, I think, rather than narrowing the type of |
Duplicate of #15235 |
Bug Report
not isinstance
check fails to narrow type for a TypeVar with a union bound.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=2be94d37f0b12222d62a7f40e59fb152
Expected Behavior
Revealed type is "builtins.str".
Actual Behavior
Revealed type is "T`-1".
Your Environment
The text was updated successfully, but these errors were encountered: