python / python/mypy

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

Open
#21,768 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied snippet with mypy 2.3.0 and reading the “Type checking calls to overloads” documentation section. Trace overload resolution for Sequence[Any] versus Sequence[int]; done means the intended behavior is established and the revealed type, implementation, or documentation matches that decision.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.