Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/griffelib/src/griffe/_internal/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,14 @@ class ExprTuple(Expr):
"""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:

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.

An empty tuple is always written as ()

So we don't need to check self.implicit, right?

# An empty tuple is always written as `()` and cannot be implicit.
# This arises in annotations like `tuple[()]`, where the AST represents
# the subscript slice as an empty Tuple node, but the parentheses must
# be preserved to produce valid Python (`tuple[]` is a SyntaxError).
yield "("
yield ")"
return
if not self.implicit:
yield "("
yield from _join(self.elements, ", ", flat=flat)
Expand Down
19 changes: 19 additions & 0 deletions packages/griffelib/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,22 @@ def test_render_dict_with_unpacking() -> None:
assert str(module["a"].value) == "{**base, 'x': 1}"
assert str(module["b"].value) == "{**d1, **d2}"
assert str(module["c"].value) == "{None: 1, 'y': 2}"


def test_empty_tuple_annotation_str() -> None:
"""Check that empty-tuple annotations round-trip correctly.

``tuple[()]`` is a valid annotation for a zero-element tuple.
Its string representation must remain ``tuple[()]``, not the
invalid ``tuple[]``.
"""
with temporary_visited_module(
"""
from typing import Tuple

def f1() -> tuple[()]: ...
def f2() -> Tuple[()]: ...
""",
) as module:
assert str(module["f1"].returns) == "tuple[()]"
assert str(module["f2"].returns) == "Tuple[()]"
Loading