8000 [3.13] gh-134718: Fix ast.dump() for empty non-default values (GH-134… · python/cpython@681856c · GitHub
[go: up one dir, main page]

Skip to content

Commit 681856c

Browse files
[3.13] gh-134718: Fix ast.dump() for empty non-default values (GH-134926) (GH-134936)
(cherry picked from commit cc344e8)
1 parent 7fdc829 commit 681856c

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Lib/ast.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,16 @@ def _format(node, level=0):
151151
if value is None and getattr(cls, name, ...) is None:
152152
keywords = True
153153
continue
154-
if (
155-
not show_empty
156-
and (value is None or value == [])
157-
# Special cases:
158-
# `Constant(value=None)` and `MatchSingleton(value=None)`
159-
and not isinstance(node, (Constant, MatchSingleton))
160-
):
161-
args_buffer.append(repr(value))
162-
continue
163-
elif not keywords:
164-
args.extend(args_buffer)
165-
args_buffer = []
154+
if not show_empty:
155+
if value == []:
156+
field_type = cls._field_types.get(name, object)
157+
if getattr(field_type, '__origin__', ...) is list:
158+
if not keywords:
159+
args_buffer.append(repr(value))
160+
continue
161+
if not keywords:
162+
args.extend(args_buffer)
163+
args_buffer = []
166164
value, simple = _format(value, level)
167165
allsimple = allsimple and simple
168166
if keywords:

Lib/test/test_ast/test_ast.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,12 +1346,24 @@ def check_text(code, empty, full, **kwargs):
13461346
full="MatchSingleton(value=None)",
13471347
)
13481348

1349+
check_node(
1350+
ast.MatchSingleton(value=[]),
1351+
empty="MatchSingleton(value=[])",
1352+
full="MatchSingleton(value=[])",
1353+
)
1354+
13491355
check_node(
13501356
ast.Constant(value=None),
13511357
empty="Constant(value=None)",
13521358
full="Constant(value=None)",
13531359
)
13541360

1361+
check_node(
1362+
ast.Constant(value=[]),
1363+
empty="Constant(value=[])",
1364+
full="Constant(value=[])",
1365+
)
1366+
13551367
check_node(
13561368
ast.Constant(value=""),
13571369
empty="Constant(value='')",
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)
0