Skip to content

Commit 4fccca7

Browse files
committed
fix: Append trailing comma to length-1 tuples
Issue-343: #343
1 parent 410a392 commit 4fccca7

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

src/_griffe/expressions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,8 @@ def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]:
802802
if not self.implicit:
803803
yield "("
804804
yield from _join(self.elements, ", ", flat=flat)
805+
if len(self.elements) == 1:
806+
yield ","
805807
if not self.implicit:
806808
yield ")"
807809

tests/test_expressions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@ def test_expressions(code: str) -> None:
8585
top_node = compile(code, filename="<>", mode="exec", flags=ast.PyCF_ONLY_AST, optimize=2)
8686
expression = get_expression(top_node.body[0].value, parent=Module("module")) # type: ignore[attr-defined]
8787
assert str(expression) == code
88+
89+
90+
def test_length_one_tuple_as_string() -> None:
91+
"""Length-1 tuples must have a trailing comma."""
92+
code = "x = ('a',)"
93+
with temporary_visited_module(code) as module:
94+
assert str(module["x"].value) == "('a',)"

0 commit comments

Comments
 (0)