-
-
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
TypeVar
bound to Union
in if isinstance(...)
branch has incompatible return type
#12800
Comments
As a side note: a |
This one looks like a bug to me :) For a (horrible) workaround, you can use overloads instead: from typing import overload
@overload
def a(x: str) -> str: ...
@overload
def a(x: int) -> int: ...
@overload
def a(x: int | str) -> int | str: ...
def a(x: int | str) -> int | str:
if isinstance(x, str):
return x
return x
b: str
c: int
d: int | str
reveal_type(a(b)) # Revealed type is "builtins.str"
reveal_type(a(c)) # Revealed type is "builtins.int"
reveal_type(a(d)) # Revealed type is "Union[builtins.int, builtins.str]" |
I don't know what does it mean for debugging, but this is also a bit odd: from typing import TypeVar, Union
T = TypeVar("T", bound=Union[str, int])
def a(x: T) -> T:
reveal_type(x) # T`-1
if isinstance(x, str):
reveal_type(x) # str
return x
reveal_type(x) # T`-1
return x Gives from typing import TypeVar, Union
T = TypeVar("T", bound=Union[str, int])
def a(x: T) -> T:
reveal_type(x) # T`-1
if isinstance(x, str | None):
reveal_type(x) # T`-1
return x
reveal_type(x) # T`-1
return x Gives x: str
if isinstance(x, str | None):
reveal_type(x) # str But... Maybe it should be a separate issue, given this weird result with x: str | int
if isinstance(x, str | None):
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]" |
@headtr1ck, a slightly simpler and less horrible workaround: just use a from typing import TypeVar, Union, cast
T = TypeVar("T", bound=Union[str, int])
def a(x: T) -> T:
if isinstance(x, str):
return cast(T, x)
return x |
This is what I ended up doing in the moment. |
Is this a duplicate of #10817? |
Closing as duplicate of #10817 |
not true, it is still open |
Bug Report
A TypeVar bound to
Union[x, y]
will resolve tox
after anif isinstance(..., x)
is used which is incompatible with the return type.I'm not sure on how to describe exactly what is going on, better check the Example.
Also mypy experts, please forgive me if that is intended behavior and not a bug (I would appreciate a working solution in this case :)
To Reproduce
Example:
Expected Behavior
I expect that this definition is correct, since whatever input is given, the output is the same.
Actual Behavior
mypy raises
error: Incompatible return value type (got "str", expected "T")
.Further Info
str -> str
,int -> int
mapping, so simply usingUnion[str, int]
won't work.Union[str, int]
, so usingTypeVar("T", str, int)
won't work either.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: