Files
Charlie Marsh 1edf9fe2ca [ty] Report deprecations for overloads, operators, and properties (#28134)
## Summary

Previously, we missed deprecations on selected overloads, implicit
operator and `__call__` calls, and property accessors. This PR adds
support for reporting `deprecated` in those contexts, e.g.:

```python
from typing import overload
from typing_extensions import deprecated

@overload
@deprecated("Use a string")
def convert(value: int) -> str: ...
@overload
def convert(value: str) -> str: ...
def convert(value: int | str) -> str:
    return str(value)

convert(1)  # Before: no diagnostic. After: deprecated.
convert("one")  # Before and after: no diagnostic.
```

This moves
[`directives_deprecated`](https://github.com/python/typing/blob/cf943ccbea5596ef969eec6e2260097ff697b7ba/conformance/tests/directives_deprecated.py)
from Partial to Pass.

<details>
<summary>Case analysis</summary>

Mypy 2.1.0, pyright 1.1.410, and pyrefly 1.3.0-dev.1 report all seven
required diagnostics in this conformance file. All comparisons
explicitly enable deprecation diagnostics. The differences below concern
additional cases in the regression tests.

We do not report a deprecated reflected operator when the left operand's
method accepts the operation. Pyright and pyrefly agree; mypy also warns
about the unused reflected method:

```python
from typing_extensions import deprecated

class Left:
    def __add__(self, other: object) -> int:
        return 0

class Right:
    @deprecated("reflected addition")
    def __radd__(self, other: object) -> int:
        return 0

Left() + Right()  # ty, pyright, pyrefly: OK; mypy: deprecated.
1 + Right()  # All four: deprecated.
```

A deprecated getter still runs during augmented assignment, even when
the setter is not deprecated. We report that deprecation, as does
pyrefly. Pyright misses the getter during augmented assignment, while
mypy loses the getter's deprecation when a non-deprecated setter or
deleter is subsequently defined. Access through the class returns the
property object without calling the getter; we leave that access quiet,
as do mypy and pyrefly:

```python
from typing_extensions import deprecated

class OldGetter:
    @property
    @deprecated("old getter")
    def value(self) -> int:
        return 0

    @value.setter
    def value(self, value: int) -> None: ...
    @value.deleter
    def value(self) -> None: ...

value = OldGetter()
value.value  # ty, pyright, pyrefly: deprecated; mypy: OK.
value.value = 1  # All four: OK.
value.value += 1  # ty, pyrefly: deprecated; mypy, pyright: OK.
OldGetter.value  # ty, mypy, pyrefly: OK; pyright: deprecated.
```

When both an overload and its implementation are deprecated, we emit the
implementation's diagnostic once, as does pyright. Mypy and pyrefly emit
separate diagnostics for the implementation and selected overload.

Union alternatives that select the same deprecated function produce one
diagnostic. If an operation can invoke several distinct deprecated
functions, we combine their messages and annotate each source
declaration. Augmented assignment still reports deprecated getters and
setters separately, since it invokes both.

</details>
2026-09-02 13:33:49 +00:00

2.4 KiB

Binary operations on union types

Binary operations on union types are only available if they are supported for all possible combinations of types:

def f1(i: int, u: int | None):
    # error: [unsupported-operator] "Operator `+` is not supported between objects of type `int` and `int | None`"
    reveal_type(i + u)  # revealed: Unknown
    # error: [unsupported-operator] "Operator `+` is not supported between objects of type `int | None` and `int`"
    reveal_type(u + i)  # revealed: Unknown

int can be added to int, and str can be added to str, but expressions of type int | str cannot be added, because that would require addition of int and str or vice versa:

def f2(i: int, s: str, int_or_str: int | str):
    i + i
    s + s
    # error: [unsupported-operator] "Operator `+` is not supported between two objects of type `int | str`"
    reveal_type(int_or_str + int_or_str)  # revealed: Unknown

However, if an operation is supported for all possible combinations, the result will be a union of the possible outcomes:

from typing import Literal

def f3(two_or_three: Literal[2, 3], a_or_b: Literal["a", "b"]):
    reveal_type(two_or_three + two_or_three)  # revealed: Literal[4, 5, 6]
    reveal_type(two_or_three**two_or_three)  # revealed: Literal[4, 8, 9, 27]

    reveal_type(a_or_b + a_or_b)  # revealed: Literal["aa", "ab", "ba", "bb"]

    reveal_type(two_or_three * a_or_b)  # revealed: Literal["aa", "bb", "aaa", "bbb"]

We treat a type annotation of float as a union of int and float, so union handling is relevant here:

def f4(x: float, y: float):
    reveal_type(x + y)  # revealed: float
    reveal_type(x - y)  # revealed: float
    reveal_type(x * y)  # revealed: float
    reveal_type(x / y)  # revealed: float
    reveal_type(x // y)  # revealed: float
    reveal_type(x % y)  # revealed: float

If any of the union elements leads to a division by zero, we will report an error:

def f5(m: int, n: Literal[-1, 0, 1]):
    # error: [division-by-zero] "Cannot divide object of type `int` by zero"
    return m / n

Binary-operator diagnostics share state across union alternatives, so several combinations that divide by zero produce only one warning per expression. Each expression has its own state, so repeating the operation still produces a warning:

def f6(m: Literal[1, 2], n: Literal[0, 1]):
    m / n  # error: [division-by-zero]
    m / n  # error: [division-by-zero]