Skip to content

Overload resolution collapses to Any when Any is nested inside a generic argument, not just top-level #21768

Description

@dpinol

Bug Report

The docs state the return type becomes Any only when "an argument [is] of type Any". But when the Any is nested inside a generic type argument (Sequence[Any] vs Sequence[int]), mypy also collapses the result to Any, even though the first overload matches unambiguously. Is this intended? If so, the "Type checking calls to overloads" section should probably document the nested case.

To Reproduce

from collections.abc import Sequence
from typing import Any, overload, reveal_type


@overload
def f(s: Sequence[int]) -> str: ...
@overload
def f(s: object) -> int: ...
def f(s: object) -> int | str:
    return "2"


concrete: Sequence[int] = [1]
nested_any: Sequence[Any] = [1]

reveal_type(f(concrete))  # Revealed type is "str"    (expected)
reveal_type(f(nested_any))  # Revealed type is "Any"    (surprising)

Expected Behavior

reveal_type on Sequence[Any] should show "str"

Actual Behavior

reveal_type on Sequence[Any] shows "Any"

Your Environment

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

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