diff --git a/stdlib/@tests/test_cases/builtins/check_dict.py b/stdlib/@tests/test_cases/builtins/check_dict.py index 2944730c7c67..fe74ad49408e 100644 --- a/stdlib/@tests/test_cases/builtins/check_dict.py +++ b/stdlib/@tests/test_cases/builtins/check_dict.py @@ -4,6 +4,10 @@ from typing import Any, Dict, Generic, Iterable, Mapping, TypeVar, Union from typing_extensions import Self, assert_type +################################################################### +# Note: tests for `dict.update()` are in `check_MutableMapping.py`. +################################################################### + # These do follow `__init__` overloads order: # mypy and pyright have different opinions about this one: # mypy raises: 'Need type annotation for "bad"' diff --git a/stdlib/@tests/test_cases/typing/check_MutableMapping.py b/stdlib/@tests/test_cases/typing/check_MutableMapping.py index d4f951060b35..1e9d747fc8f6 100644 --- a/stdlib/@tests/test_cases/typing/check_MutableMapping.py +++ b/stdlib/@tests/test_cases/typing/check_MutableMapping.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Any, Union +from typing import Any, Hashable, Sequence, Union from typing_extensions import assert_type @@ -30,6 +30,15 @@ def check_update_method__str_key() -> None: d.update([("", "")]) # type: ignore +def test_keywords_allowed_on_dict_update_where_key_type_is_str_supertype( + a: dict[object, Any], b: dict[Hashable, Any], c: dict[Sequence[str], Any], d: dict[str, Any] +) -> None: + a.update(keyword_args_are_accepted="whatever") + b.update(here_too="whooo") + c.update(and_here="hooray") + d.update(also_here="yay") + + def check_setdefault_method() -> None: d: dict[int, str] = {} d2: dict[int, str | None] = {} diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index 63c159346fd1..7d048a13c288 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -5,7 +5,7 @@ import collections # noqa: F401 # pyright: ignore[reportUnusedImport] import sys import typing_extensions from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable +from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItem, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable from abc import ABCMeta, abstractmethod from re import Match as Match, Pattern as Pattern from types import ( @@ -801,13 +801,13 @@ class MutableMapping(Mapping[_KT, _VT]): @overload def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ... @overload - def update(self: Mapping[str, _VT], m: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ... + def update(self: SupportsGetItem[str, _VT], m: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ... @overload def update(self, m: Iterable[tuple[_KT, _VT]], /) -> None: ... @overload - def update(self: Mapping[str, _VT], m: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ... + def update(self: SupportsGetItem[str, _VT], m: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ... @overload - def update(self: Mapping[str, _VT], **kwargs: _VT) -> None: ... + def update(self: SupportsGetItem[str, _VT], **kwargs: _VT) -> None: ... Text = str diff --git a/stubs/WebOb/webob/multidict.pyi b/stubs/WebOb/webob/multidict.pyi index 84815c7cfb65..bc5cd2c76138 100644 --- a/stubs/WebOb/webob/multidict.pyi +++ b/stubs/WebOb/webob/multidict.pyi @@ -1,4 +1,4 @@ -from _typeshed import SupportsKeysAndGetItem +from _typeshed import SupportsGetItem, SupportsKeysAndGetItem from _typeshed.wsgi import WSGIEnvironment from collections.abc import Collection, Iterable, Iterator, MutableMapping from typing import Literal, Protocol, TypeVar, overload, type_check_only @@ -73,11 +73,11 @@ class MultiDict(MutableMapping[_KT, _VT]): def pop(self, key: _KT, default: _T) -> _VT | _T: ... def popitem(self) -> tuple[_KT, _VT]: ... @overload # type: ignore[override] - def update(self: MultiDict[str, _VT], **kwargs: _VT) -> None: ... + def update(self: SupportsGetItem[str, _VT], **kwargs: _VT) -> None: ... @overload def update(self, m: Collection[tuple[_KT, _VT]], /) -> None: ... @overload - def update(self: MultiDict[str, _VT], m: Collection[tuple[str, _VT]], /, **kwargs: _VT) -> None: ... + def update(self: SupportsGetItem[str, _VT], m: Collection[tuple[str, _VT]], /, **kwargs: _VT) -> None: ... @overload def extend(self, other: _SupportsItemsWithIterableResult[_KT, _VT]) -> None: ... @overload