Skip to content

fix default of dict.get #13222

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

Merged
merged 7 commits into from
Mar 9, 2025
Merged
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
1 change: 0 additions & 1 deletion stdlib/@tests/stubtest_allowlists/common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

# Please keep sorted alphabetically

builtins.dict.get
collections\.ChainMap\.fromkeys # https://github.com/python/mypy/issues/17023
http.client.HTTPConnection.response_class # the actual type at runtime is abc.ABCMeta
importlib.abc.Loader.exec_module # See Lib/importlib/_abc.py. Might be defined for backwards compatibility
Expand Down
13 changes: 7 additions & 6 deletions stdlib/@tests/test_cases/builtins/check_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -84,15 +84,16 @@ 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", any_value)
result = d_str.get("key", None) # type: ignore[assignment]
# 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]

Expand Down Expand Up @@ -134,11 +135,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:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/importlib/metadata/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ...
Expand Down