Is it a good practice to pass tuple of slice to __getitem__?

Hello, currently I write a class with api like

a[{(1,2): 3, (4,7): 7}]

its signature is dict[tuple[int, int], int]

and I find I can pass directly without {} like

a[(1, 2): 3, (4, 7): 7]

then its signature becomes tuple[slice].

I don’t know whether it is good choice

but I find i cannot specify the type in slice

that is to say I cannot write tuple[slice[tuple[int, int], int, NoneType]]

maybe slice should not accept none integer argument?

In short, I think python should do one of two following things:

  1. forbid none integer argument for slice
  2. add slice support for generic alias just like list or dict, then I can write something like slice[int, int, int]

Good practice or not, Numpy has been doing this since the longest time, so this is accepted practice and cannot be forbidden in any practical sense.

If numpy already use it, I think slice should add support for __class_getitem__ then I can write slice[tuple[int, int], int, None] in my case

BTW, I am not familiar with numpy, and I never see this usage, could you give me an example how numpy use non-int slice?

In,it seems that all examples use int slice

Oops, your’re right, Numpy uses tuple-of-slices, not slice-of-tuples. I can’t think of a usage of tuple-of-slices from the top of my head either, but slices are definitely not limited to integers (and None). It is common in Pandas, for example, to use slice of strings (where the strings are titles), e.g.

df["C1":"C3"]  # This returns data between labels C1 and C3.

Are you talking here about type annotations? That might be something to discuss on the typing SIG. I don’t know what the current annotations for the slice builtin are (it doesn’t have an __annotations__ attribute).

Well, when I post this topic I don’t know whether it is proper to create slice from none integer type.

Since pandas use str slice already, I think slice should support type annotation now.

BTW, I am new to Python forum, what is typing “SIG”?

SIG is short for Special Interest Group, and the Typing SIG is a bunch of people that are interested in stuff related to typing, including type annotations. They discuss things on the Typing SIG mailing list: