Skip to content

Commit 8a56044

Browse files
authored
Loosen Mapping.update() overloads a little (#14593)
1 parent ab0968b commit 8a56044

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

stdlib/@tests/test_cases/builtins/check_dict.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from typing import Any, Dict, Generic, Iterable, Mapping, TypeVar, Union
55
from typing_extensions import Self, assert_type
66

7+
###################################################################
8+
# Note: tests for `dict.update()` are in `check_MutableMapping.py`.
9+
###################################################################
10+
711
# These do follow `__init__` overloads order:
812
# mypy and pyright have different opinions about this one:
913
# mypy raises: 'Need type annotation for "bad"'

stdlib/@tests/test_cases/typing/check_MutableMapping.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Union
3+
from typing import Any, Hashable, Sequence, Union
44
from typing_extensions import assert_type
55

66

@@ -30,6 +30,15 @@ def check_update_method__str_key() -> None:
3030
d.update([("", "")]) # type: ignore
3131

3232

33+
def test_keywords_allowed_on_dict_update_where_key_type_is_str_supertype(
34+
a: dict[object, Any], b: dict[Hashable, Any], c: dict[Sequence[str], Any], d: dict[str, Any]
35+
) -> None:
36+
a.update(keyword_args_are_accepted="whatever")
37+
b.update(here_too="whooo")
38+
c.update(and_here="hooray")
39+
d.update(also_here="yay")
40+
41+
3342
def check_setdefault_method() -> None:
3443
d: dict[int, str] = {}
3544
d2: dict[int, str | None] = {}

stdlib/typing.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import collections # noqa: F401 # pyright: ignore[reportUnusedImport]
55
import sys
66
import typing_extensions
77
from _collections_abc import dict_items, dict_keys, dict_values
8-
from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable
8+
from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItem, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable
99
from abc import ABCMeta, abstractmethod
1010
from re import Match as Match, Pattern as Pattern
1111
from types import (
@@ -801,13 +801,13 @@ class MutableMapping(Mapping[_KT, _VT]):
801801
@overload
802802
def update(self, m: SupportsKeysAndGetItem[_KT, _VT], /) -> None: ...
803803
@overload
804-
def update(self: Mapping[str, _VT], m: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ...
804+
def update(self: SupportsGetItem[str, _VT], m: SupportsKeysAndGetItem[str, _VT], /, **kwargs: _VT) -> None: ...
805805
@overload
806806
def update(self, m: Iterable[tuple[_KT, _VT]], /) -> None: ...
807807
@overload
808-
def update(self: Mapping[str, _VT], m: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
808+
def update(self: SupportsGetItem[str, _VT], m: Iterable[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
809809
@overload
810-
def update(self: Mapping[str, _VT], **kwargs: _VT) -> None: ...
810+
def update(self: SupportsGetItem[str, _VT], **kwargs: _VT) -> None: ...
811811

812812
Text = str
813813

stubs/WebOb/webob/multidict.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from _typeshed import SupportsKeysAndGetItem
1+
from _typeshed import SupportsGetItem, SupportsKeysAndGetItem
22
from _typeshed.wsgi import WSGIEnvironment
33
from collections.abc import Collection, Iterable, Iterator, MutableMapping
44
from typing import Literal, Protocol, TypeVar, overload, type_check_only
@@ -73,11 +73,11 @@ class MultiDict(MutableMapping[_KT, _VT]):
7373
def pop(self, key: _KT, default: _T) -> _VT | _T: ...
7474
def popitem(self) -> tuple[_KT, _VT]: ...
7575
@overload # type: ignore[override]
76-
def update(self: MultiDict[str, _VT], **kwargs: _VT) -> None: ...
76+
def update(self: SupportsGetItem[str, _VT], **kwargs: _VT) -> None: ...
7777
@overload
7878
def update(self, m: Collection[tuple[_KT, _VT]], /) -> None: ...
7979
@overload
80-
def update(self: MultiDict[str, _VT], m: Collection[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
80+
def update(self: SupportsGetItem[str, _VT], m: Collection[tuple[str, _VT]], /, **kwargs: _VT) -> None: ...
8181
@overload
8282
def extend(self, other: _SupportsItemsWithIterableResult[_KT, _VT]) -> None: ...
8383
@overload

0 commit comments

Comments
 (0)