From d56754cdf48f81d14e97bbc63c0d5105e4787f73 Mon Sep 17 00:00:00 2001 From: Sergei Lebedev Date: Fri, 9 Jun 2023 21:24:31 +0100 Subject: [PATCH] Remove unnecessary union in the default type in .get() and .pop() methods Fixes #10293. --- stdlib/builtins.pyi | 4 ++-- stdlib/collections/__init__.pyi | 2 +- stdlib/contextvars.pyi | 4 ++-- stdlib/multiprocessing/managers.pyi | 4 ++-- stdlib/typing.pyi | 6 +++--- stdlib/weakref.pyi | 4 ++-- tests/stubtest_allowlists/py37.txt | 2 -- tests/stubtest_allowlists/py38.txt | 1 - tests/stubtest_allowlists/py39.txt | 1 - tests/stubtest_allowlists/py3_common.txt | 5 ----- 10 files changed, 12 insertions(+), 21 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index b3871776128a..751b41addef7 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1082,11 +1082,11 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def get(self, __key: _KT) -> _VT | None: ... @overload - def get(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ... + def get(self, __key: _KT, __default: _T) -> _VT | _T: ... @overload def pop(self, __key: _KT) -> _VT: ... @overload - def pop(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ... + def pop(self, __key: _KT, __default: _T) -> _VT | _T: ... def __len__(self) -> int: ... def __getitem__(self, __key: _KT) -> _VT: ... def __setitem__(self, __key: _KT, __value: _VT) -> None: ... diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index d5ca17c749eb..882d60aa003d 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -414,7 +414,7 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def pop(self, key: _KT) -> _VT: ... @overload - def pop(self, key: _KT, default: _VT | _T) -> _VT | _T: ... + def pop(self, key: _KT, default: _T) -> _VT | _T: ... def copy(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`. diff --git a/stdlib/contextvars.pyi b/stdlib/contextvars.pyi index ef6e2700e667..886f940b6f69 100644 --- a/stdlib/contextvars.pyi +++ b/stdlib/contextvars.pyi @@ -24,10 +24,10 @@ class ContextVar(Generic[_T]): def get(self) -> _T: ... if sys.version_info >= (3, 8): @overload - def get(self, default: _D | _T) -> _D | _T: ... + def get(self, default: _D) -> _D | _T: ... else: @overload - def get(self, __default: _D | _T) -> _D | _T: ... + def get(self, __default: _D) -> _D | _T: ... def set(self, __value: _T) -> Token[_T]: ... def reset(self, __token: Token[_T]) -> None: ... diff --git a/stdlib/multiprocessing/managers.pyi b/stdlib/multiprocessing/managers.pyi index 27a903fb9987..8b967564f577 100644 --- a/stdlib/multiprocessing/managers.pyi +++ b/stdlib/multiprocessing/managers.pyi @@ -76,11 +76,11 @@ class DictProxy(BaseProxy, MutableMapping[_KT, _VT]): @overload def get(self, __key: _KT) -> _VT | None: ... @overload - def get(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ... + def get(self, __key: _KT, __default: _T) -> _VT | _T: ... @overload def pop(self, __key: _KT) -> _VT: ... @overload - def pop(self, __key: _KT, __default: _VT | _T) -> _VT | _T: ... + def pop(self, __key: _KT, __default: _T) -> _VT | _T: ... def keys(self) -> list[_KT]: ... # type: ignore[override] def items(self) -> list[tuple[_KT, _VT]]: ... # type: ignore[override] def values(self) -> list[_VT]: ... # type: ignore[override] diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index db042dc440ae..54eead573fc7 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -624,9 +624,9 @@ class Mapping(Collection[_KT], Generic[_KT, _VT_co]): def __getitem__(self, __key: _KT) -> _VT_co: ... # Mixin methods @overload - def get(self, __key: _KT) -> _VT_co | None: ... + def get(self, key: _KT) -> _VT_co | None: ... @overload - def get(self, __key: _KT, default: _VT_co | _T) -> _VT_co | _T: ... + def get(self, key: _KT, default: _T) -> _VT_co | _T: ... def items(self) -> ItemsView[_KT, _VT_co]: ... def keys(self) -> KeysView[_KT]: ... def values(self) -> ValuesView[_VT_co]: ... @@ -641,7 +641,7 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]): @overload def pop(self, __key: _KT) -> _VT: ... @overload - def pop(self, __key: _KT, default: _VT | _T) -> _VT | _T: ... + def pop(self, __key: _KT, default: _T) -> _VT | _T: ... def popitem(self) -> tuple[_KT, _VT]: ... # This overload should be allowed only if the value type is compatible with None. # diff --git a/stdlib/weakref.pyi b/stdlib/weakref.pyi index 13f48fe85a8d..6ce7cba0398b 100644 --- a/stdlib/weakref.pyi +++ b/stdlib/weakref.pyi @@ -74,7 +74,7 @@ class WeakValueDictionary(MutableMapping[_KT, _VT]): @overload def pop(self, key: _KT) -> _VT: ... @overload - def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ... + def pop(self, key: _KT, default: _T = ...) -> _VT | _T: ... if sys.version_info >= (3, 9): def __or__(self, other: Mapping[_T1, _T2]) -> WeakValueDictionary[_KT | _T1, _VT | _T2]: ... def __ror__(self, other: Mapping[_T1, _T2]) -> WeakValueDictionary[_KT | _T1, _VT | _T2]: ... @@ -117,7 +117,7 @@ class WeakKeyDictionary(MutableMapping[_KT, _VT]): @overload def pop(self, key: _KT) -> _VT: ... @overload - def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ... + def pop(self, key: _KT, default: _T = ...) -> _VT | _T: ... if sys.version_info >= (3, 9): def __or__(self, other: Mapping[_T1, _T2]) -> WeakKeyDictionary[_KT | _T1, _VT | _T2]: ... def __ror__(self, other: Mapping[_T1, _T2]) -> WeakKeyDictionary[_KT | _T1, _VT | _T2]: ... diff --git a/tests/stubtest_allowlists/py37.txt b/tests/stubtest_allowlists/py37.txt index 3a7413858aed..6323c66c5327 100644 --- a/tests/stubtest_allowlists/py37.txt +++ b/tests/stubtest_allowlists/py37.txt @@ -28,7 +28,6 @@ collections.ByteString # see comments in py3_common.txt collections.Callable collections.Mapping.__reversed__ # Set to None at runtime for a better error message configparser.ParsingError.filename -contextvars.ContextVar.get dummy_threading.Condition.acquire dummy_threading.Condition.release dummy_threading.Event.isSet @@ -68,7 +67,6 @@ collections.Generator.gi_code collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom -collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory # SpooledTemporaryFile implements IO except these methods before Python 3.11 diff --git a/tests/stubtest_allowlists/py38.txt b/tests/stubtest_allowlists/py38.txt index 1e9ca5638c6a..a7ced82bb311 100644 --- a/tests/stubtest_allowlists/py38.txt +++ b/tests/stubtest_allowlists/py38.txt @@ -87,7 +87,6 @@ collections.Generator.gi_code collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom -collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory # SpooledTemporaryFile implements IO except these methods before Python 3.11 diff --git a/tests/stubtest_allowlists/py39.txt b/tests/stubtest_allowlists/py39.txt index 277efd02f0a6..53420e1dad35 100644 --- a/tests/stubtest_allowlists/py39.txt +++ b/tests/stubtest_allowlists/py39.txt @@ -91,7 +91,6 @@ collections.Generator.gi_code collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom -collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory # Exists at runtime, but missing from stubs diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index ccc4eda51f7c..94b347cfc490 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -7,10 +7,8 @@ _csv.Dialect.__init__ # C __init__ signature is inaccurate _ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed _threading_local.local.__new__ -builtins.dict.get builtins.ellipsis # type is not exposed anywhere builtins.function -collections.ChainMap.get # Adding None to the underlying Mapping Union messed up mypy collections.ChainMap.fromkeys # Runtime has *args which can really only be one argument collections.UserList.sort # Runtime has *args but will error if any are supplied 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. typing.type_check_only # typing decorator that is not available at runtime unittest.mock.patch # It's a complicated overload and I haven't been able to figure out why stubtest doesn't like it urllib.request.HTTPPasswordMgrWithPriorAuth.__init__ # Args are passed as is to super, so super args are specified -weakref.WeakKeyDictionary.get weakref.WeakKeyDictionary.update -weakref.WeakValueDictionary.get xml.parsers.expat.expat_CAPI # ========== @@ -248,7 +244,6 @@ _collections_abc.Generator.gi_frame _collections_abc.Generator.gi_running _collections_abc.Generator.gi_yieldfrom _collections_abc.Mapping.__reversed__ # set to None at runtime for a better error message -_collections_abc.Mapping.get # Adding None to the Union messed up mypy _collections_abc.Sequence.index # Supporting None in end is not mandatory # Adding these reflected dunders to `typing.AbstractSet` causes a large number of false-positives. See #7414.