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

Display ellipsis when formatting variadic tuple[T, ...] #11857

Merged
merged 1 commit into from
Dec 29, 2021

Conversation

intgr
Copy link
Contributor

@intgr intgr commented Dec 28, 2021

Description

Previously variadic tuples were displayed as builtins.tuple[T], which
is misleading. That syntax ordinarily indicates a tuple with a single
element. This change adds the ... ellipsis when formatting.

Result:

tup: tuple[str, ...] = ()
reveal_type(tup)  # N: Revealed type is "builtins.tuple[builtins.str, ...]"
#                                                                   ^^^^^

The formatting of non-variadic tuples is unchanged (e.g. Tuple[T, U]).

Fixes #9522

Test Plan

Lots of coverage for this already in the test suite, see test file diffs.

Previously variadic tuples were displayed as `builtins.tuple[T]`, which
is misleading. That syntax ordinarily indicates a tuple with a single
element. This change adds the `...` ellipsis when formatting.

The formatting of non-variadic tuples is unchanged (e.g. `Tuple[T, U, V]`).
s += '[{}]'.format(self.list_str(t.args))
if t.type.fullname == 'builtins.tuple':
assert len(t.args) == 1
s += '[{}, ...]'.format(self.list_str(t.args))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be written as .format(t.args[0].accept(self)), but this seems prettier.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, thank you! I remember when I used to scratch my head why my variadic Tuple was revealed as a single-item tuple. It was inconsistent and it was confusing.

Thanks again!

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is great!
(I guess this might make the fact that mypy uses builtins.tuple to represent homogenous tuples less obvious for mypy contributors, but that's an issue for another time)

@hauntsaninja hauntsaninja merged commit 0650548 into python:master Dec 29, 2021
tushar-deepsource pushed a commit to DeepSourceCorp/mypy that referenced this pull request Jan 20, 2022
Previously variadic tuples were displayed as `builtins.tuple[T]`, which
is misleading. That syntax ordinarily indicates a tuple with a single
element. This change adds the `...` ellipsis when formatting.

The formatting of non-variadic tuples is unchanged (e.g. `Tuple[T, U]`).

Fixes python#9522
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

Successfully merging this pull request may close these issues.

reveal_type(x) where x: Tuple[int, ...] doesn't show "..."
3 participants