Skip to content

[3.13] gh-134718: Fix ast.dump() for empty non-default values (GH-134926) #134936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,16 @@ def _format(node, level=0):
if value is None and getattr(cls, name, ...) is None:
keywords = True
continue
if (
not show_empty
and (value is None or value == [])
# Special cases:
# `Constant(value=None)` and `MatchSingleton(value=None)`
and not isinstance(node, (Constant, MatchSingleton))
):
args_buffer.append(repr(value))
continue
elif not keywords:
args.extend(args_buffer)
args_buffer = []
if not show_empty:
if value == []:
field_type = cls._field_types.get(name, object)
if getattr(field_type, '__origin__', ...) is list:
if not keywords:
args_buffer.append(repr(value))
continue
if not keywords:
args.extend(args_buffer)
args_buffer = []
value, simple = _format(value, level)
allsimple = allsimple and simple
if keywords:
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,12 +1346,24 @@ def check_text(code, empty, full, **kwargs):
full="MatchSingleton(value=None)",
)

check_node(
ast.MatchSingleton(value=[]),
empty="MatchSingleton(value=[])",
full="MatchSingleton(value=[])",
)

check_node(
ast.Constant(value=None),
empty="Constant(value=None)",
full="Constant(value=None)",
)

check_node(
ast.Constant(value=[]),
empty="Constant(value=[])",
full="Constant(value=[])",
)

check_node(
ast.Constant(value=""),
empty="Constant(value='')",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`ast.dump` now only omits ``None`` and ``[]`` values if they are
default values.
Loading