1

I have the following type and cant figure how to annotate it: [[str, int]]

I cant use List[List[str,int]] due

TypeError: Parameters to generic types must be types. Got slice(<class 'str'>, typing.Any, None).

Can i use Tuple despite having a list [str, int]: List[Tuple[str,int]]?

And List[List[Union[str, int]] does not reveal order: str before int. I try to annotate the https://redis.io/commands/xread response for redis-py

droid192
  • 2,011
  • 26
  • 43
  • 3
    I don't think this is supported; it's a mild misuse of `list` (which is logically intended to be a homogeneous sequence); `typing.Tuple` defaults to heterogeneous with fixed length with `Ellipsis` allowing it to be homogeneous with variable length (when used as immutable `list`), but `typing.List` (and `typing.Sequence`, the more generic version) don't have a reversed behavior to switch to fixed length heterogeneous behavior. – ShadowRanger Sep 13 '19 at 12:51
  • appreciate your words, yet spagetti for my brain – droid192 Sep 13 '19 at 13:33
  • 2
    I'd read [this answer to "What's the difference between lists and tuples?"](https://stackoverflow.com/a/626871/364696); it's a longer way of describing the difference in *intent*, beyond the much more minor differences in *function* (which is just mutable vs. immutable). You *can* use `tuple`s as just immutable `list`s (and `typing` supports it, because immutable `list`s are useful), but `typing` doesn't support `list`s used as mutable `tuple`s because the use cases are much more limited (and usually better achieved with class instances, especially now that the `dataclasses` module exists). – ShadowRanger Sep 13 '19 at 14:11
  • 2
    No, the `list` type does not support this. – juanpa.arrivillaga Sep 13 '19 at 14:43

0 Answers0