diff --git a/mypy/typeshed/stdlib/_typeshed/__init__.pyi b/mypy/typeshed/stdlib/_typeshed/__init__.pyi index fa6ef8150e3a5..3eaf8df964934 100644 --- a/mypy/typeshed/stdlib/_typeshed/__init__.pyi +++ b/mypy/typeshed/stdlib/_typeshed/__init__.pyi @@ -59,12 +59,11 @@ Unused: TypeAlias = object # stable MaybeNone: TypeAlias = Any # stable # typeshed-internal type aliases to facilitate transition from -# `float` to either `float | int` or `float` (and similar for `complex`). -# When you encounter one of these type aliases, you are encouraged to -# replace them with the correct type. Please don't use them outside typeshed. +# `float` to either `float | int` or `float`. When you encounter this type +# alias, you are encouraged to replace it with the correct type. Please don't +# use it outside typeshed. # See https://github.com/python/typeshed/issues/16059 for details. FloatInt: TypeAlias = float | int -ComplexInt: TypeAlias = complex | float | int # Used to mark arguments that default to a sentinel value. This prevents # stubtest from complaining about the default value not matching. @@ -146,6 +145,15 @@ class SupportsDivMod(Protocol[_T_contra, _T_co]): class SupportsRDivMod(Protocol[_T_contra, _T_co]): def __rdivmod__(self, other: _T_contra, /) -> _T_co: ... +class SupportsRAnd(Protocol[_T_contra, _T_co]): + def __rand__(self, other: _T_contra, /) -> _T_co: ... + +class SupportsROr(Protocol[_T_contra, _T_co]): + def __ror__(self, other: _T_contra, /) -> _T_co: ... + +class SupportsRXor(Protocol[_T_contra, _T_co]): + def __rxor__(self, other: _T_contra, /) -> _T_co: ... + # This protocol is generic over the iterator type, while Iterable is # generic over the type that is iterated over. class SupportsIter(Protocol[_T_co]): diff --git a/mypy/typeshed/stdlib/_weakrefset.pyi b/mypy/typeshed/stdlib/_weakrefset.pyi index 82ffa4463f488..b2d0bbadbba78 100644 --- a/mypy/typeshed/stdlib/_weakrefset.pyi +++ b/mypy/typeshed/stdlib/_weakrefset.pyi @@ -27,11 +27,11 @@ class WeakSet(MutableSet[_T]): def difference(self, other: Iterable[_T]) -> Self: ... def __sub__(self, other: Iterable[Any]) -> Self: ... def difference_update(self, other: Iterable[Any]) -> None: ... - def __isub__(self, other: Iterable[Any]) -> Self: ... + def __isub__(self, other: Iterable[Any]) -> Self: ... # type: ignore[override] def intersection(self, other: Iterable[_T]) -> Self: ... def __and__(self, other: Iterable[Any]) -> Self: ... def intersection_update(self, other: Iterable[Any]) -> None: ... - def __iand__(self, other: Iterable[Any]) -> Self: ... + def __iand__(self, other: Iterable[Any]) -> Self: ... # type: ignore[override] def issubset(self, other: Iterable[_T]) -> bool: ... def __le__(self, other: Iterable[_T]) -> bool: ... def __lt__(self, other: Iterable[_T]) -> bool: ... diff --git a/mypy/typeshed/stdlib/ast.pyi b/mypy/typeshed/stdlib/ast.pyi index f84c93e7953e5..000f84093604d 100644 --- a/mypy/typeshed/stdlib/ast.pyi +++ b/mypy/typeshed/stdlib/ast.pyi @@ -1711,7 +1711,7 @@ else: if sys.version_info < (3, 14): @deprecated("Removed in Python 3.14. Use `ast.Constant` instead.") class Num(Constant, metaclass=_ABC): - def __new__(cls, n: complex, **kwargs: Unpack[_Attributes]) -> Constant: ... # type: ignore[misc] # pyright: ignore[reportInconsistentConstructor] + def __new__(cls, n: complex | float | int, **kwargs: Unpack[_Attributes]) -> Constant: ... # type: ignore[misc] # pyright: ignore[reportInconsistentConstructor] @deprecated("Removed in Python 3.14. Use `ast.Constant` instead.") class Str(Constant, metaclass=_ABC): diff --git a/mypy/typeshed/stdlib/builtins.pyi b/mypy/typeshed/stdlib/builtins.pyi index e5890b5025ef1..fcc236432bbde 100644 --- a/mypy/typeshed/stdlib/builtins.pyi +++ b/mypy/typeshed/stdlib/builtins.pyi @@ -25,9 +25,13 @@ from _typeshed import ( SupportsLenAndGetItem, SupportsNext, SupportsRAdd, + SupportsRAnd, SupportsRDivMod, SupportsRichComparison, SupportsRichComparisonT, + SupportsROr, + SupportsRSub, + SupportsRXor, SupportsWrite, ) from collections.abc import Awaitable, Callable, Hashable, Iterable, Iterator, MutableSet, Set as AbstractSet, Sized @@ -80,6 +84,7 @@ _KT = TypeVar("_KT") _VT = TypeVar("_VT") _VT_co = TypeVar("_VT_co", covariant=True) _S = TypeVar("_S") +_R = TypeVar("_R") _T1 = TypeVar("_T1") _T2 = TypeVar("_T2") _T3 = TypeVar("_T3") @@ -428,7 +433,7 @@ class float: def __format__(self, format_spec: str, /) -> str: ... if sys.version_info >= (3, 14): @classmethod - def from_number(cls, number: float | SupportsIndex | SupportsFloat, /) -> Self: ... + def from_number(cls, number: SupportsIndex | SupportsFloat, /) -> Self: ... @disjoint_base class complex: @@ -440,23 +445,23 @@ class complex: imag: complex | SupportsFloat | SupportsIndex = 0, ) -> Self: ... @overload - def __new__(cls, real: str | SupportsComplex | SupportsFloat | SupportsIndex | complex) -> Self: ... + def __new__(cls, real: str | SupportsComplex | SupportsFloat | SupportsIndex | complex | float | int) -> Self: ... @property def real(self) -> float: ... @property def imag(self) -> float: ... def conjugate(self) -> complex: ... - def __add__(self, value: complex, /) -> complex: ... - def __sub__(self, value: complex, /) -> complex: ... - def __mul__(self, value: complex, /) -> complex: ... - def __pow__(self, value: complex, mod: None = None, /) -> complex: ... - def __truediv__(self, value: complex, /) -> complex: ... - def __radd__(self, value: complex, /) -> complex: ... - def __rsub__(self, value: complex, /) -> complex: ... - def __rmul__(self, value: complex, /) -> complex: ... - def __rpow__(self, value: complex, mod: None = None, /) -> complex: ... - def __rtruediv__(self, value: complex, /) -> complex: ... + def __add__(self, value: complex | float | int, /) -> complex: ... + def __sub__(self, value: complex | float | int, /) -> complex: ... + def __mul__(self, value: complex | float | int, /) -> complex: ... + def __pow__(self, value: complex | float | int, mod: None = None, /) -> complex: ... + def __truediv__(self, value: complex | float | int, /) -> complex: ... + def __radd__(self, value: complex | float | int, /) -> complex: ... + def __rsub__(self, value: complex | float | int, /) -> complex: ... + def __rmul__(self, value: complex | float | int, /) -> complex: ... + def __rpow__(self, value: complex | float | int, mod: None = None, /) -> complex: ... + def __rtruediv__(self, value: complex | float | int, /) -> complex: ... def __eq__(self, value: object, /) -> bool: ... def __ne__(self, value: object, /) -> bool: ... def __neg__(self) -> complex: ... @@ -469,7 +474,7 @@ class complex: def __complex__(self) -> complex: ... if sys.version_info >= (3, 14): @classmethod - def from_number(cls, number: complex | SupportsComplex | SupportsFloat | SupportsIndex, /) -> Self: ... + def from_number(cls, number: SupportsComplex | SupportsFloat | SupportsIndex, /) -> Self: ... @type_check_only class _FormatMapMapping(Protocol): @@ -1294,6 +1299,8 @@ if sys.version_info >= (3, 15): def __getitem__(self, key: _KT, /) -> _VT_co: ... def __reversed__(self) -> Iterator[_KT]: ... def __iter__(self) -> Iterator[_KT]: ... + def __eq__(self, value: object, /) -> bool: ... + def __ne__(self, value: object, /) -> bool: ... def __hash__(self) -> int: ... def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... def __or__(self, value: dict[_T1, _T2] | frozendict[_T1, _T2], /) -> frozendict[_KT | _T1, _VT_co | _T2]: ... @@ -1328,32 +1335,48 @@ class set(MutableSet[_T]): def __len__(self) -> int: ... def __contains__(self, o: object, /) -> bool: ... def __iter__(self) -> Iterator[_T]: ... - def __and__(self, value: AbstractSet[object], /) -> set[_T]: ... - def __iand__(self, value: AbstractSet[object], /) -> Self: ... + def __and__(self, value: set[Any] | frozenset[Any], /) -> set[_T]: ... # type: ignore[override] + @overload # type: ignore[misc] + def __iand__(self, value: set[Any] | frozenset[Any], /) -> Self: ... @overload + def __iand__(self, value: SupportsRAnd[set[_T], _R], /) -> _R: ... + + @overload # type: ignore[override] def __rand__(self, value: set[_S], /) -> set[_S]: ... @overload def __rand__(self, value: frozenset[_S], /) -> frozenset[_S]: ... - def __or__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... - def __ior__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] + def __or__(self, value: set[_S] | frozenset[_S], /) -> set[_T | _S]: ... # type: ignore[override] + @overload # type: ignore[override,misc] + def __ior__(self, value: set[_T] | frozenset[_T], /) -> Self: ... @overload + def __ior__(self, value: SupportsROr[set[_T], _R], /) -> _R: ... + + @overload # type: ignore[override] def __ror__(self, value: set[_S], /) -> set[_T | _S]: ... @overload def __ror__(self, value: frozenset[_S], /) -> frozenset[_T | _S]: ... - def __sub__(self, value: AbstractSet[object], /) -> set[_T]: ... - def __isub__(self, value: AbstractSet[object], /) -> Self: ... + def __sub__(self, value: set[Any] | frozenset[Any], /) -> set[_T]: ... # type: ignore[override] + @overload # type: ignore[misc] + def __isub__(self, value: set[Any] | frozenset[Any], /) -> Self: ... @overload + def __isub__(self, value: SupportsRSub[set[_T], _R], /) -> _R: ... + + @overload # type: ignore[override] def __rsub__(self, value: set[_S], /) -> set[_S]: ... @overload def __rsub__(self, value: frozenset[_S], /) -> frozenset[_S]: ... def __xor__(self, value: set[_S] | frozenset[_S], /) -> set[_T | _S]: ... # type: ignore[override] - def __ixor__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] + + @overload # type: ignore[override,misc] + def __ixor__(self, value: set[_T] | frozenset[_T], /) -> Self: ... + @overload + def __ixor__(self, value: SupportsRXor[set[_T], _R], /) -> _R: ... @overload # type: ignore[override] def __rxor__(self, value: set[_S], /) -> set[_T | _S]: ... @@ -1386,23 +1409,25 @@ class frozenset(AbstractSet[_T_co]): def __len__(self) -> int: ... def __contains__(self, o: object, /) -> bool: ... def __iter__(self) -> Iterator[_T_co]: ... - def __and__(self, value: AbstractSet[object], /) -> frozenset[_T_co]: ... + # The binary operators only support set and frozenset. For other operands, + # Python falls back to the operand's reflected method. + def __and__(self, value: set[Any] | frozenset[Any], /) -> frozenset[_T_co]: ... # type: ignore[override] - @overload + @overload # type: ignore[override] def __rand__(self, value: set[_S], /) -> set[_S]: ... @overload def __rand__(self, value: frozenset[_S], /) -> frozenset[_S]: ... - def __or__(self, value: AbstractSet[_S], /) -> frozenset[_T_co | _S]: ... + def __or__(self, value: set[_S] | frozenset[_S], /) -> frozenset[_T_co | _S]: ... # type: ignore[override] - @overload + @overload # type: ignore[override] def __ror__(self, value: set[_S], /) -> set[_T_co | _S]: ... @overload def __ror__(self, value: frozenset[_S], /) -> frozenset[_T_co | _S]: ... - def __sub__(self, value: AbstractSet[object], /) -> frozenset[_T_co]: ... + def __sub__(self, value: set[Any] | frozenset[Any], /) -> frozenset[_T_co]: ... # type: ignore[override] - @overload + @overload # type: ignore[override] def __rsub__(self, value: set[_S], /) -> set[_S]: ... @overload def __rsub__(self, value: frozenset[_S], /) -> frozenset[_S]: ... @@ -1474,6 +1499,7 @@ class property: fdel: Callable[[Any], None] | None = None, doc: str | None = None, ) -> None: ... + def __set_name__(self, owner: type, name: str, /) -> None: ... def getter(self, fget: Callable[[Any], Any], /) -> property: ... def setter(self, fset: Callable[[Any, Any], None], /) -> property: ... def deleter(self, fdel: Callable[[Any], None], /) -> property: ... @@ -2059,9 +2085,9 @@ def pow(base: float, exp: int, mod: None = None) -> float: ... # return type must be Any (same as complex base, complex exp), # as `float | complex` causes too many false-positive errors @overload -def pow(base: float, exp: complex | _SupportsSomeKindOfPow, mod: None = None) -> Any: ... +def pow(base: float, exp: complex | float | int | _SupportsSomeKindOfPow, mod: None = None) -> Any: ... @overload -def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = None) -> complex: ... +def pow(base: complex, exp: complex | float | int | _SupportsSomeKindOfPow, mod: None = None) -> complex: ... @overload def pow(base: _SupportsPow2[_E_contra, _T_co], exp: _E_contra, mod: None = None) -> _T_co: ... # type: ignore[overload-overlap] @overload @@ -2069,7 +2095,7 @@ def pow(base: _SupportsPow3NoneOnly[_E_contra, _T_co], exp: _E_contra, mod: None @overload def pow(base: _SupportsPow3[_E_contra, _M_contra, _T_co], exp: _E_contra, mod: _M_contra) -> _T_co: ... @overload -def pow(base: _SupportsSomeKindOfPow, exp: float, mod: None = None) -> Any: ... +def pow(base: _SupportsSomeKindOfPow, exp: float | int, mod: None = None) -> Any: ... @overload def pow(base: _SupportsSomeKindOfPow, exp: complex, mod: None = None) -> complex: ... @@ -2221,7 +2247,7 @@ if sys.version_info >= (3, 15): locals: Mapping[str, object] | None = None, fromlist: Sequence[str] | None = (), level: int = 0, - ) -> Any: ... + ) -> types.LazyImportType: ... def __build_class__(func: Callable[[], CellType | Any], name: str, /, *bases: Any, metaclass: Any = ..., **kwds: Any) -> Any: ... diff --git a/mypy/typeshed/stdlib/cmath.pyi b/mypy/typeshed/stdlib/cmath.pyi index 554eb54e2e4cc..4898b1c00232b 100644 --- a/mypy/typeshed/stdlib/cmath.pyi +++ b/mypy/typeshed/stdlib/cmath.pyi @@ -9,7 +9,7 @@ nanj: Final[complex] tau: Final[float] _F: TypeAlias = SupportsFloat | SupportsIndex -_C: TypeAlias = SupportsFloat | SupportsComplex | SupportsIndex | complex +_C: TypeAlias = SupportsComplex | SupportsFloat | SupportsIndex | complex | float | int def acos(z: _C, /) -> complex: ... def acosh(z: _C, /) -> complex: ... diff --git a/mypy/typeshed/stdlib/functools.pyi b/mypy/typeshed/stdlib/functools.pyi index 42041befd9999..25e75b4f18f01 100644 --- a/mypy/typeshed/stdlib/functools.pyi +++ b/mypy/typeshed/stdlib/functools.pyi @@ -233,7 +233,7 @@ class _SingleDispatchCallable(Generic[_T]): registry: types.MappingProxyType[Any, Callable[..., _T]] def dispatch(self, cls: Any) -> Callable[..., _T]: ... - # @fun.register(complex) + # @fun.register(complex | float | int) # def _(arg, verbose=False): ... @overload def register(self, cls: _RegType, func: None = None) -> Callable[[Callable[..., _T]], Callable[..., _T]]: ... diff --git a/mypy/typeshed/stdlib/multiprocessing/context.pyi b/mypy/typeshed/stdlib/multiprocessing/context.pyi index 925f364f1826c..e86fd71ade4d6 100644 --- a/mypy/typeshed/stdlib/multiprocessing/context.pyi +++ b/mypy/typeshed/stdlib/multiprocessing/context.pyi @@ -175,7 +175,11 @@ class DefaultContext(BaseContext): else: def __init__(self, context: BaseContext) -> None: ... - def get_start_method(self, allow_none: bool = False) -> str: ... + @overload + def get_start_method(self, allow_none: Literal[False] = False) -> str: ... + @overload + def get_start_method(self, allow_none: bool) -> str | None: ... + def get_all_start_methods(self) -> list[str]: ... _default_context: DefaultContext diff --git a/mypy/typeshed/stdlib/multiprocessing/managers.pyi b/mypy/typeshed/stdlib/multiprocessing/managers.pyi index 0727f60f10809..6768f16832ed3 100644 --- a/mypy/typeshed/stdlib/multiprocessing/managers.pyi +++ b/mypy/typeshed/stdlib/multiprocessing/managers.pyi @@ -180,11 +180,11 @@ if sys.version_info >= (3, 14): def __contains__(self, o: object, /) -> bool: ... def __iter__(self) -> Iterator[_T]: ... def __and__(self, value: AbstractSet[object], /) -> set[_T]: ... - def __iand__(self, value: AbstractSet[object], /) -> Self: ... + def __iand__(self, value: AbstractSet[object], /) -> Self: ... # type: ignore[override] def __or__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... def __ior__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] def __sub__(self, value: AbstractSet[_T | None], /) -> set[_T]: ... - def __isub__(self, value: AbstractSet[object], /) -> Self: ... + def __isub__(self, value: AbstractSet[object], /) -> Self: ... # type: ignore[override] def __xor__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... def __ixor__(self, value: AbstractSet[_T], /) -> Self: ... # type: ignore[override,misc] def __le__(self, value: AbstractSet[object], /) -> bool: ... @@ -192,9 +192,9 @@ if sys.version_info >= (3, 14): def __ge__(self, value: AbstractSet[object], /) -> bool: ... def __gt__(self, value: AbstractSet[object], /) -> bool: ... def __eq__(self, value: object, /) -> bool: ... - def __rand__(self, value: AbstractSet[object], /) -> set[_T]: ... + def __rand__(self, value: AbstractSet[object], /) -> set[_T]: ... # type: ignore[override] def __ror__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... # type: ignore[misc] - def __rsub__(self, value: AbstractSet[_T], /) -> set[_T]: ... + def __rsub__(self, value: AbstractSet[_T], /) -> set[_T]: ... # type: ignore[override] def __rxor__(self, value: AbstractSet[_S], /) -> set[_T | _S]: ... # type: ignore[override,misc] def __class_getitem__(cls, item: Any, /) -> GenericAlias: ... diff --git a/mypy/typeshed/stdlib/optparse.pyi b/mypy/typeshed/stdlib/optparse.pyi index 35dfc54a9d498..710a51c19c41b 100644 --- a/mypy/typeshed/stdlib/optparse.pyi +++ b/mypy/typeshed/stdlib/optparse.pyi @@ -29,7 +29,7 @@ SUPPRESS_HELP: Final = "SUPPRESSHELP" SUPPRESS_USAGE: Final = "SUPPRESSUSAGE" # Can return complex, float, or int depending on the option's type -def check_builtin(option: Option, opt: str, value: str) -> complex: ... +def check_builtin(option: Option, opt: str, value: str) -> complex | float | int: ... def check_choice(option: Option, opt: str, value: str) -> str: ... class OptParseError(Exception): diff --git a/mypy/typeshed/stdlib/pydoc.pyi b/mypy/typeshed/stdlib/pydoc.pyi index dd9a365dff0bc..9943aaa5e0d89 100644 --- a/mypy/typeshed/stdlib/pydoc.pyi +++ b/mypy/typeshed/stdlib/pydoc.pyi @@ -82,11 +82,11 @@ class HTMLRepr(Repr): def __init__(self) -> None: ... def escape(self, text: str) -> str: ... def repr(self, object: object) -> str: ... - def repr1(self, x: object, level: complex) -> str: ... - def repr_string(self, x: str, level: complex) -> str: ... - def repr_str(self, x: str, level: complex) -> str: ... - def repr_instance(self, x: object, level: complex) -> str: ... - def repr_unicode(self, x: AnyStr, level: complex) -> str: ... + def repr1(self, x: object, level: int) -> str: ... + def repr_string(self, x: str, level: int) -> str: ... + def repr_str(self, x: str, level: int) -> str: ... + def repr_instance(self, x: object, level: int) -> str: ... + def repr_unicode(self, x: AnyStr, level: int) -> str: ... class HTMLDoc(Doc): _repr_instance: HTMLRepr @@ -190,10 +190,10 @@ class HTMLDoc(Doc): class TextRepr(Repr): def __init__(self) -> None: ... - def repr1(self, x: object, level: complex) -> str: ... - def repr_string(self, x: str, level: complex) -> str: ... - def repr_str(self, x: str, level: complex) -> str: ... - def repr_instance(self, x: object, level: complex) -> str: ... + def repr1(self, x: object, level: int) -> str: ... + def repr_string(self, x: str, level: int) -> str: ... + def repr_str(self, x: str, level: int) -> str: ... + def repr_instance(self, x: object, level: int) -> str: ... class TextDoc(Doc): _repr_instance: TextRepr diff --git a/mypy/typeshed/stdlib/re.pyi b/mypy/typeshed/stdlib/re.pyi index 42eb751498c08..bbfa2b333c0f1 100644 --- a/mypy/typeshed/stdlib/re.pyi +++ b/mypy/typeshed/stdlib/re.pyi @@ -343,6 +343,8 @@ def subn( def escape(pattern: AnyStr) -> AnyStr: ... @overload def escape(pattern: ReadableBuffer) -> bytes: ... +@overload +def escape(pattern: str | ReadableBuffer) -> str | bytes: ... def purge() -> None: ... diff --git a/mypy/typeshed/stdlib/typing.pyi b/mypy/typeshed/stdlib/typing.pyi index 0ea21c5dd7f1e..e5e30b0c248fe 100644 --- a/mypy/typeshed/stdlib/typing.pyi +++ b/mypy/typeshed/stdlib/typing.pyi @@ -5,7 +5,18 @@ import collections # ruff: ignore[unused-import] # pyright: ignore[reportUnuse import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItem, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable +from _typeshed import ( + IdentityFunction, + ReadableBuffer, + SupportsGetItem, + SupportsGetItemViewable, + SupportsKeysAndGetItem, + SupportsRAnd, + SupportsROr, + SupportsRSub, + SupportsRXor, + Viewable, +) from abc import ABCMeta, abstractmethod from re import Match as Match, Pattern as Pattern from types import ( @@ -411,6 +422,7 @@ _FT = TypeVar("_FT", bound=Callable[..., Any] | type) # These type variables are used by the container types. _S = TypeVar("_S") +_R = TypeVar("_R") _KT = TypeVar("_KT") # Key type. _VT = TypeVar("_VT") # Value type. _T_co = TypeVar("_T_co", covariant=True) # Any type covariant containers. @@ -737,11 +749,20 @@ class AbstractSet(Collection[_T_co]): def __lt__(self, other: AbstractSet[Any], /) -> bool: ... def __gt__(self, other: AbstractSet[Any], /) -> bool: ... def __ge__(self, other: AbstractSet[Any], /) -> bool: ... + # These annotations describe what all implementations support, including + # set and frozenset, which don't use the mixin methods. While the mixin + # methods accept any iterable, set and frozenset only accept set and + # frozenset. The forward methods still accept any AbstractSet, because + # if one operand doesn't support the other, Python falls back to the + # other operand's reflected method, which always returns an AbstractSet. def __and__(self, other: AbstractSet[Any], /) -> AbstractSet[_T_co]: ... def __or__(self, other: AbstractSet[_T], /) -> AbstractSet[_T_co | _T]: ... def __sub__(self, other: AbstractSet[Any], /) -> AbstractSet[_T_co]: ... def __xor__(self, other: AbstractSet[_T], /) -> AbstractSet[_T_co | _T]: ... - def __rxor__(self, other: Iterable[_T], /) -> AbstractSet[_T_co | _T]: ... + def __rand__(self, other: AbstractSet[_T], /) -> AbstractSet[_T]: ... + def __ror__(self, other: AbstractSet[_T], /) -> AbstractSet[_T_co | _T]: ... + def __rsub__(self, other: AbstractSet[_T], /) -> AbstractSet[_T]: ... + def __rxor__(self, other: AbstractSet[_T], /) -> AbstractSet[_T_co | _T]: ... def __eq__(self, other: object, /) -> bool: ... def isdisjoint(self, other: Iterable[Any], /) -> bool: ... @@ -754,10 +775,32 @@ class MutableSet(AbstractSet[_T]): def clear(self) -> None: ... def pop(self) -> _T: ... def remove(self, value: _T, /) -> None: ... - def __ior__(self, it: AbstractSet[_T], /) -> typing_extensions.Self: ... # type: ignore[override,misc] - def __iand__(self, it: AbstractSet[Any], /) -> typing_extensions.Self: ... - def __ixor__(self, it: AbstractSet[_T], /) -> typing_extensions.Self: ... # type: ignore[override,misc] - def __isub__(self, it: AbstractSet[Any], /) -> typing_extensions.Self: ... + + # These annotations describe what all implementations support, including + # set, which doesn't use the mixin methods. While the mixin methods accept + # any iterable and always modify the set in place, set only does so for + # set and frozenset arguments. For other arguments, Python falls back to + # the argument's reflected method and rebinds the target to its result, + # which might not even be a mutable set. + @overload # type: ignore[override,misc] + def __ior__(self, it: set[_T] | frozenset[_T], /) -> typing_extensions.Self: ... + @overload + def __ior__(self, it: SupportsROr[MutableSet[_T], _R], /) -> typing_extensions.Self | _R: ... + + @overload # type: ignore[misc] + def __iand__(self, it: set[Any] | frozenset[Any], /) -> typing_extensions.Self: ... + @overload + def __iand__(self, it: SupportsRAnd[MutableSet[_T], _R], /) -> typing_extensions.Self | _R: ... + + @overload # type: ignore[override,misc] + def __ixor__(self, it: set[_T] | frozenset[_T], /) -> typing_extensions.Self: ... + @overload + def __ixor__(self, it: SupportsRXor[MutableSet[_T], _R], /) -> typing_extensions.Self | _R: ... + + @overload # type: ignore[misc] + def __isub__(self, it: set[Any] | frozenset[Any], /) -> typing_extensions.Self: ... + @overload + def __isub__(self, it: SupportsRSub[MutableSet[_T], _R], /) -> typing_extensions.Self | _R: ... class MappingView(Sized): __slots__ = ("_mapping",)