Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed-size, multi-type sequence #592

Open
srittau opened this issue Nov 19, 2018 · 2 comments
Open

Fixed-size, multi-type sequence #592

srittau opened this issue Nov 19, 2018 · 2 comments
Labels
topic: feature Discussions about new features for Python's type annotations

Comments

@srittau
Copy link
Collaborator

srittau commented Nov 19, 2018

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.

@JukkaL
Copy link
Contributor

JukkaL commented Nov 19, 2018

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.

@srittau
Copy link
Collaborator Author

srittau commented Nov 19, 2018

Thanks, I fixed the example. I agree that this is not high priority.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: feature Discussions about new features for Python's type annotations
Projects
None yet
Development

No branches or pull requests

2 participants