Skip to content

Bug: overloads not matched when using TypeVar with default (where Pyright matches correctly) #19182

Open
@MarcoGorelli

Description

@MarcoGorelli

Bug Report

In the example below, mypy reveals the type to be Any, whereas Pyright correctly detects Bar

Note that replacing T | None with T makes Mypy also infer the type correctly

Noticed while working on pandas-stubs

To Reproduce

from typing import Any, Callable, Generic, Protocol, Self, overload, reveal_type

from typing_extensions import TypeVar

T = TypeVar("T", bound=str | int, default=Any)


class Foo(Generic[T]):
    def __init__(self, t: T) -> None:
        self.t = t


class Bar(Protocol):
    @overload
    def apply(self, f: Callable[..., Foo]) -> Self: ...
    @overload
    def apply(self, f: Callable[..., T | None]) -> Foo[T]: ...


def func() -> Foo:
    return Foo(3)


def main(b: Bar) -> None:
    reveal_type(b.apply(func))

https://mypy-play.net/?mypy=latest&python=3.12&gist=6df33ff32887ddfab4c1ae43edd62b2d

Expected Behavior

that reveal_type would show Bar (for reference, this is what Pyright does)

Actual Behavior

note: Revealed type is "Any"

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-callsFunction calls, *args, **kwargs, defaultstopic-pep-696TypeVar defaults

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions