Skip to content

Question about Tuple with ellipsis #180

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

Closed
mkurnikov opened this issue Feb 1, 2016 · 8 comments
Closed

Question about Tuple with ellipsis #180

mkurnikov opened this issue Feb 1, 2016 · 8 comments

Comments

@mkurnikov
Copy link

Does Tuple support form of Tuple[int, str, ...] with more than one parameter before the ellipsis? It seems that it's not, currently.

class TupleMeta(TypingMeta):
    ...
    def __getitem__(self, parameters):
        if self.__tuple_params__ is not None:
            raise TypeError("Cannot re-parameterize %r" % (self,))
        if not isinstance(parameters, tuple):
            parameters = (parameters,)
        if len(parameters) == 2 and parameters[1] == Ellipsis:
            parameters = parameters[:1]
            use_ellipsis = True
            msg = "Tuple[t, ...]: t must be a type."
        else:
            use_ellipsis = False
            msg = "Tuple[t0, t1, ...]: each t must be a type."
        parameters = tuple(_type_check(p, msg) for p in parameters)
        return self.__class__(self.__name__, self.__bases__,
                              dict(self.__dict__), parameters,
                              use_ellipsis=use_ellipsis, _root=True)

I guess, Ellipsis-related check has to be implemented somehow like this:

    if parameters[-1] == Ellipsis:
        parameters = parameters[:-1]
        use_ellipsis = True
@gvanrossum
Copy link
Member

PP 484 is clear on the matter; only one type is allowed (and required) t appear before the ellipsis:

  • Tuple, used by listing the element types, for example
    Tuple[int, int, str].
    Arbitrary-length homogeneous tuples can be expressed
    using one type and ellipsis, for example Tuple[int, ...].

@srittau
Copy link
Collaborator

srittau commented Dec 24, 2021

@AlfreG Your question is probably better suited for our discussion forum: https://github.com/python/typing/discussions

@luckydonald
Copy link

luckydonald commented Jan 23, 2022

@srittau I love that you dug this ticket out after 5 years for that comment.

Careful, even if it reads like that, the non-working Tuple[int, int, str, ...] can't be written as Tuple[int, ...] but you will need Tuple[Any, ...] instead.

luckydonald added a commit to luckydonald/fastorm that referenced this issue Jan 23, 2022
@srittau
Copy link
Collaborator

srittau commented Jan 23, 2022

I have no idea what happened here. My answer was clearly not meant for this issue, but I'm not sure which issue it was meant for.

@erictraut
Copy link
Collaborator

FWIW, if PEP 646 is accepted, it will enable tuple types like this to be expressed as follows:

tuple[int, int, Unpack[tuple[str, ...]]]

(or if the nicer syntax for Unpack is approved)
tuple[int, int, *tuple[str, ...]]

Mypy doesn't yet contain support for PEP 646, but if you're interested in playing around with it, pyright and pyre both implement the latest draft of the PEP.

@JelleZijlstra
Copy link
Member

The nicer syntax was approved but will only be in Python 3.11. We'll backport Unpack support to older versions in typing-extensions.

@erictraut
Copy link
Collaborator

erictraut commented Jan 23, 2022

That's great news about the nicer syntax being approved. Do you recall where you saw that? I'm interested in the details. I presume that the more general syntax change proposed in PEP 637 was not accepted.

@JelleZijlstra
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants