From 211b595591a0528200a32e453fcdf16dbaae144e Mon Sep 17 00:00:00 2001 From: Neil Mitchell Date: Fri, 27 Jun 2025 10:22:04 +0100 Subject: [PATCH 1/3] Make Mapping.get(default) less constrained At the moment we say that `default` can either be the same as the value type, or anything it wants to be. Given: ``` def f(mapping: Mapping[str, str]): reveal_type(mapping.get("x", "y")) ``` The `_T` is entirely unconstrained, and thus `str | int` is a perfectly valid type. This change makes it obviously `str | str` which collapses to `str` correctly. This fix makes Pyrefly happier, although Pyright already seems to deal with it, but this doesn't harm Pyright. --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 79ab9eee924f..84fced377312 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -746,7 +746,7 @@ class Mapping(Collection[_KT], Generic[_KT, _VT_co]): @overload 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]: ... From 3138b3fea8e55e1cea8a3a69c108049bdee432c5 Mon Sep 17 00:00:00 2001 From: Neil Mitchell Date: Fri, 27 Jun 2025 10:29:46 +0100 Subject: [PATCH 2/3] Remove unused stub ignore --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 130c6d2b8285..89fff2aacd8c 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -184,7 +184,6 @@ _collections_abc.AsyncGenerator.aclose _collections_abc.AsyncIterator.__anext__ _collections_abc.Callable # Typing-related weirdness -_collections_abc.Mapping.get # Adding None to the Union messed up mypy _collections_abc.Sequence.index # Supporting None in end is not mandatory _ctypes.CFuncPtr # stubtest erroneously thinks it can't be subclassed From 2335a542e6f66d5516602a00b2971b81d16941f6 Mon Sep 17 00:00:00 2001 From: Neil Mitchell Date: Fri, 27 Jun 2025 10:30:16 +0100 Subject: [PATCH 3/3] Update py39.txt --- stdlib/@tests/stubtest_allowlists/py39.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 37c1ec2ad171..db681ff7835e 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -20,7 +20,6 @@ collections.AsyncGenerator.asend # async at runtime, deliberately not in the st collections.AsyncIterator.__anext__ # async at runtime, deliberately not in the stub, see #7491 collections.ByteString # see comments in py3_common.txt collections.Callable -collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory # ===========