From 353c07a081afa240e65624f503b960eaa9432698 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sun, 8 Dec 2024 15:37:02 -0800 Subject: [PATCH 1/5] Alternate method of handling dict.get default value --- stdlib/@tests/stubtest_allowlists/common.txt | 4 ++-- stdlib/builtins.pyi | 2 ++ stdlib/importlib/metadata/__init__.pyi | 4 ++++ stdlib/typing.pyi | 6 +++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 8a984022920d..e7fbf80cd6c8 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -10,7 +10,7 @@ _collections_abc.AsyncGenerator.ag_frame _collections_abc.AsyncGenerator.ag_running asyncio.BaseEventLoop.subprocess_exec # BaseEventLoop adds several parameters and stubtest fails on the difference if we add them asyncio.base_events.BaseEventLoop.subprocess_exec # BaseEventLoop adds several parameters and stubtest fails on the difference if we add them -builtins.dict.get +# builtins.dict.get collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023 configparser.SectionProxy.__getattr__ # SectionProxy can have arbitrary attributes when custom converters are used configparser.SectionProxy.getboolean # SectionProxy get functions are set in __init__ @@ -190,7 +190,7 @@ _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.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. diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index ad10ba9dff4c..0438d8eb69f1 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1128,6 +1128,8 @@ class dict(MutableMapping[_KT, _VT]): @overload def get(self, key: _KT, default: _VT, /) -> _VT: ... @overload + def get(self, key: _KT, default: None, /) -> _VT | None: ... + @overload def get(self, key: _KT, default: _T, /) -> _VT | _T: ... @overload def pop(self, key: _KT, /) -> _VT: ... diff --git a/stdlib/importlib/metadata/__init__.pyi b/stdlib/importlib/metadata/__init__.pyi index 5e26f8987277..7dfd0b7796b4 100644 --- a/stdlib/importlib/metadata/__init__.pyi +++ b/stdlib/importlib/metadata/__init__.pyi @@ -141,6 +141,10 @@ if sys.version_info >= (3, 10) and sys.version_info < (3, 12): @overload def get(self, name: _KT) -> _VT | None: ... @overload + def get(self, name: _KT, default: _VT) -> _VT: ... + @overload + def get(self, name: _KT, default: None) -> _VT | None: ... + @overload def get(self, name: _KT, default: _T) -> _VT | _T: ... def __iter__(self) -> Iterator[_KT]: ... def __contains__(self, *args: object) -> bool: ... diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 741e7b8a3167..81f6dd4320ed 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -698,7 +698,11 @@ 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: _VT_co) -> _VT_co: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant variable as parameter + @overload + def get(self, key: _KT, /, default: None) -> _VT_co | None: ... + @overload + 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 f50a1f17347a7e0c117e98323fc71e71cbc6c664 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sun, 8 Dec 2024 15:41:31 -0800 Subject: [PATCH 2/5] allowlist --- stdlib/@tests/stubtest_allowlists/py38.txt | 2 +- stdlib/@tests/stubtest_allowlists/py39.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 6d98ee251773..9bda4e8e0399 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -78,7 +78,7 @@ collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom collections.Mapping.__reversed__ # Set to None at runtime for a better error message -collections.Mapping.get # Adding None to the Union messed up mypy +# collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 0ef22057acce..926a944fcc2d 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -67,7 +67,7 @@ collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom collections.Mapping.__reversed__ # Set to None at runtime for a better error message -collections.Mapping.get # Adding None to the Union messed up mypy +# collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs From d9a81c36dbb1db0ce9ad07561749e9a3adaa0fd4 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sun, 8 Dec 2024 15:43:34 -0800 Subject: [PATCH 3/5] allowlist --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/@tests/stubtest_allowlists/py310.txt | 1 + stdlib/@tests/stubtest_allowlists/py311.txt | 1 + stdlib/@tests/stubtest_allowlists/py312.txt | 1 + stdlib/@tests/stubtest_allowlists/py313.txt | 1 + 5 files changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index e7fbf80cd6c8..705639cbac8e 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -190,7 +190,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. diff --git a/stdlib/@tests/stubtest_allowlists/py310.txt b/stdlib/@tests/stubtest_allowlists/py310.txt index 93e595839d40..2c364c6439b6 100644 --- a/stdlib/@tests/stubtest_allowlists/py310.txt +++ b/stdlib/@tests/stubtest_allowlists/py310.txt @@ -17,6 +17,7 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. +_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py311.txt b/stdlib/@tests/stubtest_allowlists/py311.txt index 4841231d6eb6..a0daac4528de 100644 --- a/stdlib/@tests/stubtest_allowlists/py311.txt +++ b/stdlib/@tests/stubtest_allowlists/py311.txt @@ -38,6 +38,7 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. +_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py312.txt b/stdlib/@tests/stubtest_allowlists/py312.txt index 9ae160e7e819..267d6db22daf 100644 --- a/stdlib/@tests/stubtest_allowlists/py312.txt +++ b/stdlib/@tests/stubtest_allowlists/py312.txt @@ -37,6 +37,7 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. +_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py313.txt b/stdlib/@tests/stubtest_allowlists/py313.txt index 4049026a4c37..e98e726d0d07 100644 --- a/stdlib/@tests/stubtest_allowlists/py313.txt +++ b/stdlib/@tests/stubtest_allowlists/py313.txt @@ -60,6 +60,7 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. +_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault From db80c97f72d4378c66d9b15242842a569afb4429 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 9 Dec 2024 10:48:28 -0800 Subject: [PATCH 4/5] leave Mapping alone --- stdlib/@tests/stubtest_allowlists/common.txt | 1 + stdlib/@tests/stubtest_allowlists/py310.txt | 1 - stdlib/@tests/stubtest_allowlists/py311.txt | 1 - stdlib/@tests/stubtest_allowlists/py312.txt | 1 - stdlib/@tests/stubtest_allowlists/py313.txt | 1 - stdlib/@tests/stubtest_allowlists/py38.txt | 2 +- stdlib/@tests/stubtest_allowlists/py39.txt | 2 +- stdlib/typing.pyi | 6 +----- 8 files changed, 4 insertions(+), 11 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 705639cbac8e..a4b3c46a4178 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -190,6 +190,7 @@ _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. diff --git a/stdlib/@tests/stubtest_allowlists/py310.txt b/stdlib/@tests/stubtest_allowlists/py310.txt index 2c364c6439b6..93e595839d40 100644 --- a/stdlib/@tests/stubtest_allowlists/py310.txt +++ b/stdlib/@tests/stubtest_allowlists/py310.txt @@ -17,7 +17,6 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. -_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py311.txt b/stdlib/@tests/stubtest_allowlists/py311.txt index a0daac4528de..4841231d6eb6 100644 --- a/stdlib/@tests/stubtest_allowlists/py311.txt +++ b/stdlib/@tests/stubtest_allowlists/py311.txt @@ -38,7 +38,6 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. -_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py312.txt b/stdlib/@tests/stubtest_allowlists/py312.txt index 267d6db22daf..9ae160e7e819 100644 --- a/stdlib/@tests/stubtest_allowlists/py312.txt +++ b/stdlib/@tests/stubtest_allowlists/py312.txt @@ -37,7 +37,6 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. -_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py313.txt b/stdlib/@tests/stubtest_allowlists/py313.txt index e98e726d0d07..4049026a4c37 100644 --- a/stdlib/@tests/stubtest_allowlists/py313.txt +++ b/stdlib/@tests/stubtest_allowlists/py313.txt @@ -60,7 +60,6 @@ _collections_abc.Generator.send _collections_abc.Generator.throw # These are not positional-only at runtime, but we treat them as positional-only to match dict. -_collections_abc.Mapping.get _collections_abc.MutableMapping.pop _collections_abc.MutableMapping.setdefault diff --git a/stdlib/@tests/stubtest_allowlists/py38.txt b/stdlib/@tests/stubtest_allowlists/py38.txt index 9bda4e8e0399..6d98ee251773 100644 --- a/stdlib/@tests/stubtest_allowlists/py38.txt +++ b/stdlib/@tests/stubtest_allowlists/py38.txt @@ -78,7 +78,7 @@ collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom collections.Mapping.__reversed__ # Set to None at runtime for a better error message -# collections.Mapping.get # Adding None to the Union messed up mypy +collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs diff --git a/stdlib/@tests/stubtest_allowlists/py39.txt b/stdlib/@tests/stubtest_allowlists/py39.txt index 926a944fcc2d..0ef22057acce 100644 --- a/stdlib/@tests/stubtest_allowlists/py39.txt +++ b/stdlib/@tests/stubtest_allowlists/py39.txt @@ -67,7 +67,7 @@ collections.Generator.gi_frame collections.Generator.gi_running collections.Generator.gi_yieldfrom collections.Mapping.__reversed__ # Set to None at runtime for a better error message -# collections.Mapping.get # Adding None to the Union messed up mypy +collections.Mapping.get # Adding None to the Union messed up mypy collections.Sequence.index # Supporting None in end is not mandatory xxsubtype # module missing from the stubs diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 81f6dd4320ed..741e7b8a3167 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -698,11 +698,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) -> _VT_co: ... # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant variable as parameter - @overload - def get(self, key: _KT, /, default: None) -> _VT_co | None: ... - @overload - def get(self, key: _KT, /, default: _T) -> _VT_co | _T: ... + def get(self, key: _KT, /, default: _VT_co | _T) -> _VT_co | _T: ... def items(self) -> ItemsView[_KT, _VT_co]: ... def keys(self) -> KeysView[_KT]: ... def values(self) -> ValuesView[_VT_co]: ... From 4b9399fec07ecce7d463a2914d1757240ee6e2ff Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Mon, 9 Dec 2024 10:52:01 -0800 Subject: [PATCH 5/5] stubtest --- stdlib/importlib/metadata/__init__.pyi | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stdlib/importlib/metadata/__init__.pyi b/stdlib/importlib/metadata/__init__.pyi index 7dfd0b7796b4..44c0bc4d91b7 100644 --- a/stdlib/importlib/metadata/__init__.pyi +++ b/stdlib/importlib/metadata/__init__.pyi @@ -141,10 +141,10 @@ if sys.version_info >= (3, 10) and sys.version_info < (3, 12): @overload def get(self, name: _KT) -> _VT | None: ... @overload - def get(self, name: _KT, default: _VT) -> _VT: ... - @overload - def get(self, name: _KT, default: None) -> _VT | None: ... - @overload + # def get(self, name: _KT, default: _VT) -> _VT: ... + # @overload + # def get(self, name: _KT, default: None) -> _VT | None: ... + # @overload def get(self, name: _KT, default: _T) -> _VT | _T: ... def __iter__(self) -> Iterator[_KT]: ... def __contains__(self, *args: object) -> bool: ...