Skip to content

Commit

Permalink
fix: Append trailing comma to length-1 tuples
Browse files Browse the repository at this point in the history
Issue-343: #343
  • Loading branch information
pawamoy committed Dec 26, 2024
1 parent 410a392 commit 4fccca7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/_griffe/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,8 @@ def iterate(self, *, flat: bool = True) -> Iterator[str | Expr]:
if not self.implicit:
yield "("
yield from _join(self.elements, ", ", flat=flat)
if len(self.elements) == 1:
yield ","
if not self.implicit:
yield ")"

Expand Down
7 changes: 7 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@ def test_expressions(code: str) -> None:
top_node = compile(code, filename="<>", mode="exec", flags=ast.PyCF_ONLY_AST, optimize=2)
expression = get_expression(top_node.body[0].value, parent=Module("module")) # type: ignore[attr-defined]
assert str(expression) == code


def test_length_one_tuple_as_string() -> None:
"""Length-1 tuples must have a trailing comma."""
code = "x = ('a',)"
with temporary_visited_module(code) as module:
assert str(module["x"].value) == "('a',)"

0 comments on commit 4fccca7

Please sign in to comment.