Skip to content

Commit 1840a0a

Browse files
committed
Add Protocols for mapping inside dictionary view objects
1 parent 1e537d6 commit 1840a0a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,7 @@ if sys.version_info >= (3, 14):
377377
# These return annotations, which can be arbitrary objects
378378
AnnotateFunc: TypeAlias = Callable[[Format], dict[str, AnnotationForm]]
379379
EvaluateFunc: TypeAlias = Callable[[Format], AnnotationForm]
380+
381+
# Suitable for dictionary view objects
382+
class Viewable(Sized, Iterable[_KT_co], Protocol): ...
383+
class SupportsGetItemViewable(Viewable[_KT], SupportsGetItem[_KT, _VT_co], Protocol): ...

stdlib/typing.pyi

Lines changed: 5 additions & 5 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, SupportsKeysAndGetItem
8+
from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable
99
from abc import ABCMeta, abstractmethod
1010
from re import Match as Match, Pattern as Pattern
1111
from types import (
@@ -703,11 +703,11 @@ class MutableSet(AbstractSet[_T]):
703703
def __isub__(self, it: AbstractSet[Any]) -> typing_extensions.Self: ...
704704

705705
class MappingView(Sized):
706-
def __init__(self, mapping: Mapping[Any, Any]) -> None: ... # undocumented
706+
def __init__(self, mapping: Sized) -> None: ... # undocumented
707707
def __len__(self) -> int: ...
708708

709709
class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
710-
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
710+
def __init__(self, mapping: SupportsGetItemViewable[_KT_co, _VT_co]) -> None: ... # undocumented
711711
def __and__(self, other: Iterable[Any]) -> set[tuple[_KT_co, _VT_co]]: ...
712712
def __rand__(self, other: Iterable[_T]) -> set[_T]: ...
713713
def __contains__(self, item: object) -> bool: ...
@@ -720,7 +720,7 @@ class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co,
720720
def __rxor__(self, other: Iterable[_T]) -> set[tuple[_KT_co, _VT_co] | _T]: ...
721721

722722
class KeysView(MappingView, AbstractSet[_KT_co]):
723-
def __init__(self, mapping: Mapping[_KT_co, Any]) -> None: ... # undocumented
723+
def __init__(self, mapping: Viewable[_KT_co]) -> None: ... # undocumented
724724
def __and__(self, other: Iterable[Any]) -> set[_KT_co]: ...
725725
def __rand__(self, other: Iterable[_T]) -> set[_T]: ...
726726
def __contains__(self, key: object) -> bool: ...
@@ -733,7 +733,7 @@ class KeysView(MappingView, AbstractSet[_KT_co]):
733733
def __rxor__(self, other: Iterable[_T]) -> set[_KT_co | _T]: ...
734734

735735
class ValuesView(MappingView, Collection[_VT_co]):
736-
def __init__(self, mapping: Mapping[Any, _VT_co]) -> None: ... # undocumented
736+
def __init__(self, mapping: SupportsGetItemViewable[Any, _VT_co]) -> None: ... # undocumented
737737
def __contains__(self, value: object) -> bool: ...
738738
def __iter__(self) -> Iterator[_VT_co]: ...
739739

0 commit comments

Comments
 (0)