diff --git a/stdlib/array.pyi b/stdlib/array.pyi index f4071834f7a2..460b12a0641c 100644 --- a/stdlib/array.pyi +++ b/stdlib/array.pyi @@ -1,6 +1,6 @@ import sys from _typeshed import Self -from typing import BinaryIO, Generic, Iterable, MutableSequence, TypeVar, Union, overload +from typing import Any, BinaryIO, Generic, Iterable, MutableSequence, TypeVar, Union, overload from typing_extensions import Literal, SupportsIndex _IntTypeCode = Literal["b", "B", "h", "H", "i", "I", "l", "L", "q", "Q"] @@ -68,5 +68,7 @@ class array(MutableSequence[_T], Generic[_T]): def __lt__(self, __other: array[_T]) -> bool: ... def __mul__(self, __n: int) -> array[_T]: ... def __rmul__(self, __n: int) -> array[_T]: ... + def __copy__(self) -> array[_T]: ... + def __deepcopy__(self, __unused: Any) -> array[_T]: ... ArrayType = array diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 0c110f0753b4..05f5e95ba172 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -57,6 +57,9 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def __iter__(self) -> Iterator[_KT]: ... def __contains__(self, key: object) -> bool: ... def copy(self: Self) -> Self: ... + if sys.version_info >= (3, 7): + def __copy__(self: Self) -> Self: ... + # `UserDict.fromkeys` has the same semantics as `dict.fromkeys`, so should be kept in line with `dict.fromkeys`. # TODO: Much like `dict.fromkeys`, the true signature of `UserDict.fromkeys` is inexpressible in the current type system. # See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963. @@ -105,6 +108,9 @@ class UserList(MutableSequence[_T]): def pop(self, i: int = ...) -> _T: ... def remove(self, item: _T) -> None: ... def copy(self: Self) -> Self: ... + if sys.version_info >= (3, 7): + def __copy__(self: Self) -> Self: ... + def count(self, item: _T) -> int: ... # All arguments are passed to `list.index` at runtime, so the signature should be kept in line with `list.index`. def index(self, item: _T, __start: SupportsIndex = ..., __stop: SupportsIndex = ...) -> int: ... @@ -375,6 +381,7 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ... def copy(self: Self) -> Self: ... + __copy__ = copy # All arguments to `fromkeys` are passed to `dict.fromkeys` at runtime, so the signature should be kept in line with `dict.fromkeys`. @classmethod @overload diff --git a/stdlib/fractions.pyi b/stdlib/fractions.pyi index 5f404b0151c7..b33526c4f42d 100644 --- a/stdlib/fractions.pyi +++ b/stdlib/fractions.pyi @@ -2,7 +2,7 @@ import sys from _typeshed import Self from decimal import Decimal from numbers import Integral, Rational, Real -from typing import Union, overload +from typing import Any, Union, overload from typing_extensions import Literal _ComparableNum = Union[int, float, Decimal, Real] @@ -135,6 +135,8 @@ class Fraction(Rational): def __le__(self, other: _ComparableNum) -> bool: ... def __ge__(self, other: _ComparableNum) -> bool: ... def __bool__(self) -> bool: ... + def __copy__(self: Self) -> Self: ... + def __deepcopy__(self: Self, memo: Any) -> Self: ... if sys.version_info >= (3, 11): def __int__(self) -> int: ... # Not actually defined within fractions.py, but provides more useful diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index c80f91a4a863..0edb258cb4d0 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -29,6 +29,8 @@ class _lru_cache_wrapper(Generic[_T]): def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ... def cache_info(self) -> _CacheInfo: ... def cache_clear(self) -> None: ... + def __copy__(self) -> _lru_cache_wrapper[_T]: ... + def __deepcopy__(self, __memo: Any) -> _lru_cache_wrapper[_T]: ... if sys.version_info >= (3, 8): @overload diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index b6edd3ac8ba6..17062f1db3e2 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -635,6 +635,8 @@ class Match(Generic[AnyStr]): def __getitem__(self, __key: _Literal[0]) -> AnyStr: ... @overload def __getitem__(self, __key: int | str) -> AnyStr | Any: ... + def __copy__(self) -> Match[AnyStr]: ... + def __deepcopy__(self, __memo: Any) -> Match[AnyStr]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... @@ -658,6 +660,8 @@ class Pattern(Generic[AnyStr]): def subn(self, repl: AnyStr, string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... @overload def subn(self, repl: Callable[[Match[AnyStr]], AnyStr], string: AnyStr, count: int = ...) -> tuple[AnyStr, int]: ... + def __copy__(self) -> Pattern[AnyStr]: ... + def __deepcopy__(self, __memo: Any) -> Pattern[AnyStr]: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 761016905a09..adf493bad3bd 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -46,6 +46,8 @@ class WeakValueDictionary(MutableMapping[_KT, _VT]): def __contains__(self, o: object) -> bool: ... def __iter__(self) -> Iterator[_KT]: ... def copy(self) -> WeakValueDictionary[_KT, _VT]: ... + __copy__ = copy + def __deepcopy__(self: Self, memo: Any) -> Self: ... # These are incompatible with Mapping def keys(self) -> Iterator[_KT]: ... # type: ignore[override] def values(self) -> Iterator[_VT]: ... # type: ignore[override] @@ -84,6 +86,8 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]): def __contains__(self, o: object) -> bool: ... def __iter__(self) -> Iterator[_KT]: ... def copy(self) -> WeakKeyDictionary[_KT, _VT]: ... + __copy__ = copy + def __deepcopy__(self: Self, memo: Any) -> Self: ... # These are incompatible with Mapping def keys(self) -> Iterator[_KT]: ... # type: ignore[override] def values(self) -> Iterator[_VT]: ... # type: ignore[override] diff --git a/stdlib/xml/etree/ElementTree.pyi b/stdlib/xml/etree/ElementTree.pyi index 1f845389089e..70873562728e 100644 --- a/stdlib/xml/etree/ElementTree.pyi +++ b/stdlib/xml/etree/ElementTree.pyi @@ -85,9 +85,12 @@ class Element(MutableSequence[Element]): def iterfind(self, path: str, namespaces: dict[str, str] | None = ...) -> Generator[Element, None, None]: ... def itertext(self) -> Generator[str, None, None]: ... def keys(self) -> KeysView[str]: ... + # makeelement returns the type of self in Python impl, but not in C impl def makeelement(self, __tag: str, __attrib: dict[str, str]) -> Element: ... def remove(self, __subelement: Element) -> None: ... def set(self, __key: str, __value: str) -> None: ... + def __copy__(self) -> Element: ... # returns the type of self in Python impl, but not in C impl + def __deepcopy__(self, __memo: Any) -> Element: ... # Only exists in C impl def __delitem__(self, __i: SupportsIndex | slice) -> None: ... @overload def __getitem__(self, __i: SupportsIndex) -> Element: ...