Currently the builtins.pyi stub file has the following definition for `slice`:
class slice(object):
    start: Any
    step: Any
    stop: Any
    def __init__(self, stop: Any) -> None: ...
    def __init__(self, start: Any, stop: Any, step: Any = ...) -> None: ...
    __hash__: None  # type: ignore
    def indices(self, len: int) -> Tuple[int, int, int]: ...
Would you want it to have a single type parameter, so start/stop/step should all have the same type, or do you think there's a need to specify their types separately?

The simplest improvement I could think of would use a single type parameter, be something like
class slice(Generic[T]):
    start: Optional[T]
    step: Optional[T]
    stop: Optional[T]
    def __init__(self, stop: Optional[T]) -> None: ...
    def __init__(self, start:  Optional[T] , stop:  Optional[T] , step:  Optional[T] = ...) -> None: ...
    __hash__: None  # type: ignore
    def indices(self, len: int) -> Tuple[int, int, int]: ...
Note that the `indices()` method is only useful if T is int.

I wouldn't be surprised if there were additional pitfalls though -- I don't have much experience myself with non-numeric indexes.

On Thu, Dec 10, 2020 at 11:01 AM Hao Zhang via Typing-sig <> wrote:
(it is formerly discussed in )

Currently we cannot write type annotation for slice like `slice[str, str]`.

And it is useful when write something like `a["C1":"C2"]` , which is used in pandas already.

And I also want to write something like `a[(1,2):4,(5,6):9]` which type could be `tuple[slice[tuple[int, int], int, None], ...]`

So I think it would be better if slice support type annotation
Typing-sig mailing list --
To unsubscribe send an email to
Member address:

--Guido van Rossum (
Pronouns: he/him (why is my pronoun here?)