Fixed-size, multi-type sequence
This came up in python/typeshed#2287, but I have encountered the same problem myself. As far as I know, currently the only way to annotate a multi-type, fixed length sequence is using Tuple. But for input types this is often too strict:
def foo(x: Tuple[int, str]) -> None:
a, b = x
def bar(x: Tuple[int, str]) -> None:
a = x[0]
b = x[1]
The implementations accept any iterable (first case) or sequence (second case) that yield two values:
class Iter:
def __iter__(self):
yield 1
yield ""
foo((1, ""))
foo([1, ""])
foo(Iter())
bar((1, ""))
bar([1, ""])
It would be useful to have a way to annotate this. Of course type checkers would still need to reject generic cases like this:
def foo(x: TupleLike[str, str]): ...
x: List[str] = ...
foo(x)
But it could allow some things that are not possible at the moment, and opens the possibility for future improvements.
I've seen this come up once in a while. These would primarily be useful for annotating legacy interfaces.
Some observations:
- We'd need at least two separate types for these -- a fixed-length sequence and a fixed-length iterable.
- A fixed-length sequence would be a subtype of a fixed-length iterable (when item types are compatible).
- We could add a third type for a fixed-length iterator. Without this it wouldn't be possible to annotate a suitable
__iter__method. - All the above types should perhaps use structural subtyping. By using literal types we might be able to have a user-defined class that is accepted as a fixed-length sequence.
- It would be easy to image a fourth type for a fixed-length list. Not sure how useful this would be.
Also, the Iter example should probably be like this:
class Iter:
def __iter__(self):
yield 1
yield ""
The main objection to this feature I have is that it's pretty complicated to implement, and the use cases seem infrequent. Other extensions are probably higher priority.
Thanks, I fixed the example. I agree that this is not high priority.