python / python/mypy

Wrong overload resolution with alias to Any

Open
#15,630 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Overload resolution appears to pick the first matching overload if the argument type is an alias to Any. It should consider all overloads. This works correctly when using Any directly (without an alias). This came up in https://github.com/python/typeshed/issues/10405.

To Reproduce

from typing import Any, Union, overload

@overload
def my_origin(x: type) -> type: ...

@overload
def my_origin(x: Any) -> Any: ...

def my_origin(x: Any) -> Any:
    return x

def call_any(spec: Any) -> bool:
    return my_origin(spec) is Union

TypeSpec = Any

def call_alias(spec: TypeSpec) -> bool:
    return my_origin(spec) is Union

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=26e08b7414caad553cd0e9549928da11

Expected Behavior

Both call_any and call_alias should type check without errors, since they are the same thing.

Actual Behavior

call_alias produces an error in strict mode:

proj/overload.py:18: error: Non-overlapping identity check (left operand type: "type", right operand type: "<typing special form>")  [comparison-overlap]

Your Environment

  • Mypy version used: 1.4.1
  • Mypy command-line flags: --strict
  • Python version used: 3.10.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 strict-mode reproducer from the issue and compare overload resolution for Any with the TypeSpec alias. Locate the overload-resolution path responsible for the differing comparison-overlap result; done means both call_any and call_alias type-check without errors and a regression test covers the case.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.