Fix default of dict.get (#13222)

This commit is contained in:
Stephen Morton
2025-03-09 05:44:27 -07:00
committed by GitHub
parent 0cdb5e969f
commit 846d167f51
4 changed files with 9 additions and 9 deletions
@@ -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
@@ -67,7 +67,7 @@ int_value = 1
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,15 +84,16 @@ assert_type(d_str.get("key", int_value), Union[str, int])
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]
@@ -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:
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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]: ...