-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Type narrowing of 'TypeVar' causes '[return-value]' error #15151
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
One way to think about it is that " |
That would be incorrect, since |
What am I missing? from typing import TypeVar
T = TypeVar('T')
class mystr(str):
pass
def f(v: T) -> T:
if isinstance(v, str):
return mystr("bar") # at this point, T is narrowed to TypeVar('T', bound=str)
return v
value = f("foo")
reveal_type(value) # N: Revealed type is "builtins.str" IIUC, |
|
Bug Report
mypy
reports a type incompatibility error when theTypeVar
s are narrowed within a generic function but the generic arg is not changed in any way.To Reproduce
…and a mypy-play link.
Expected Behavior
No errors are thrown since the variable wasn't patched in any way (e.g., it was not returned as
str(v)
).Actual Behavior
Your Environment
mypy.ini
(and other config files): NoneThe text was updated successfully, but these errors were encountered: