Skip to content

Commit cfc5425

Browse files
authored
Add defaultdict.__(r)or__; improve ChainMap.__(r)or__ and UserDict.__(r)or__ (#10427)
Add __or__ to defaultdict Also, add overloads with Self type to other __[r]or__ methods.
1 parent 19992e6 commit cfc5425

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

stdlib/builtins.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,13 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
11021102
__hash__: ClassVar[None] # type: ignore[assignment]
11031103
if sys.version_info >= (3, 9):
11041104
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
1105+
@overload
1106+
def __or__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ...
1107+
@overload
11051108
def __or__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
1109+
@overload
1110+
def __ror__(self, __value: Mapping[_KT, _VT]) -> dict[_KT, _VT]: ...
1111+
@overload
11061112
def __ror__(self, __value: Mapping[_T1, _T2]) -> dict[_KT | _T1, _VT | _T2]: ...
11071113
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
11081114
@overload # type: ignore[misc]

stdlib/collections/__init__.pyi

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,14 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
8383
@overload
8484
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> UserDict[_T, _S]: ...
8585
if sys.version_info >= (3, 9):
86+
@overload
87+
def __or__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
88+
@overload
8689
def __or__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
87-
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
90+
@overload # type: ignore[misc]
91+
def __ror__(self, other: UserDict[_KT, _VT] | dict[_KT, _VT]) -> Self: ...
92+
@overload # type: ignore[misc]
93+
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
8894
# UserDict.__ior__ should be kept roughly in line with MutableMapping.update()
8995
@overload # type: ignore[misc]
9096
def __ior__(self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@@ -391,6 +397,15 @@ class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]):
391397
def __missing__(self, __key: _KT) -> _VT: ...
392398
def __copy__(self) -> Self: ...
393399
def copy(self) -> Self: ...
400+
if sys.version_info >= (3, 9):
401+
@overload
402+
def __or__(self, __value: Mapping[_KT, _VT]) -> Self: ...
403+
@overload
404+
def __or__(self, __value: Mapping[_T1, _T2]) -> defaultdict[_KT | _T1, _VT | _T2]: ...
405+
@overload
406+
def __ror__(self, __value: Mapping[_KT, _VT]) -> Self: ...
407+
@overload
408+
def __ror__(self, __value: Mapping[_T1, _T2]) -> defaultdict[_KT | _T1, _VT | _T2]: ...
394409

395410
class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
396411
maps: list[MutableMapping[_KT, _VT]]
@@ -425,7 +440,13 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
425440
@overload
426441
def fromkeys(cls, __iterable: Iterable[_T], __value: _S) -> ChainMap[_T, _S]: ...
427442
if sys.version_info >= (3, 9):
443+
@overload
444+
def __or__(self, other: Mapping[_KT, _VT]) -> Self: ...
445+
@overload
428446
def __or__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
447+
@overload
448+
def __ror__(self, other: Mapping[_KT, _VT]) -> Self: ...
449+
@overload
429450
def __ror__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
430451
# ChainMap.__ior__ should be kept roughly in line with MutableMapping.update()
431452
@overload # type: ignore[misc]

0 commit comments

Comments
 (0)