Skip to content

Commit cc344e8

Browse files
gh-134718: Fix ast.dump() for empty non-default values (GH-134926)
1 parent ce6a637 commit cc344e8

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

Lib/ast.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,16 @@ def _format(node, level=0):
147147
if value is None and getattr(cls, name, ...) is None:
148148
keywords = True
149149
continue
150-
if (
151-
not show_empty
152-
and (value is None or value == [])
153-
# Special cases:
154-
# `Constant(value=None)` and `MatchSingleton(value=None)`
155-
and not isinstance(node, (Constant, MatchSingleton))
156-
):
157-
args_buffer.append(repr(value))
158-
continue
159-
elif not keywords:
160-
args.extend(args_buffer)
161-
args_buffer = []
150+
if not show_empty:
151+
if value == []:
152+
field_type = cls._field_types.get(name, object)
153+
if getattr(field_type, '__origin__', ...) is list:
154+
if not keywords:
155+
args_buffer.append(repr(value))
156+
continue
157+
if not keywords:
158+
args.extend(args_buffer)
159+
args_buffer = []
162160
value, simple = _format(value, level)
163161
allsimple = allsimple and simple
164162
if keywords:

Lib/test/test_ast/test_ast.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,18 +1543,42 @@ def check_text(code, empty, full, **kwargs):
15431543
full="MatchSingleton(value=None)",
15441544
)
15451545

1546+
check_node(
1547+
ast.MatchSingleton(value=[]),
1548+
empty="MatchSingleton(value=[])",
1549+
full="MatchSingleton(value=[])",
1550+
)
1551+
15461552
check_node(
15471553
ast.Constant(value=None),
15481554
empty="Constant(value=None)",
15491555
full="Constant(value=None)",
15501556
)
15511557

1558+
check_node(
1559+
ast.Constant(value=[]),
1560+
empty="Constant(value=[])",
1561+
full="Constant(value=[])",
1562+
)
1563+
15521564
check_node(
15531565
ast.Constant(value=''),
15541566
empty="Constant(value='')",
15551567
full="Constant(value='')",
15561568
)
15571569

1570+
check_node(
1571+
ast.Interpolation(value=ast.Constant(42), str=None, conversion=-1),
1572+
empty="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
1573+
full="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
1574+
)
1575+
1576+
check_node(
1577+
ast.Interpolation(value=ast.Constant(42), str=[], conversion=-1),
1578+
empty="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
1579+
full="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
1580+
)
1581+
15581582
check_text(
15591583
"def a(b: int = 0, *, c): ...",
15601584
empty="Module(body=[FunctionDef(name='a', args=arguments(args=[arg(arg='b', annotation=Name(id='int', ctx=Load()))], kwonlyargs=[arg(arg='c')], kw_defaults=[None], defaults=[Constant(value=0)]), body=[Expr(value=Constant(value=Ellipsis))])])",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`ast.dump` now only omits ``None`` and ``[]`` values if they are
2+
default values.

0 commit comments

Comments
 (0)