
Your use of slices definitely looks like a hack. :-) I'm not sure I would want to legitimize that use case by complicating the parameters for slice[]. It might be better for you to wait for PEP 637, then you can write `T[i=0, j=2, k=1]`.
Are there other uses you are aware of where slices are not homogeneous in type? If this is rare we could just tell users to use `Any` or a union, while simplifying life for other users -- types with multiple parameters are harder to remember (e.g. I can never remember what the three parameters to Generator mean :-).
Maybe you can just open an issue for the typeshed repo to add a single type parameter to the slice class? The default would be `Any` so existing uses of `slice` without parameters would mean the same thing as they currently mean (start, stop and step all have type `Any`).
On Fri, Dec 11, 2020 at 11:37 AM Hao Zhang via Typing-sig < typing-sig@python.org> wrote:
Can you show some examples of what you mean here?
It needs a little more background to explain why I want to use something like `d[{(1,2): 3}]`, let me change another easier example.
Now I have a named tensor where there is a str as name for every index. such as a normal 2*3*4 tensor `T`(2*3*4 tensor means it is something like `float[2][3][4]`), whose three index has name `i` and `j` and `k`. set `i=0, j=2, k=1`, and I will get `T[0, 2, 1]`.
In my application, it is difficult to tell the index order, because transpose is very frequent in many other operations. So I have to use `T[{"i": 0, "j": 2, "k": 1}]` to get the item from so-called "named tensor". Then when tensor `T` is transposed into a 2*4*3 tensor `G`, I can also use the same expression above to get the same item(`T[{"i": 0, "j": 2, "k": 1}] == G[{"i": 0, "j": 2, "k": 1}] == T[0, 2, 1] == G[0, 1, 2]`)
I think `T[{"i": 0, "j": 2, "k": 1}]` is a little wordy, If I can use `T["i": 0, "j": 2, "k": 1]` directly, it would be better. And luckly, it is valid syntax in python so I can use it now, python treats it as `tuple[slice, ...]` where each slice is something like `slice[str, int, None]`. But sadly I cannot describe such detail for slice in type annotation as `slice[str, int, None]`, that is why I post this thread.
Can you clarify this too?
In python colon in `d[x:y]` is always treated as slice, but sometimes it may not means "slice of list", just like the example above. So I think maybe slice should support three parameters rather than single paramter. _______________________________________________ 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