3

A few valid values for a tuple that I'm trying to annotate:

("foo", 1, 2)
("bar", 11)
("baz", 42, 31, 20, 0, -700, 44444, 12345, 1, 2, 3, 4, 5, 6, 7, 8, 9)

I was expecting this to work:

my_tuple: Tuple[str, int, ...]  # doesn't work!

... but that throws error: Unexpected '...'

Any way to annotate this structure?

frnhr
  • 12,354
  • 9
  • 63
  • 90
  • 1
    I would go with a different datastructure: `Tuple[str, List[int]]`. The ellipsis syntax seems only possible for homogeneous tuples. At least the standard library only mentions this case. – Wombatz Jul 16 '21 at 20:50
  • Would the `any` type be a reasonable substitute for an ellipsis? – Ghoti Jul 16 '21 at 20:55
  • "Just in case anyone's curious: it's also used in the standard-library typing module: e.g. Callable[..., int] to indicate a callable that returns an int without specifying the signature, or Tuple[str, ...] to indicate a variable-length homogeneous tuple of strings." – Anakhand https://stackoverflow.com/questions/772124/what-does-the-ellipsis-object-do perhaps not. – Ghoti Jul 16 '21 at 20:56
  • 1
    @Ghoti no, because `Tuple[str, Any]` would imply a single value – juanpa.arrivillaga Jul 16 '21 at 21:03
  • 1
    @Ghoti Presumably it's the responsibility of the container type to determine how it uses the ellipsis. And `Tuple` doesn't allow the mixed use. – Barmar Jul 16 '21 at 21:04

0 Answers0