forked from sphinx-doc/sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
bugSomething isn't workingSomething isn't working
Description
1-element tuple rendered incorrectly
User request:
Describe the bug
This is a followup to sphinx-doc#7964 which has been addressed in sphinx-doc#8265.
However the special case of a 1-element tuple is still not handled correctly.
(1,) is rendered as (1), but should keep the trailing comma.
To Reproduce
Add a testcase
("(1,)", "(1,)"), # Tuple (single element)
at tests/test_pycode_ast.py around the tuple cases.
Researcher specification:
- Root cause:
- In
sphinx/pycode/ast.py,_UnparseVisitor.visit_Tuplejoins elements with", ".join(...)for non-empty tuples, which drops the trailing comma for single-element tuples.
- In
- Proposed fix:
- Special-case single-element tuples:
- If
len(node.elts) == 1, return"(" + self.visit(node.elts[0]) + ",)". - If
not node.elts, return"()". - Else, keep current behavior for multi-element tuples.
- If
- Special-case single-element tuples:
- Files/functions to modify:
sphinx/pycode/ast.py: class_UnparseVisitor, methodvisit_Tuple.
- Tests to add:
- In
tests/test_pycode_ast.pyadd:("(1,)", "(1,)")# single-element tuple- Optionally
( "((1,),)", "((1,),)" )# nested single-element tuple
- In
- Reproduction & expected failure before fix:
- Run:
pytest -q tests/test_pycode_ast.py -k test_unparse - Failure snippet for the new case:
- Expected:
(1,) - Actual:
(1) - Assertion diff:
(1)!=(1,)
- Expected:
- Run:
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working