Skip to content

mypy doesn't escape typevars from generics in self-type #18400

Open
@A5rocks

Description

Bug Report

Passing a callable to another which matches the typevar with another typevar doesn't allow passing them into the return type. (if that makes no sense, the reproducer below is simple)

To Reproduce

from typing import Generic, TypeVar, Callable

T = TypeVar("T")
V = TypeVar("V")

class X(Generic[T]):
    def f(self: X[Callable[[V], None]]) -> Callable[[V], V]:
        def inner_f(v: V) -> V:
            return v

        return inner_f

reveal_type(X[Callable[[T], None]]().f())  # N: Revealed type is "def (Never) -> Never"
# ^ this should keep the generic

Expected Behavior

I don't expect a Never.

Your Environment

Checked on mypy playground.

  • Mypy version used: v1.14
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions