-
Notifications
You must be signed in to change notification settings - Fork 135
Open
Labels
needs-designNeeds further design before implementationNeeds further design before implementationoverloadstyping semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc
Description
Consider the following example:
overloaded.pyi:
from typing import overload
from typing_extensions import LiteralString
@overload
def f(x: LiteralString) -> LiteralString: ...
@overload
def f(x: str) -> str: ...from typing import Any
from typing_extensions import LiteralString
from overloaded import f
def _(literal: LiteralString, string: str, any: Any):
reveal_type(f(literal)) # revealed: LiteralString
reveal_type(f(string)) # revealed: str
# `Any` matches both overloads, but the return types are not equivalent.
# Pyright and mypy both reveal `str` here, contrary to the spec.
reveal_type(f(any)) # revealed: AnyFor the last call, ty reveals Any while Pyright and mypy reveals str. The reason ty reveals Any is because the overload matching is ambiguous according to the spec. Here's how the algorithm detects that:
- Arity does not eliminate any overloads
- Type checking does not eliminate any overloads
- Step 3 is skipped since step 2 did not result in errors for all overloads
- Step 4 has no effect, since neither overload has a variadic parameter
- All materialization of
Anyare not assignable to either of the overloads since it could materialize into anything other thanLiteralStringorstr. The return types of the remaining overloads are not equivalent so the overload matching is ambiguous.
More formally, at the end of step 5, before concluding that the overload matching is ambiguous, ty should check whether the return types are overlapping and return the widest type of them instead of Any.
I'm not exactly sure if this is what Pyright / mypy does so it might be useful to hear from someone who knows about this.
Metadata
Metadata
Assignees
Labels
needs-designNeeds further design before implementationNeeds further design before implementationoverloadstyping semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc