From a48e61e5c30978ac5c4c15ff6f3acf1a33a628f9 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sun, 8 Dec 2024 02:58:08 -0800 Subject: [PATCH 1/4] fix default of dict.get --- stdlib/@tests/stubtest_allowlists/common.txt | 1 - stdlib/builtins.pyi | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/@tests/stubtest_allowlists/common.txt b/stdlib/@tests/stubtest_allowlists/common.txt index 8a984022920d..2fc473315b55 100644 --- a/stdlib/@tests/stubtest_allowlists/common.txt +++ b/stdlib/@tests/stubtest_allowlists/common.txt @@ -10,7 +10,6 @@ _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 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__ diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index ad10ba9dff4c..9bfa16675859 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -1124,7 +1124,7 @@ class dict(MutableMapping[_KT, _VT]): def fromkeys(cls, iterable: Iterable[_T], value: _S, /) -> dict[_T, _S]: ... # Positional-only in dict, but not in MutableMapping @overload # type: ignore[override] - def get(self, key: _KT, /) -> _VT | None: ... + def get(self, key: _KT, default: None = None, /) -> _VT | None: ... @overload def get(self, key: _KT, default: _VT, /) -> _VT: ... @overload From f182d185b3cbddf0a5274cb2f2634926bc184c5d Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Sun, 8 Dec 2024 03:04:27 -0800 Subject: [PATCH 2/4] fix stubtest for 3.10 and 3.11 --- stdlib/importlib/metadata/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/importlib/metadata/__init__.pyi b/stdlib/importlib/metadata/__init__.pyi index 5e26f8987277..8ab7a0c4a9e8 100644 --- a/stdlib/importlib/metadata/__init__.pyi +++ b/stdlib/importlib/metadata/__init__.pyi @@ -139,7 +139,7 @@ if sys.version_info >= (3, 10) and sys.version_info < (3, 12): class Deprecated(Generic[_KT, _VT]): def __getitem__(self, name: _KT) -> _VT: ... @overload - def get(self, name: _KT) -> _VT | None: ... + def get(self, name: _KT, default: None = None) -> _VT | None: ... @overload def get(self, name: _KT, default: _T) -> _VT | _T: ... def __iter__(self) -> Iterator[_KT]: ... From 9e0e9ffbe25f638b12900167a53271e434735b1c Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Thu, 6 Mar 2025 22:43:20 -0800 Subject: [PATCH 3/4] update dict tests --- stdlib/@tests/test_cases/builtins/check_dict.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stdlib/@tests/test_cases/builtins/check_dict.py b/stdlib/@tests/test_cases/builtins/check_dict.py index dd4569eccbe5..87ce2f59f245 100644 --- a/stdlib/@tests/test_cases/builtins/check_dict.py +++ b/stdlib/@tests/test_cases/builtins/check_dict.py @@ -67,7 +67,7 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str] assert_type(d_any["key"], Any) assert_type(d_any.get("key"), Union[Any, None]) -assert_type(d_any.get("key", None), Any) +assert_type(d_any.get("key", None), Union[Any, None]) assert_type(d_any.get("key", any_value), Any) assert_type(d_any.get("key", str_value), Any) assert_type(d_any.get("key", int_value), Any) @@ -84,14 +84,14 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str] result: str result = d_any["key"] result = d_any.get("key") # type: ignore[assignment] -result = d_any.get("key", None) +result = d_any.get("key", None) # type: ignore[assignment] result = d_any.get("key", any_value) result = d_any.get("key", str_value) result = d_any.get("key", int_value) result = d_str["key"] result = d_str.get("key") # type: ignore[assignment] -result = d_str.get("key", None) # type: ignore[arg-type] +result = d_str.get("key", None) # type: ignore[assignment] result = d_str.get("key", any_value) result = d_str.get("key", str_value) result = d_str.get("key", int_value) # type: ignore[arg-type] @@ -134,11 +134,11 @@ def test8() -> str: def test9() -> str: - return d_str.get("key", None) # type: ignore[arg-type] + return d_str.get("key", None) # type: ignore[return-value] def test10() -> str: - return d_str.get("key", any_value) + return d_str.get("key", any_value) # type: ignore[no-any-return] def test11() -> str: From 5ed6e180f53f8a3fafb9ee355da69df02bc1fe85 Mon Sep 17 00:00:00 2001 From: Stephen Morton Date: Thu, 6 Mar 2025 22:50:00 -0800 Subject: [PATCH 4/4] fix for pyright --- stdlib/@tests/test_cases/builtins/check_dict.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/@tests/test_cases/builtins/check_dict.py b/stdlib/@tests/test_cases/builtins/check_dict.py index 87ce2f59f245..d89c3a27d489 100644 --- a/stdlib/@tests/test_cases/builtins/check_dict.py +++ b/stdlib/@tests/test_cases/builtins/check_dict.py @@ -92,7 +92,8 @@ def test_iterable_tuple_overload(x: Iterable[tuple[int, str]]) -> dict[int, str] result = d_str["key"] result = d_str.get("key") # type: ignore[assignment] result = d_str.get("key", None) # type: ignore[assignment] -result = d_str.get("key", any_value) +# Pyright has str | None here, see https://github.com/microsoft/pyright/discussions/9570 +result = d_str.get("key", any_value) # pyright: ignore[reportAssignmentType] result = d_str.get("key", str_value) result = d_str.get("key", int_value) # type: ignore[arg-type]