Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
hauntsaninja merged 1 commit into
python:masterfrom
intgr:varargs-tuple-formatting
Dec 29, 2021
Merged

Display ellipsis when formatting variadic tuple[T, ...]#11857
hauntsaninja merged 1 commit into
python:masterfrom
intgr:varargs-tuple-formatting

Conversation

@intgr

@intgr intgr commented Dec 28, 2021

Copy link
Copy Markdown
Contributor

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]`).
Comment thread mypy/types.py
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
Copy Markdown
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.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

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!

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

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