python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.35k stars 2.81k forks source link

Wrong overload resolution with alias to Any #15630

Open eltoder opened 1 year ago

eltoder commented 1 year ago

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

AlexWaygood commented 1 year ago

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