Skip to content

Commit fef5d94

Browse files
authored
Add Protocols for mapping inside dictionary view objects (#14574)
1 parent 85a787b commit fef5d94

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See the README.md file in this directory for more information.
44

55
import sys
6-
from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized
6+
from collections.abc import Awaitable, Callable, Iterable, Iterator, Sequence, Set as AbstractSet, Sized
77
from dataclasses import Field
88
from os import PathLike
99
from types import FrameType, TracebackType
@@ -275,6 +275,16 @@ class SupportsWrite(Protocol[_T_contra]):
275275
class SupportsFlush(Protocol):
276276
def flush(self) -> object: ...
277277

278+
# Suitable for dictionary view objects
279+
class Viewable(Protocol[_T_co]):
280+
def __len__(self) -> int: ...
281+
def __iter__(self) -> Iterator[_T_co]: ...
282+
283+
class SupportsGetItemViewable(Protocol[_KT, _VT_co]):
284+
def __len__(self) -> int: ...
285+
def __iter__(self) -> Iterator[_KT]: ...
286+
def __getitem__(self, key: _KT, /) -> _VT_co: ...
287+
278288
# Unfortunately PEP 688 does not allow us to distinguish read-only
279289
# from writable buffers. We use these aliases for readability for now.
280290
# Perhaps a future extension of the buffer protocol will allow us to

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: tuple[object, object]) -> bool: ... # type: ignore[override]
@@ -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)