Currently the builtins.pyi stub file has the following definition for `slice`:
```py
class slice(object):
    start: Any
    step: Any
    stop: Any
    @overload
    def __init__(self, stop: Any) -> None: ...
    @overload
    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
```py
class slice(Generic[T]):
    start: Optional[T]
    step: Optional[T]
    stop: Optional[T]
    @overload
    def __init__(self, stop: Optional[T]) -> None: ...
    @overload
    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 <typing-sig@python.org> wrote:
(it is formerly discussed in https://discuss.python.org/t/is-it-a-good-practice-to-pass-tuple-of-slice-to-getitem/6108 )

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 -- typing-sig@python.org
To unsubscribe send an email to typing-sig-leave@python.org
https://mail.python.org/mailman3/lists/typing-sig.python.org/
Member address: guido@python.org


--
--Guido van Rossum (python.org/~guido)
Pronouns: he/him (why is my pronoun here?)