Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

stdlib: Add several missing __(deep)copy__ methods #7242

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stdlib/array.pyi
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion stdlib/fractions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions stdlib/functools.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand All @@ -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: ...

Expand Down
4 changes: 4 additions & 0 deletions stdlib/weakref.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions stdlib/xml/etree/ElementTree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down