Skip to content

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

Closed

Description

@nikitagashkov

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

Activity

  1. ikonst commented on Apr 30, 2023

    @ikonst
    Contributor

    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".

  2. JelleZijlstra commented on Jun 4, 2023

    @JelleZijlstra
    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.

  3. ikonst commented on Jun 6, 2023

    @ikonst
    Contributor

    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.

  4. hauntsaninja commented on Jun 6, 2023

    @hauntsaninja
    Collaborator
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions