fix: render empty-tuple subscript annotation as tuple[()] not tuple[]#474
Open
gaoflow wants to merge 1 commit into
Open
fix: render empty-tuple subscript annotation as tuple[()] not tuple[]#474gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
An empty `ExprTuple` with `implicit=True` (produced when parsing a subscript like `tuple[()]`) previously yielded nothing in `iterate()`, resulting in the invalid string `tuple[]`. An empty tuple has no implicit form—it must always be written as `()`—so treat an empty implicit tuple the same as an explicit one. Before: `str(returns)` of `def f() -> tuple[()]` gave `"tuple[]"`, which is a `SyntaxError` when re-parsed. After: correctly yields `"tuple[()]"`.
pawamoy
reviewed
Jun 25, 2026
pawamoy
left a comment
Member
There was a problem hiding this comment.
Thanks.
Please follow our PR template.
| """Whether the tuple is implicit (e.g. without parentheses in a subscript's slice).""" | ||
|
|
||
| def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]: | ||
| if self.implicit and not self.elements: |
Member
There was a problem hiding this comment.
An empty tuple is always written as
()
So we don't need to check self.implicit, right?
Contributor
Author
|
Updated the PR description to follow the template, including the AI disclosure checkbox. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For reviewers
Description of the change
Fix rendering for empty-tuple subscript annotations so
tuple[()]andTuple[()]stringify as valid Python instead oftuple[]/Tuple[].tuple[()]is a valid annotation for a zero-element tuple. Griffe parses it as anExprSubscriptwhose slice is an implicit, emptyExprTuple. BecauseExprTuple.iterate()suppressed parentheses whenimplicit=Trueand yielded no elements for an empty tuple, the outer subscript rendered empty brackets.An empty tuple has no omitted source form; it must be written as
(). This adds a focused early return for the empty implicit tuple case and covers both builtintupleandtyping.Tuplewithtest_empty_tuple_annotation_str.Validation:
test_empty_tuple_annotation_str.Relevant resources