Skip to content

Add Protocols for mapping inside dictionary view objects #14574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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: object) -> bool: ...
Expand All @@ -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: ...
Expand All @@ -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]: ...

Expand Down
Loading