Skip to content

Commit 8b7e2fb

Browse files
committed
Remove unnecessary union in the default type in .get() and .pop() methods
Fixes python#10293.
1 parent 0aa5dd5 commit 8b7e2fb

File tree

10 files changed

+12
-21
lines changed

10 files changed

+12
-21
lines changed

stdlib/builtins.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,11 +1082,11 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
10821082
@overload
10831083
def get(self, __key: _KT) -> _VT | None: ...
10841084
@overload
1085-
def get(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ...
1085+
def get(self, __key: _KT, __default: _T) -> _VT | _T: ...
10861086
@overload
10871087
def pop(self, __key: _KT) -> _VT: ...
10881088
@overload
1089-
def pop(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ...
1089+
def pop(self, __key: _KT, __default: _T) -> _VT | _T: ...
10901090
def __len__(self) -> int: ...
10911091
def __getitem__(self, __key: _KT) -> _VT: ...
10921092
def __setitem__(self, __key: _KT, __value: _VT) -> None: ...

stdlib/collections/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
414414
@overload
415415
def pop(self, key: _KT) -> _VT: ...
416416
@overload
417-
def pop(self, key: _KT, default: _VT | _T) -> _VT | _T: ...
417+
def pop(self, key: _KT, default: _T) -> _VT | _T: ...
418418
def copy(self) -> Self: ...
419419
__copy__ = copy
420420
# All arguments to `fromkeys` are passed to `dict.fromkeys` at runtime, so the signature should be kept in line with `dict.fromkeys`.

stdlib/contextvars.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class ContextVar(Generic[_T]):
2424
def get(self) -> _T: ...
2525
if sys.version_info >= (3, 8):
2626
@overload
27-
def get(self, default: _D | _T) -> _D | _T: ...
27+
def get(self, default: _D) -> _D | _T: ...
2828
else:
2929
@overload
30-
def get(self, __default: _D | _T) -> _D | _T: ...
30+
def get(self, __default: _D) -> _D | _T: ...
3131

3232
def set(self, __value: _T) -> Token[_T]: ...
3333
def reset(self, __token: Token[_T]) -> None: ...

stdlib/multiprocessing/managers.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ class DictProxy(BaseProxy, MutableMapping[_KT, _VT]):
7676
@overload
7777
def get(self, __key: _KT) -> _VT | None: ...
7878
@overload
79-
def get(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ...
79+
def get(self, __key: _KT, __default: _T) -> _VT | _T: ...
8080
@overload
8181
def pop(self, __key: _KT) -> _VT: ...
8282
@overload
83-
def pop(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ...
83+
def pop(self, __key: _KT, __default: _T) -> _VT | _T: ...
8484
def keys(self) -> list[_KT]: ... # type: ignore[override]
8585
def items(self) -> list[tuple[_KT, _VT]]: ... # type: ignore[override]
8686
def values(self) -> list[_VT]: ... # type: ignore[override]

stdlib/typing.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,9 @@ class Mapping(Collection[_KT], Generic[_KT, _VT_co]):
624624
def __getitem__(self, __key: _KT) -> _VT_co: ...
625625
# Mixin methods
626626
@overload
627-
def get(self, __key: _KT) -> _VT_co | None: ...
627+
def get(self, key: _KT) -> _VT_co | None: ...
628628
@overload
629-
def get(self, __key: _KT, default: _VT_co | _T) -> _VT_co | _T: ...
629+
def get(self, key: _KT, default: _T) -> _VT_co | _T: ...
630630
def items(self) -> ItemsView[_KT, _VT_co]: ...
631631
def keys(self) -> KeysView[_KT]: ...
632632
def values(self) -> ValuesView[_VT_co]: ...
@@ -641,7 +641,7 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
641641
@overload
642642
def pop(self, __key: _KT) -> _VT: ...
643643
@overload
644-
def pop(self, __key: _KT, default: _VT | _T) -> _VT | _T: ...
644+
def pop(self, __key: _KT, default: _T) -> _VT | _T: ...
645645
def popitem(self) -> tuple[_KT, _VT]: ...
646646
# This overload should be allowed only if the value type is compatible with None.
647647
#

stdlib/weakref.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class WeakValueDictionary(MutableMapping[_KT, _VT]):
7474
@overload
7575
def pop(self, key: _KT) -> _VT: ...
7676
@overload
77-
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
77+
def pop(self, key: _KT, default: _T = ...) -> _VT | _T: ...
7878
if sys.version_info >= (3, 9):
7979
def __or__(self, other: Mapping[_T1, _T2]) -> WeakValueDictionary[_KT | _T1, _VT | _T2]: ...
8080
def __ror__(self, other: Mapping[_T1, _T2]) -> WeakValueDictionary[_KT | _T1, _VT | _T2]: ...
@@ -117,7 +117,7 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]):
117117
@overload
118118
def pop(self, key: _KT) -> _VT: ...
119119
@overload
120-
def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ...
120+
def pop(self, key: _KT, default: _T = ...) -> _VT | _T: ...
121121
if sys.version_info >= (3, 9):
122122
def __or__(self, other: Mapping[_T1, _T2]) -> WeakKeyDictionary[_KT | _T1, _VT | _T2]: ...
123123
def __ror__(self, other: Mapping[_T1, _T2]) -> WeakKeyDictionary[_KT | _T1, _VT | _T2]: ...

tests/stubtest_allowlists/py37.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ collections.ByteString # see comments in py3_common.txt
2828
collections.Callable
2929
collections.Mapping.__reversed__ # Set to None at runtime for a better error message
3030
configparser.ParsingError.filename
31-
contextvars.ContextVar.get
3231
dummy_threading.Condition.acquire
3332
dummy_threading.Condition.release
3433
dummy_threading.Event.isSet
@@ -68,7 +67,6 @@ collections.Generator.gi_code
6867
collections.Generator.gi_frame
6968
collections.Generator.gi_running
7069
collections.Generator.gi_yieldfrom
71-
collections.Mapping.get # Adding None to the Union messed up mypy
7270
collections.Sequence.index # Supporting None in end is not mandatory
7371

7472
# SpooledTemporaryFile implements IO except these methods before Python 3.11

tests/stubtest_allowlists/py38.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ collections.Generator.gi_code
8787
collections.Generator.gi_frame
8888
collections.Generator.gi_running
8989
collections.Generator.gi_yieldfrom
90-
collections.Mapping.get # Adding None to the Union messed up mypy
9190
collections.Sequence.index # Supporting None in end is not mandatory
9291

9392
# SpooledTemporaryFile implements IO except these methods before Python 3.11

tests/stubtest_allowlists/py39.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ collections.Generator.gi_code
9191
collections.Generator.gi_frame
9292
collections.Generator.gi_running
9393
collections.Generator.gi_yieldfrom
94-
collections.Mapping.get # Adding None to the Union messed up mypy
9594
collections.Sequence.index # Supporting None in end is not mandatory
9695

9796
# Exists at runtime, but missing from stubs

tests/stubtest_allowlists/py3_common.txt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
_csv.Dialect.__init__ # C __init__ signature is inaccurate
88
_ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed
99
_threading_local.local.__new__
10-
builtins.dict.get
1110
builtins.ellipsis # type is not exposed anywhere
1211
builtins.function
13-
collections.ChainMap.get # Adding None to the underlying Mapping Union messed up mypy
1412
collections.ChainMap.fromkeys # Runtime has *args which can really only be one argument
1513
collections.UserList.sort # Runtime has *args but will error if any are supplied
1614
configparser.SectionProxy.__getattr__ # SectionProxy can have arbitrary attributes when custom converters are used
@@ -103,9 +101,7 @@ typing.IO.__next__ # Added because IO streams are iterable. See https://github.
103101
typing.type_check_only # typing decorator that is not available at runtime
104102
unittest.mock.patch # It's a complicated overload and I haven't been able to figure out why stubtest doesn't like it
105103
urllib.request.HTTPPasswordMgrWithPriorAuth.__init__ # Args are passed as is to super, so super args are specified
106-
weakref.WeakKeyDictionary.get
107104
weakref.WeakKeyDictionary.update
108-
weakref.WeakValueDictionary.get
109105
xml.parsers.expat.expat_CAPI
110106

111107
# ==========
@@ -248,7 +244,6 @@ _collections_abc.Generator.gi_frame
248244
_collections_abc.Generator.gi_running
249245
_collections_abc.Generator.gi_yieldfrom
250246
_collections_abc.Mapping.__reversed__ # set to None at runtime for a better error message
251-
_collections_abc.Mapping.get # Adding None to the Union messed up mypy
252247
_collections_abc.Sequence.index # Supporting None in end is not mandatory
253248

254249
# Adding these reflected dunders to `typing.AbstractSet` causes a large number of false-positives. See #7414.

0 commit comments

Comments
 (0)