Skip to content

Remove unnecessary union in the default type in .get() and .pop() methods #10294

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 2 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
2 changes: 1 addition & 1 deletion stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/contextvars.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
4 changes: 2 additions & 2 deletions stdlib/multiprocessing/managers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 3 additions & 3 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand All @@ -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.
#
Expand Down
4 changes: 2 additions & 2 deletions stdlib/weakref.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand Down Expand Up @@ -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]: ...
Expand Down
2 changes: 0 additions & 2 deletions tests/stubtest_allowlists/py37.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/stubtest_allowlists/py38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/stubtest_allowlists/py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions tests/stubtest_allowlists/py3_common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

# ==========
Expand Down Expand Up @@ -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.
Expand Down