Skip to content

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

Closed
nikitagashkov opened this issue Apr 27, 2023 · 4 comments · Fixed by #19183
Closed

Type narrowing of 'TypeVar' causes '[return-value]' error #15151

nikitagashkov opened this issue Apr 27, 2023 · 4 comments · Fixed by #19183
Labels
bug mypy got something wrong

Comments

@nikitagashkov
Copy link

Bug Report

mypy reports a type incompatibility error when the TypeVars are narrowed within a generic function but the generic arg is not changed in any way.

To Reproduce

from typing import TypeVar

T = TypeVar("T")


def foo(v: T) -> T:
    if isinstance(v, str):
        return v  # error: Incompatible return value type (got "str", expected "T")  [return-value]
    return v

…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

main.py:8: error: Incompatible return value type (got "str", expected "T")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
@nikitagashkov nikitagashkov added the bug mypy got something wrong label Apr 27, 2023
@ikonst
Copy link
Contributor

ikonst commented Apr 30, 2023

One way to think about it is that "v didn't change" suggesting that v should narrow not to an Instance but to a TypeVarType bound to str. Or maybe it should "narrow" not v itself but T so that the function itself, within that branch, would now be happy with return "foo".

@JelleZijlstra
Copy link
Member

Or maybe it should "narrow" not v itself but T so that the function itself, within that branch, would now be happy with return "foo".

That would be incorrect, since foo can be a subtype of str.

@ikonst
Copy link
Contributor

ikonst commented Jun 6, 2023

That would be incorrect, since foo can be a subtype of str.

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, value being deemed a str here is fine, even though it might end up a mystr.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jun 6, 2023

from typing import TypeVar

T = TypeVar('T')

class mystr(str): ...
  
class otherstr(str): ...

def f(v: T) -> T:
    if isinstance(v, str):
        return mystr("bar")
    return v

value = f(otherstr("foo"))
reveal_type(value)  # oops, revealed type is "__main__.otherstr"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
4 participants