Skip to content
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

Replace plain slice type hints with IndexSlice. #13007

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions stdlib/_ctypes.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import ReadableBuffer, WriteableBuffer
from _typeshed import IndexSlice, ReadableBuffer, WriteableBuffer
from abc import abstractmethod
from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
from ctypes import CDLL, ArgumentError as ArgumentError, c_void_p
Expand Down Expand Up @@ -96,7 +96,7 @@ class _Pointer(_PointerLike, _CData, Generic[_CT]):
@overload
def __getitem__(self, key: int, /) -> Any: ...
@overload
def __getitem__(self, key: slice, /) -> list[Any]: ...
def __getitem__(self, key: IndexSlice, /) -> list[Any]: ...
def __setitem__(self, key: int, value: Any, /) -> None: ...

@overload
Expand Down Expand Up @@ -193,11 +193,11 @@ class Array(_CData, Generic[_CT]):
@overload
def __getitem__(self, key: int, /) -> Any: ...
@overload
def __getitem__(self, key: slice, /) -> list[Any]: ...
def __getitem__(self, key: IndexSlice, /) -> list[Any]: ...
@overload
def __setitem__(self, key: int, value: Any, /) -> None: ...
@overload
def __setitem__(self, key: slice, value: Iterable[Any], /) -> None: ...
def __setitem__(self, key: IndexSlice, value: Iterable[Any], /) -> None: ...
def __iter__(self) -> Iterator[Any]: ...
# Can't inherit from Sized because the metaclass conflict between
# Sized and _CData prevents using _CDataMeta.
Expand Down
8 changes: 4 additions & 4 deletions stdlib/_operator.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import SupportsGetItem
from _typeshed import IndexSlice, SupportsGetItem
from collections.abc import Callable, Container, Iterable, MutableMapping, MutableSequence, Sequence
from operator import attrgetter as attrgetter, itemgetter as itemgetter, methodcaller as methodcaller
from typing import Any, AnyStr, Protocol, SupportsAbs, SupportsIndex, TypeVar, overload
Expand Down Expand Up @@ -75,18 +75,18 @@ def countOf(a: Iterable[object], b: object, /) -> int: ...
@overload
def delitem(a: MutableSequence[Any], b: SupportsIndex, /) -> None: ...
@overload
def delitem(a: MutableSequence[Any], b: slice, /) -> None: ...
def delitem(a: MutableSequence[Any], b: IndexSlice, /) -> None: ...
@overload
def delitem(a: MutableMapping[_K, Any], b: _K, /) -> None: ...
@overload
def getitem(a: Sequence[_T], b: slice, /) -> Sequence[_T]: ...
def getitem(a: Sequence[_T], b: IndexSlice, /) -> Sequence[_T]: ...
@overload
def getitem(a: SupportsGetItem[_K, _V], b: _K, /) -> _V: ...
def indexOf(a: Iterable[_T], b: _T, /) -> int: ...
@overload
def setitem(a: MutableSequence[_T], b: SupportsIndex, c: _T, /) -> None: ...
@overload
def setitem(a: MutableSequence[_T], b: slice, c: Sequence[_T], /) -> None: ...
def setitem(a: MutableSequence[_T], b: IndexSlice, c: Sequence[_T], /) -> None: ...
@overload
def setitem(a: MutableMapping[_K, _V], b: _K, c: _V, /) -> None: ...
def length_hint(obj: object, default: int = 0, /) -> int: ...
Expand Down
7 changes: 5 additions & 2 deletions stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ MaybeNone: TypeAlias = Any # stable
# def foo(x: int | None | _SentinelType = ...) -> None: ...
sentinel: Any

IndexSlice: TypeAlias = slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]
IntSlice: TypeAlias = slice[int | None, int | None, int | None]

# stable
class IdentityFunction(Protocol):
def __call__(self, x: _T, /) -> _T: ...
Expand Down Expand Up @@ -281,15 +284,15 @@ WriteableBuffer: TypeAlias = Buffer
ReadableBuffer: TypeAlias = Buffer # stable

class SliceableBuffer(Buffer, Protocol):
def __getitem__(self, slice: slice, /) -> Sequence[int]: ...
def __getitem__(self, slice: IndexSlice, /) -> Sequence[int]: ...

class IndexableBuffer(Buffer, Protocol):
def __getitem__(self, i: int, /) -> int: ...

class SupportsGetItemBuffer(SliceableBuffer, IndexableBuffer, Protocol):
def __contains__(self, x: Any, /) -> bool: ...
@overload
def __getitem__(self, slice: slice, /) -> Sequence[int]: ...
def __getitem__(self, slice: IndexSlice, /) -> Sequence[int]: ...
@overload
def __getitem__(self, i: int, /) -> int: ...

Expand Down
8 changes: 4 additions & 4 deletions stdlib/array.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
from _typeshed import ReadableBuffer, SupportsRead, SupportsWrite
from _typeshed import IndexSlice, ReadableBuffer, SupportsRead, SupportsWrite
from collections.abc import Iterable

# pytype crashes if array inherits from collections.abc.MutableSequence instead of typing.MutableSequence
Expand Down Expand Up @@ -66,12 +66,12 @@ class array(MutableSequence[_T]):
@overload
def __getitem__(self, key: SupportsIndex, /) -> _T: ...
@overload
def __getitem__(self, key: slice, /) -> array[_T]: ...
def __getitem__(self, key: IndexSlice, /) -> array[_T]: ...
@overload # type: ignore[override]
def __setitem__(self, key: SupportsIndex, value: _T, /) -> None: ...
@overload
def __setitem__(self, key: slice, value: array[_T], /) -> None: ...
def __delitem__(self, key: SupportsIndex | slice, /) -> None: ...
def __setitem__(self, key: IndexSlice, value: array[_T], /) -> None: ...
def __delitem__(self, key: SupportsIndex | IndexSlice, /) -> None: ...
def __add__(self, value: array[_T], /) -> array[_T]: ...
def __eq__(self, value: object, /) -> bool: ...
def __ge__(self, value: array[_T], /) -> bool: ...
Expand Down
27 changes: 14 additions & 13 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ from _typeshed import (
ConvertibleToFloat,
ConvertibleToInt,
FileDescriptorOrPath,
IndexSlice,
MaybeNone,
OpenBinaryMode,
OpenBinaryModeReading,
Expand Down Expand Up @@ -594,9 +595,9 @@ class str(Sequence[str]):
def __eq__(self, value: object, /) -> bool: ...
def __ge__(self, value: str, /) -> bool: ...
@overload
def __getitem__(self: LiteralString, key: SupportsIndex | slice, /) -> LiteralString: ...
def __getitem__(self: LiteralString, key: SupportsIndex | IndexSlice, /) -> LiteralString: ...
@overload
def __getitem__(self, key: SupportsIndex | slice, /) -> str: ... # type: ignore[misc]
def __getitem__(self, key: SupportsIndex | IndexSlice, /) -> str: ... # type: ignore[misc]
def __gt__(self, value: str, /) -> bool: ...
def __hash__(self) -> int: ...
@overload
Expand Down Expand Up @@ -702,7 +703,7 @@ class bytes(Sequence[int]):
@overload
def __getitem__(self, key: SupportsIndex, /) -> int: ...
@overload
def __getitem__(self, key: slice, /) -> bytes: ...
def __getitem__(self, key: IndexSlice, /) -> bytes: ...
def __add__(self, value: ReadableBuffer, /) -> bytes: ...
def __mul__(self, value: SupportsIndex, /) -> bytes: ...
def __rmul__(self, value: SupportsIndex, /) -> bytes: ...
Expand Down Expand Up @@ -808,12 +809,12 @@ class bytearray(MutableSequence[int]):
@overload
def __getitem__(self, key: SupportsIndex, /) -> int: ...
@overload
def __getitem__(self, key: slice, /) -> bytearray: ...
def __getitem__(self, key: IndexSlice, /) -> bytearray: ...
@overload
def __setitem__(self, key: SupportsIndex, value: SupportsIndex, /) -> None: ...
@overload
def __setitem__(self, key: slice, value: Iterable[SupportsIndex] | bytes, /) -> None: ...
def __delitem__(self, key: SupportsIndex | slice, /) -> None: ...
def __setitem__(self, key: IndexSlice, value: Iterable[SupportsIndex] | bytes, /) -> None: ...
def __delitem__(self, key: SupportsIndex | IndexSlice, /) -> None: ...
def __add__(self, value: ReadableBuffer, /) -> bytearray: ...
# The superclass wants us to accept Iterable[int], but that fails at runtime.
def __iadd__(self, value: ReadableBuffer, /) -> Self: ... # type: ignore[override]
Expand Down Expand Up @@ -879,14 +880,14 @@ class memoryview(Sequence[_I]):
@overload
def __getitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...], /) -> _I: ...
@overload
def __getitem__(self, key: slice, /) -> memoryview[_I]: ...
def __getitem__(self, key: IndexSlice, /) -> memoryview[_I]: ...
def __contains__(self, x: object, /) -> bool: ...
def __iter__(self) -> Iterator[_I]: ...
def __len__(self) -> int: ...
def __eq__(self, value: object, /) -> bool: ...
def __hash__(self) -> int: ...
@overload
def __setitem__(self, key: slice, value: ReadableBuffer, /) -> None: ...
def __setitem__(self, key: IndexSlice, value: ReadableBuffer, /) -> None: ...
@overload
def __setitem__(self, key: SupportsIndex | tuple[SupportsIndex, ...], value: _I, /) -> None: ...
if sys.version_info >= (3, 10):
Expand Down Expand Up @@ -974,7 +975,7 @@ class tuple(Sequence[_T_co]):
@overload
def __getitem__(self, key: SupportsIndex, /) -> _T_co: ...
@overload
def __getitem__(self, key: slice, /) -> tuple[_T_co, ...]: ...
def __getitem__(self, key: IndexSlice, /) -> tuple[_T_co, ...]: ...
def __iter__(self) -> Iterator[_T_co]: ...
def __lt__(self, value: tuple[_T_co, ...], /) -> bool: ...
def __le__(self, value: tuple[_T_co, ...], /) -> bool: ...
Expand Down Expand Up @@ -1051,12 +1052,12 @@ class list(MutableSequence[_T]):
@overload
def __getitem__(self, i: SupportsIndex, /) -> _T: ...
@overload
def __getitem__(self, s: slice, /) -> list[_T]: ...
def __getitem__(self, s: IndexSlice, /) -> list[_T]: ...
@overload
def __setitem__(self, key: SupportsIndex, value: _T, /) -> None: ...
@overload
def __setitem__(self, key: slice, value: Iterable[_T], /) -> None: ...
def __delitem__(self, key: SupportsIndex | slice, /) -> None: ...
def __setitem__(self, key: IndexSlice, value: Iterable[_T], /) -> None: ...
def __delitem__(self, key: SupportsIndex | IndexSlice, /) -> None: ...
# Overloading looks unnecessary, but is needed to work around complex mypy problems
@overload
def __add__(self, value: list[_T], /) -> list[_T]: ...
Expand Down Expand Up @@ -1257,7 +1258,7 @@ class range(Sequence[int]):
@overload
def __getitem__(self, key: SupportsIndex, /) -> int: ...
@overload
def __getitem__(self, key: slice, /) -> range: ...
def __getitem__(self, key: IndexSlice, /) -> range: ...
def __reversed__(self) -> Iterator[int]: ...

class property:
Expand Down
10 changes: 5 additions & 5 deletions stdlib/collections/__init__.pyi
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 SupportsItems, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from _typeshed import IndexSlice, SupportsItems, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT
from typing import Any, Generic, NoReturn, SupportsIndex, TypeVar, final, overload
from typing_extensions import Self

Expand Down Expand Up @@ -129,12 +129,12 @@ class UserList(MutableSequence[_T]):
@overload
def __getitem__(self, i: SupportsIndex) -> _T: ...
@overload
def __getitem__(self, i: slice) -> Self: ...
def __getitem__(self, i: IndexSlice) -> Self: ...
@overload
def __setitem__(self, i: SupportsIndex, item: _T) -> None: ...
@overload
def __setitem__(self, i: slice, item: Iterable[_T]) -> None: ...
def __delitem__(self, i: SupportsIndex | slice) -> None: ...
def __setitem__(self, i: IndexSlice, item: Iterable[_T]) -> None: ...
def __delitem__(self, i: SupportsIndex | IndexSlice) -> None: ...
def __add__(self, other: Iterable[_T]) -> Self: ...
def __radd__(self, other: Iterable[_T]) -> Self: ...
def __iadd__(self, other: Iterable[_T]) -> Self: ...
Expand Down Expand Up @@ -174,7 +174,7 @@ class UserString(Sequence[UserString]):
def __hash__(self) -> int: ...
def __contains__(self, char: object) -> bool: ...
def __len__(self) -> int: ...
def __getitem__(self, index: SupportsIndex | slice) -> Self: ...
def __getitem__(self, index: SupportsIndex | IndexSlice) -> Self: ...
def __iter__(self) -> Iterator[Self]: ...
def __reversed__(self) -> Iterator[Self]: ...
def __add__(self, other: object) -> Self: ...
Expand Down
10 changes: 5 additions & 5 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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, IndexSlice, ReadableBuffer, SupportsKeysAndGetItem
from abc import ABCMeta, abstractmethod
from re import Match as Match, Pattern as Pattern
from types import (
Expand Down Expand Up @@ -581,7 +581,7 @@ class Sequence(Reversible[_T_co], Collection[_T_co]):
def __getitem__(self, index: int) -> _T_co: ...
@overload
@abstractmethod
def __getitem__(self, index: slice) -> Sequence[_T_co]: ...
def __getitem__(self, index: IndexSlice) -> Sequence[_T_co]: ...
# Mixin methods
def index(self, value: Any, start: int = 0, stop: int = ...) -> int: ...
def count(self, value: Any) -> int: ...
Expand All @@ -597,19 +597,19 @@ class MutableSequence(Sequence[_T]):
def __getitem__(self, index: int) -> _T: ...
@overload
@abstractmethod
def __getitem__(self, index: slice) -> MutableSequence[_T]: ...
def __getitem__(self, index: IndexSlice) -> MutableSequence[_T]: ...
@overload
@abstractmethod
def __setitem__(self, index: int, value: _T) -> None: ...
@overload
@abstractmethod
def __setitem__(self, index: slice, value: Iterable[_T]) -> None: ...
def __setitem__(self, index: IndexSlice, value: Iterable[_T]) -> None: ...
@overload
@abstractmethod
def __delitem__(self, index: int) -> None: ...
@overload
@abstractmethod
def __delitem__(self, index: slice) -> None: ...
def __delitem__(self, index: IndexSlice) -> None: ...
# Mixin methods
def append(self, value: _T) -> None: ...
def clear(self) -> None: ...
Expand Down
Loading