GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `__getitem__` iterable support for builtins, collections, itertools by mixilchenko · Pull Request #8597 · python/typeshed

__getitem__ iterable support for builtins, collections, itertools #8597

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.

Sign up for GitHub

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

Closed
mixilchenko wants to merge 5 commits into python:master from mixilchenko:get-item-iterable

Uh oh!

There was an error while loading. Please reload this page.

Closed

__getitem__ iterable support for builtins, collections, itertools #8597

Changes from 1 commit
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
e1d57a0
Introduce `IterableOrSupportsIntGetItem` protocol
mixilchenko Aug 23, 2022
7b4b38c
Support `builtins` iteration with `__getitem__`
mixilchenko Aug 23, 2022
37dc024
Support `itertools` iteration with `__getitem__`
mixilchenko Aug 23, 2022
f923643
Support `collections` iteration with `__getitem__`
mixilchenko Aug 23, 2022
0e5af3f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 23, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Support collections iteration with __getitem__
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@mixilchenko
mixilchenko committed Aug 23, 2022
commit f923643395916c732cf481ef14770f8733d5a193

There are no files selected for viewing

63 changes: 31 additions & 32 deletions 63 stdlib/collections/__init__.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from _collections_abc import dict_items, dict_keys, dict_values
from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from _typeshed import IterableOrSupportsIntGetItem, Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from typing import Any, Generic, NoReturn, TypeVar, overload
from typing_extensions import SupportsIndex, final

Expand All @@ -11,7 +11,6 @@ if sys.version_info >= (3, 10):
from collections.abc import (
Callable,
ItemsView,
Iterable,
Iterator,
KeysView,
Mapping,
Expand All @@ -38,11 +37,11 @@ _VT_co = TypeVar("_VT_co", covariant=True)
# namedtuple is special-cased in the type checker; the initializer is ignored.
def namedtuple(
typename: str,
field_names: str | Iterable[str],
field_names: str | IterableOrSupportsIntGetItem[str],
*,
rename: bool = ...,
module: str | None = ...,
defaults: Iterable[Any] | None = ...,
defaults: IterableOrSupportsIntGetItem[Any] | None = ...,
) -> type[tuple[Any, ...]]: ...

class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
Expand All @@ -57,11 +56,11 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
@overload
def __init__(self: UserDict[str, _VT], __dict: SupportsKeysAndGetItem[str, _VT], **kwargs: _VT) -> None: ...
@overload
def __init__(self, __iterable: Iterable[tuple[_KT, _VT]]) -> None: ...
def __init__(self, __iterable: IterableOrSupportsIntGetItem[tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(self: UserDict[str, _VT], __iterable: Iterable[tuple[str, _VT]], **kwargs: _VT) -> None: ...
def __init__(self: UserDict[str, _VT], __iterable: IterableOrSupportsIntGetItem[tuple[str, _VT]], **kwargs: _VT) -> None: ...
@overload
def __init__(self: UserDict[str, str], __iterable: Iterable[list[str]]) -> None: ...
def __init__(self: UserDict[str, str], __iterable: IterableOrSupportsIntGetItem[list[str]]) -> None: ...
def __len__(self) -> int: ...
def __getitem__(self, key: _KT) -> _VT: ...
def __setitem__(self, key: _KT, item: _VT) -> None: ...
Expand All @@ -76,25 +75,25 @@ class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
# See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963.
@classmethod
@overload
def fromkeys(cls, iterable: Iterable[_T], value: None = ...) -> UserDict[_T, Any | None]: ...
def fromkeys(cls, iterable: IterableOrSupportsIntGetItem[_T], value: None = ...) -> UserDict[_T, Any | None]: ...
@classmethod
@overload
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> UserDict[_T, _S]: ...
def fromkeys(cls, iterable: IterableOrSupportsIntGetItem[_T], value: _S) -> UserDict[_T, _S]: ...
if sys.version_info >= (3, 9):
def __or__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ...
def __ror__(self, other: UserDict[_T1, _T2] | dict[_T1, _T2]) -> UserDict[_KT | _T1, _VT | _T2]: ... # type: ignore[misc]
# UserDict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self: Self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@overload
def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...
def __ior__(self: Self, other: IterableOrSupportsIntGetItem[tuple[_KT, _VT]]) -> Self: ...

class UserList(MutableSequence[_T]):
data: list[_T]
@overload
def __init__(self, initlist: None = ...) -> None: ...
@overload
def __init__(self, initlist: Iterable[_T]) -> None: ...
def __init__(self, initlist: IterableOrSupportsIntGetItem[_T]) -> None: ...
def __lt__(self, other: list[_T] | UserList[_T]) -> bool: ...
def __le__(self, other: list[_T] | UserList[_T]) -> bool: ...
def __gt__(self, other: list[_T] | UserList[_T]) -> bool: ...
Expand All @@ -109,11 +108,11 @@ class UserList(MutableSequence[_T]):
@overload
def __setitem__(self, i: SupportsIndex, item: _T) -> None: ...
@overload
def __setitem__(self, i: slice, item: Iterable[_T]) -> None: ...
def __setitem__(self, i: slice, item: IterableOrSupportsIntGetItem[_T]) -> None: ...
def __delitem__(self, i: SupportsIndex | slice) -> None: ...
def __add__(self: Self, other: Iterable[_T]) -> Self: ...
def __radd__(self: Self, other: Iterable[_T]) -> Self: ...
def __iadd__(self: Self, other: Iterable[_T]) -> Self: ...
def __add__(self: Self, other: IterableOrSupportsIntGetItem[_T]) -> Self: ...
def __radd__(self: Self, other: IterableOrSupportsIntGetItem[_T]) -> Self: ...
def __iadd__(self: Self, other: IterableOrSupportsIntGetItem[_T]) -> Self: ...
def __mul__(self: Self, n: int) -> Self: ...
def __rmul__(self: Self, n: int) -> Self: ...
def __imul__(self: Self, n: int) -> Self: ...
Expand All @@ -131,7 +130,7 @@ class UserList(MutableSequence[_T]):
def sort(self: UserList[SupportsRichComparisonT], *, key: None = ..., reverse: bool = ...) -> None: ...
@overload
def sort(self, *, key: Callable[[_T], SupportsRichComparison], reverse: bool = ...) -> None: ...
def extend(self, other: Iterable[_T]) -> None: ...
def extend(self, other: IterableOrSupportsIntGetItem[_T]) -> None: ...

class UserString(Sequence[UserString]):
data: str
Expand Down Expand Up @@ -187,7 +186,7 @@ class UserString(Sequence[UserString]):
def istitle(self) -> bool: ...
def isupper(self) -> bool: ...
def isascii(self) -> bool: ...
def join(self, seq: Iterable[str]) -> str: ...
def join(self, seq: IterableOrSupportsIntGetItem[str]) -> str: ...
def ljust(self: Self, width: int, *args: Any) -> Self: ...
def lower(self: Self) -> Self: ...
def lstrip(self: Self, chars: str | None = ...) -> Self: ...
Expand Down Expand Up @@ -225,13 +224,13 @@ class deque(MutableSequence[_T], Generic[_T]):
@overload
def __init__(self, *, maxlen: int | None = ...) -> None: ...
@overload
def __init__(self, iterable: Iterable[_T], maxlen: int | None = ...) -> None: ...
def __init__(self, iterable: IterableOrSupportsIntGetItem[_T], maxlen: int | None = ...) -> None: ...
def append(self, __x: _T) -> None: ...
def appendleft(self, __x: _T) -> None: ...
def copy(self: Self) -> Self: ...
def count(self, __x: _T) -> int: ...
def extend(self, __iterable: Iterable[_T]) -> None: ...
def extendleft(self, __iterable: Iterable[_T]) -> None: ...
def extend(self, __iterable: IterableOrSupportsIntGetItem[_T]) -> None: ...
def extendleft(self, __iterable: IterableOrSupportsIntGetItem[_T]) -> None: ...
def insert(self, __i: int, __x: _T) -> None: ...
def index(self, __x: _T, __start: int = ..., __stop: int = ...) -> int: ...
def pop(self) -> _T: ... # type: ignore[override]
Expand All @@ -246,7 +245,7 @@ class deque(MutableSequence[_T], Generic[_T]):
def __delitem__(self, __i: SupportsIndex) -> None: ... # type: ignore[override]
def __contains__(self, __o: object) -> bool: ...
def __reduce__(self: Self) -> tuple[type[Self], tuple[()], None, Iterator[_T]]: ...
def __iadd__(self: Self, __iterable: Iterable[_T]) -> Self: ...
def __iadd__(self: Self, __iterable: IterableOrSupportsIntGetItem[_T]) -> Self: ...
def __add__(self: Self, __other: Self) -> Self: ...
def __mul__(self: Self, __other: int) -> Self: ...
def __imul__(self: Self, __other: int) -> Self: ...
Expand All @@ -265,7 +264,7 @@ class Counter(dict[_T, int], Generic[_T]):
@overload
def __init__(self, __mapping: SupportsKeysAndGetItem[_T, int]) -> None: ...
@overload
def __init__(self, __iterable: Iterable[_T]) -> None: ...
def __init__(self, __iterable: IterableOrSupportsIntGetItem[_T]) -> None: ...
def copy(self: Self) -> Self: ...
def elements(self) -> Iterator[_T]: ...
def most_common(self, n: int | None = ...) -> list[tuple[_T, int]]: ...
Expand All @@ -276,17 +275,17 @@ class Counter(dict[_T, int], Generic[_T]):
@overload
def subtract(self, __mapping: Mapping[_T, int]) -> None: ...
@overload
def subtract(self, __iterable: Iterable[_T]) -> None: ...
def subtract(self, __iterable: IterableOrSupportsIntGetItem[_T]) -> None: ...
# Unlike dict.update(), use Mapping instead of SupportsKeysAndGetItem for the first overload
# (source code does an `isinstance(other, Mapping)` check)
#
# The second overload is also deliberately different to dict.update()
# (if it were `Iterable[_T] | Iterable[tuple[_T, int]]`,
# (if it were `IterableOrSupportsIntGetItem[_T] | IterableOrSupportsIntGetItem[tuple[_T, int]]`,
# the tuples would be added as keys, breaking type safety)
@overload # type: ignore[override]
def update(self, __m: Mapping[_T, int], **kwargs: int) -> None: ...
@overload
def update(self, __m: Iterable[_T], **kwargs: int) -> None: ...
def update(self, __m: IterableOrSupportsIntGetItem[_T], **kwargs: int) -> None: ...
@overload
def update(self, __m: None = ..., **kwargs: int) -> None: ...
def __missing__(self, key: _T) -> int: ...
Expand Down Expand Up @@ -352,10 +351,10 @@ class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
# See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963.
@classmethod
@overload
def fromkeys(cls, iterable: Iterable[_T], value: None = ...) -> OrderedDict[_T, Any | None]: ...
def fromkeys(cls, iterable: IterableOrSupportsIntGetItem[_T], value: None = ...) -> OrderedDict[_T, Any | None]: ...
@classmethod
@overload
def fromkeys(cls, iterable: Iterable[_T], value: _S) -> OrderedDict[_T, _S]: ...
def fromkeys(cls, iterable: IterableOrSupportsIntGetItem[_T], value: _S) -> OrderedDict[_T, _S]: ...
# Keep OrderedDict.setdefault in line with MutableMapping.setdefault, modulo positional-only differences.
@overload
def setdefault(self: OrderedDict[_KT, _T | None], key: _KT) -> _T | None: ...
Expand All @@ -382,12 +381,12 @@ class defaultdict(dict[_KT, _VT], Generic[_KT, _VT]):
**kwargs: _VT,
) -> None: ...
@overload
def __init__(self, __default_factory: Callable[[], _VT] | None, __iterable: Iterable[tuple[_KT, _VT]]) -> None: ...
def __init__(self, __default_factory: Callable[[], _VT] | None, __iterable: IterableOrSupportsIntGetItem[tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(
self: defaultdict[str, _VT],
__default_factory: Callable[[], _VT] | None,
__iterable: Iterable[tuple[str, _VT]],
__iterable: IterableOrSupportsIntGetItem[tuple[str, _VT]],
**kwargs: _VT,
) -> None: ...
def __missing__(self, __key: _KT) -> _VT: ...
Expand Down Expand Up @@ -418,15 +417,15 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
# All arguments to `fromkeys` are passed to `dict.fromkeys` at runtime, so the signature should be kept in line with `dict.fromkeys`.
@classmethod
@overload
def fromkeys(cls, iterable: Iterable[_T], __value: None = ...) -> ChainMap[_T, Any | None]: ...
def fromkeys(cls, iterable: IterableOrSupportsIntGetItem[_T], __value: None = ...) -> ChainMap[_T, Any | None]: ...
@classmethod
@overload
def fromkeys(cls, __iterable: Iterable[_T], __value: _S) -> ChainMap[_T, _S]: ...
def fromkeys(cls, __iterable: IterableOrSupportsIntGetItem[_T], __value: _S) -> ChainMap[_T, _S]: ...
if sys.version_info >= (3, 9):
def __or__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
def __ror__(self, other: Mapping[_T1, _T2]) -> ChainMap[_KT | _T1, _VT | _T2]: ...
# ChainMap.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self: Self, other: SupportsKeysAndGetItem[_KT, _VT]) -> Self: ...
@overload
def __ior__(self: Self, other: Iterable[tuple[_KT, _VT]]) -> Self: ...
def __ior__(self: Self, other: IterableOrSupportsIntGetItem[tuple[_KT, _VT]]) -> Self: ...
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.