Skip to content

Return type of unbound methods of generic types incorrectly inferred. #13835

Closed as not planned
@jonathanslenders

Description

@jonathanslenders

Bug Report
Consider the following snippet:

from typing import Generic, TypeVar

T = TypeVar("T")


class Base(Generic[T]):
    def f(self) -> _T: ...


class Child(Base[bool]):
    ...


# Following three lines demonstrate two issues:
# - `T` was never filled in.
# - The return type became `Any`.

# test.py:21: note: Revealed type is "def (self: a.Base[T`1]) -> Any"
# test.py:22: note: Revealed type is "def [T] (self: a.Base[T`1]) -> Any"
# test.py:23: note: Revealed type is "def (self: a.Base[T`1]) -> Any"
reveal_type(Child.f)
reveal_type(Base.f)  # In this case, probably expected.
reveal_type(Base[int].f)

# When calling the unbound method, the return type is `Any`.
unbound_method = Child.f
x = unbound_method(Base())
reveal_type(x)  # Any.

Expected Behavior

I'd expect the typevar to be replaced.

Your Environment

  • Mypy version used: 0.982
  • Python version used: 3.9.13

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