Add Protocols for mapping inside dictionary view objects (#14574)

This commit is contained in:
Gatsik
2025-08-18 09:22:34 +03:00
committed by GitHub
parent 85a787bba3
commit fef5d941b0
2 changed files with 16 additions and 6 deletions
+11 -1
View File
@@ -3,7 +3,7 @@
# See the README.md file in this directory for more information.
import sys
from collections.abc import Awaitable, Callable, Iterable, Sequence, Set as AbstractSet, Sized
from collections.abc import Awaitable, Callable, Iterable, Iterator, Sequence, Set as AbstractSet, Sized
from dataclasses import Field
from os import PathLike
from types import FrameType, TracebackType
@@ -275,6 +275,16 @@ class SupportsWrite(Protocol[_T_contra]):
class SupportsFlush(Protocol):
def flush(self) -> object: ...
# Suitable for dictionary view objects
class Viewable(Protocol[_T_co]):
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[_T_co]: ...
class SupportsGetItemViewable(Protocol[_KT, _VT_co]):
def __len__(self) -> int: ...
def __iter__(self) -> Iterator[_KT]: ...
def __getitem__(self, key: _KT, /) -> _VT_co: ...
# Unfortunately PEP 688 does not allow us to distinguish read-only
# from writable buffers. We use these aliases for readability for now.
# Perhaps a future extension of the buffer protocol will allow us to
+5 -5
View File
@@ -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, SupportsKeysAndGetItem
from _typeshed import IdentityFunction, ReadableBuffer, SupportsGetItemViewable, SupportsKeysAndGetItem, Viewable
from abc import ABCMeta, abstractmethod
from re import Match as Match, Pattern as Pattern
from types import (
@@ -703,11 +703,11 @@ class MutableSet(AbstractSet[_T]):
def __isub__(self, it: AbstractSet[Any]) -> typing_extensions.Self: ...
class MappingView(Sized):
def __init__(self, mapping: Mapping[Any, Any]) -> None: ... # undocumented
def __init__(self, mapping: Sized) -> None: ... # undocumented
def __len__(self) -> int: ...
class ItemsView(MappingView, AbstractSet[tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]):
def __init__(self, mapping: Mapping[_KT_co, _VT_co]) -> None: ... # undocumented
def __init__(self, mapping: SupportsGetItemViewable[_KT_co, _VT_co]) -> None: ... # undocumented
def __and__(self, other: Iterable[Any]) -> set[tuple[_KT_co, _VT_co]]: ...
def __rand__(self, other: Iterable[_T]) -> set[_T]: ...
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,
def __rxor__(self, other: Iterable[_T]) -> set[tuple[_KT_co, _VT_co] | _T]: ...
class KeysView(MappingView, AbstractSet[_KT_co]):
def __init__(self, mapping: Mapping[_KT_co, Any]) -> None: ... # undocumented
def __init__(self, mapping: Viewable[_KT_co]) -> None: ... # undocumented
def __and__(self, other: Iterable[Any]) -> set[_KT_co]: ...
def __rand__(self, other: Iterable[_T]) -> set[_T]: ...
def __contains__(self, key: object) -> bool: ...
@@ -733,7 +733,7 @@ class KeysView(MappingView, AbstractSet[_KT_co]):
def __rxor__(self, other: Iterable[_T]) -> set[_KT_co | _T]: ...
class ValuesView(MappingView, Collection[_VT_co]):
def __init__(self, mapping: Mapping[Any, _VT_co]) -> None: ... # undocumented
def __init__(self, mapping: SupportsGetItemViewable[Any, _VT_co]) -> None: ... # undocumented
def __contains__(self, value: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...