mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Wrong overload resolution with alias to Any

Open eltoder opened this issue 2 years ago • 1 comments

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

eltoder avatar Jul 09 '23 17:07 eltoder

Adding reveal_type calls to the example confirms that mypy is picking a different overload with the alias-to-Any version: https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=8b3429d9daae0e46ebc64d5aae592513

AlexWaygood avatar Jul 09 '23 17:07 AlexWaygood