Skip to content

Odd behavior with overload and Any arguments. #15452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Dr-Irv opened this issue Jun 16, 2023 · 0 comments
Open

Odd behavior with overload and Any arguments. #15452

Dr-Irv opened this issue Jun 16, 2023 · 0 comments
Labels
bug mypy got something wrong

Comments

@Dr-Irv
Copy link

Dr-Irv commented Jun 16, 2023

Bug Report
The behavior of overload is not properly matching when one of the arguments is Any.

See microsoft/pyright#5216 (comment)

To Reproduce

from typing import Any, Literal, TypeVar, overload

_T = TypeVar("_T")

class A:
    @overload
    def method1(self, k: Literal["hi"], default: Any) -> float:
        ...

    @overload
    def method1(self, k: str, default: _T) -> Any | _T:
        ...

    def method1(self, k: str, default: _T) -> Any | _T:
        ...

def func(a: A, b: list, c: Any):
    v1 = a.method1("hi", [])
    reveal_type(v1)  # mypy: float

    my_list1: list = []
    v2 = a.method1("hi", my_list1)
    reveal_type(v2)  # mypy: Any

    v3 = a.method1("hi", b)
    reveal_type(v3)  # mypy: Any

    v4 = a.method1("hi", c)
    reveal_type(v4)  # mypy: Any

    my_list2: list[int] = []
    v5 = a.method1("hi", my_list2)
    reveal_type(v5)  # mypy: float```

**Expected Behavior**

All of the revealed types should have been `float`, since the first argument of each call to `method1()` is `"hi"`.  This is the behavior of `pyright`, which seems to make more sense.

**Actual Behavior**
```text
overload.py:19: note: Revealed type is "builtins.float"
overload.py:23: note: Revealed type is "Any"
overload.py:26: note: Revealed type is "Any"
overload.py:29: note: Revealed type is "Any"
overload.py:33: note: Revealed type is "builtins.float"

Your Environment

  • Mypy version used: 1.3
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant