Skip to content
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

Edge cases in overload resolution #2671

Closed
JelleZijlstra opened this issue Dec 7, 2021 · 2 comments
Closed

Edge cases in overload resolution #2671

JelleZijlstra opened this issue Dec 7, 2021 · 2 comments

Comments

@JelleZijlstra
Copy link
Contributor

Consider this test case:

from typing import overload
from typing import List, Any, Union
from typing_extensions import Literal


@overload
def overloaded(lst: List[str]) -> Literal[1]:
    pass


@overload
def overloaded(lst: List[int]) -> Literal[2]:
    pass


def overloaded(lst: List[Any]) -> int:
    raise NotImplementedError


def check(
    unannotated,
    list_union: List[Union[int, str]],
    union_list: Union[List[int], List[str]],
    explicit: Any,
):
    val = overloaded([])  # pyright and mypy: Literal[1]
    reveal_type(val)
    val2 = overloaded([unannotated])  # pyright: Literal[1], mypy: Any
    reveal_type(val2)
    val3 = overloaded(unannotated)  # pyright: Literal[1], mypy: Any
    reveal_type(val3)
    val4 = overloaded(list_union)  # pyright: Unknown, mypy: Literal[1] (and both give an error)
    reveal_type(val4)
    val5 = overloaded(union_list)  # pyright and mypy: Literal[2, 1]
    reveal_type(val5)
    val6 = overloaded(explicit)  # pyright: Literal[1], mypy: Any
    reveal_type(val6)
    val7 = overloaded(["x"])
    reveal_type(val7)  # Literal[1]
    val8 = overloaded([1])
    reveal_type(val8)  # Literal[2]

I was surprised about the cases (val2, val3, val6) where pyright infers Literal[1] and mypy infers Any. It's consistent with the rule that we pick the first overload that matches, but Michael's comment in python/typing#253 (comment) (the closest we have to a spec of how overload works in mypy) suggests to avoid matches due to Any. That's also what I'd expect as a user.

I'm curious what led you to pick pyright's behavior here; I couldn't find any mention of it in the documentation.

@hmc-cs-mdrissi
Copy link

There's been a similar issue before on overload ambiguity with Any and answer then is here, #2521 (comment) . Main reason is Any behaves poorly for IDE experience.

@JelleZijlstra
Copy link
Contributor Author

Thanks @hmc-cs-mdrissi, that issue answers my questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants