From 65405e9ef28e2a4f3ad290b61e69269686079be9 Mon Sep 17 00:00:00 2001 From: Thanos <111999343+Sachaa-Thanasius@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:10:29 +0530 Subject: [PATCH] Make slice generic (#11637) --- stdlib/builtins.pyi | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index d94c07e62cba..ce0ea609e8f0 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -9,6 +9,7 @@ from _typeshed import ( ConvertibleToFloat, ConvertibleToInt, FileDescriptorOrPath, + MaybeNone, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, @@ -94,6 +95,9 @@ _SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant _AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any]) _AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True) _P = ParamSpec("_P") +_StartT = TypeVar("_StartT", covariant=True, default=Any) +_StopT = TypeVar("_StopT", covariant=True, default=Any) +_StepT = TypeVar("_StepT", covariant=True, default=Any) class object: __doc__: str | None @@ -936,17 +940,25 @@ class bool(int): def __invert__(self) -> int: ... @final -class slice: +class slice(Generic[_StartT, _StopT, _StepT]): @property - def start(self) -> Any: ... + def start(self) -> _StartT: ... @property - def step(self) -> Any: ... + def step(self) -> _StepT: ... @property - def stop(self) -> Any: ... + def stop(self) -> _StopT: ... @overload - def __new__(cls, stop: Any, /) -> Self: ... + def __new__(cls, stop: int | None) -> slice[int | MaybeNone, int | MaybeNone, int | MaybeNone]: ... @overload - def __new__(cls, start: Any, stop: Any, step: Any = ..., /) -> Self: ... + def __new__( + cls, start: int | None, stop: int | None, step: int | None = None + ) -> slice[int | MaybeNone, int | MaybeNone, int | MaybeNone]: ... + @overload + def __new__(cls, stop: _T2, /) -> slice[Any, _T2, Any]: ... + @overload + def __new__(cls, start: _T1, stop: _T2, /) -> slice[_T1, _T2, Any]: ... + @overload + def __new__(cls, start: _T1, stop: _T2, step: _T3, /) -> slice[_T1, _T2, _T3]: ... def __eq__(self, value: object, /) -> bool: ... if sys.version_info >= (3, 12): def __hash__(self) -> int: ...