Allow returning unions/intersections rather than `Any` for amiguous overloads.
topic: documentation
Currently, the typing spec for overloads, [step 5](https://typing.python.org/en/latest/spec/overload.html#step-5), states that
> Once this filtering process is applied for all arguments, examine the return types of the remaining overloads. If these return types include type variables, they should be replaced with their solved types. If the resulting return types for all remaining overloads are [equivalent](https://typing.python.org/en/latest/spec/glossary.html#term-equivalent), proceed to step 6.
>
> If the return types are not equivalent, overload matching is ambiguous. In this case, assume a return type of Any and stop.
However, couldn't this rule be relaxed to returning the union of the return type of all matching overloads? For instance, consider this example derived from a real world use case (numpy scalar ops)
```python
from typing import Any, overload, assert_type
class A[T]: # covariant
def get(self) -> T: ...
@overload
def op(l: A[None], r: A[None]) -> A[None]: ...
@overload
def op(l: A[None], r: A[Any]) -> A[None]: ...
@overload
def op(l: A[Any], r: A[None]) -> A[None]: ...
@overload
def op(l: A[Any], r: A[Any]) -> A[Any]: ...
def test(x: A[None], y: A[Any]) -> None:
assert_type(op(x, x), A[None]) # spec: ✅️
assert_type(op(x, y), A[None]) # spec: ✅️
assert_type(op(y, x), A[None]) # spec: ✅️
assert_type(op(y, y), A[Any] | A[None]) # spec: ❌️ (expected Any)
```
According to the rule stated above, `op(A[Any], A[Any])` should be inferred to as `Any`, because the overloads are ambious and `A[Any]` is not [equivalent](https://typing.python.org/en/latest/spec/glossary.html#term-equivalent) to `A[None]` as not all materializations of `A[Any]` can be assigned to `A[None]`.
However inferring `Any` loses deducible information: we know that no matter what, the return type must be an `A`. So in principle the return type should be `A[Any] | A[None]`, because if either the left argument or the right argument materializes to `A[None]`, then we get `A[None]` and otherwise if they materialize to something else we get `A[Any]`.
2 条评论