Skip to content

Commit

Permalink
Add defaultdict.__(r)or__; improve ChainMap.__(r)or__ and `UserDi…
Browse files Browse the repository at this point in the history
…ct.__(r)or__` (#10427)

Add __or__ to defaultdict

Also, add overloads with Self type to other __[r]or__ methods.
  • Loading branch information
eltoder authored Jul 11, 2023
1 parent 19992e6 commit cfc5425
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,13 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
__hash__: ClassVar[None] # type: ignore[assignment]
if sys.version_info >= (3, 9):
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
@overload
def __or__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ...
@overload
def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
@overload
def __ror__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ...
@overload
def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
Expand Down
23 changes: 22 additions & 1 deletion stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,14 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
@overload
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> UserDict[_T, _S]: ...
if sys.version_info >= (3, 9):
@overload
def __or__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
@overload
def __or__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
@overload # type: ignore[misc]
def __ror__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
@overload # type: ignore[misc]
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
# UserDict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
Expand Down Expand Up @@ -391,6 +397,15 @@ class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]):
def __missing__(self, __key: _KT) -> _VT: ...
def __copy__(self) -> Self: ...
def copy(self) -> Self: ...
if sys.version_info >= (3, 9):
@overload
def __or__(self, __value: Mapping[_KT, _VT]) -> Self: ...
@overload
def __or__(self, __value: Mapping[_T1, _T2]) -> defaultdict[_KT | _T1, _VT | _T2]: ...
@overload
def __ror__(self, __value: Mapping[_KT, _VT]) -> Self: ...
@overload
def __ror__(self, __value: Mapping[_T1, _T2]) -> defaultdict[_KT | _T1, _VT | _T2]: ...

class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
maps: list[MutableMapping[_KT, _VT]]
Expand Down Expand Up @@ -425,7 +440,13 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
@overload
def fromkeys(cls, __iterable: Iterable[_T], __value: _S) -> ChainMap[_T, _S]: ...
if sys.version_info >= (3, 9):
@overload
def __or__(self, other: Mapping[_KT, _VT]) -> Self: ...
@overload
def __or__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
@overload
def __ror__(self, other: Mapping[_KT, _VT]) -> Self: ...
@overload
def __ror__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
# ChainMap.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
Expand Down

0 comments on commit cfc5425

Please sign in to comment.