mirror of
https://github.com/python/cpython.git
synced 2025-09-18 22:50:26 +00:00
gh-134718: Fix ast.dump() for empty non-default values (GH-134926)
This commit is contained in:
parent
ce6a6371a2
commit
cc344e8dd0
3 changed files with 36 additions and 12 deletions
22
Lib/ast.py
22
Lib/ast.py
|
@ -147,18 +147,16 @@ def dump(
|
||||||
if value is None and getattr(cls, name, ...) is None:
|
if value is None and getattr(cls, name, ...) is None:
|
||||||
keywords = True
|
keywords = True
|
||||||
continue
|
continue
|
||||||
if (
|
if not show_empty:
|
||||||
not show_empty
|
if value == []:
|
||||||
and (value is None or value == [])
|
field_type = cls._field_types.get(name, object)
|
||||||
# Special cases:
|
if getattr(field_type, '__origin__', ...) is list:
|
||||||
# `Constant(value=None)` and `MatchSingleton(value=None)`
|
if not keywords:
|
||||||
and not isinstance(node, (Constant, MatchSingleton))
|
args_buffer.append(repr(value))
|
||||||
):
|
continue
|
||||||
args_buffer.append(repr(value))
|
if not keywords:
|
||||||
continue
|
args.extend(args_buffer)
|
||||||
elif not keywords:
|
args_buffer = []
|
||||||
args.extend(args_buffer)
|
|
||||||
args_buffer = []
|
|
||||||
value, simple = _format(value, level)
|
value, simple = _format(value, level)
|
||||||
allsimple = allsimple and simple
|
allsimple = allsimple and simple
|
||||||
if keywords:
|
if keywords:
|
||||||
|
|
|
@ -1543,18 +1543,42 @@ Module(
|
||||||
full="MatchSingleton(value=None)",
|
full="MatchSingleton(value=None)",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
check_node(
|
||||||
|
ast.MatchSingleton(value=[]),
|
||||||
|
empty="MatchSingleton(value=[])",
|
||||||
|
full="MatchSingleton(value=[])",
|
||||||
|
)
|
||||||
|
|
||||||
check_node(
|
check_node(
|
||||||
ast.Constant(value=None),
|
ast.Constant(value=None),
|
||||||
empty="Constant(value=None)",
|
empty="Constant(value=None)",
|
||||||
full="Constant(value=None)",
|
full="Constant(value=None)",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
check_node(
|
||||||
|
ast.Constant(value=[]),
|
||||||
|
empty="Constant(value=[])",
|
||||||
|
full="Constant(value=[])",
|
||||||
|
)
|
||||||
|
|
||||||
check_node(
|
check_node(
|
||||||
ast.Constant(value=''),
|
ast.Constant(value=''),
|
||||||
empty="Constant(value='')",
|
empty="Constant(value='')",
|
||||||
full="Constant(value='')",
|
full="Constant(value='')",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
check_node(
|
||||||
|
ast.Interpolation(value=ast.Constant(42), str=None, conversion=-1),
|
||||||
|
empty="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
|
||||||
|
full="Interpolation(value=Constant(value=42), str=None, conversion=-1)",
|
||||||
|
)
|
||||||
|
|
||||||
|
check_node(
|
||||||
|
ast.Interpolation(value=ast.Constant(42), str=[], conversion=-1),
|
||||||
|
empty="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
|
||||||
|
full="Interpolation(value=Constant(value=42), str=[], conversion=-1)",
|
||||||
|
)
|
||||||
|
|
||||||
check_text(
|
check_text(
|
||||||
"def a(b: int = 0, *, c): ...",
|
"def a(b: int = 0, *, c): ...",
|
||||||
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))])])",
|
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))])])",
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:func:`ast.dump` now only omits ``None`` and ``[]`` values if they are
|
||||||
|
default values.
|
Loading…
Add table
Add a link
Reference in a new issue