8000 gh-116023: Add `show_empty=False` to `ast.dump` by sobolevn · Pull Request #116037 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-116023: Add show_empty=False to ast.dump #116037

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 14 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed Apr 18, 2024
commit 15aa403ca1b55a30371e514f50a2d14f3840195e
74 changes: 42 additions & 32 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ Comprehensions

>>> print(ast.dump(
... ast.parse('[x for x in numbers]', mode='eval'),
... indent=4, show_empty=True,
... indent=4,
... ))
Expression(
body=ListComp(
Expand All @@ -733,11 +733,10 @@ Comprehensions
comprehension(
target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()),
ifs=[],
is_async=0)]))
>>> print(ast.dump(
... ast.parse('{x: x**2 for x in numbers}', mode='eval'),
... indent=4, show_empty=True,
... indent=4,
... ))
Expression(
body=DictComp(
Expand All @@ -750,11 +749,10 @@ Comprehensions
comprehension(
target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()),
ifs=[],
is_async=0)]))
>>> print(ast.dump(
... ast.parse('{x for x in numbers}', mode='eval'),
... indent=4, show_empty=True,
... indent=4,
... ))
Expression(
body=SetComp(
Expand All @@ -763,7 +761,6 @@ Comprehensions
comprehension(
target=Name(id='x', ctx=Store()),
iter=Name(id='numbers', ctx=Load()),
ifs=[],
is_async=0)]))


Expand Down Expand Up @@ -1404,7 +1401,7 @@ Pattern matching
... ...
... case tuple():
... ...
... """), indent=4, show_empty=True))
... """), indent=4))
Module(
body=[
Match(
Expand All @@ -1425,14 +1422,10 @@ Pattern matching
value=Constant(value=Ellipsis))]),
match_case(
pattern=MatchClass(
cls=Name(id='tuple', ctx=Load()),
patterns=[],
kwd_attrs=[],
kwd_patterns=[]),
cls=Name(id='tuple', ctx=Load())),
body=[
Expr(
value=Constant(value=Ellipsis))])])],
type_ignores=[])
value=Constant(value=Ellipsis))])])])

.. versionadded:: 3.10

Expand Down Expand Up @@ -1585,7 +1578,7 @@ Pattern matching
... ...
... case {**rest}:
... ...
... """), indent=4, show_empty=True))
... """), indent=4))
Module(
body=[
Match(
Expand All @@ -1603,11 +1596,10 @@ Pattern matching
Expr(
value=Constant(value=Ellipsis))]),
match_case(
pattern=MatchMapping(keys=[], patterns=[], rest='rest'),
pattern=MatchMapping(rest='rest'),
body=[
Expr(
value=Constant(value=Ellipsis))])])],
type_ignores=[])
value=Constant(value=Ellipsis))])])])

.. versionadded:: 3.10

Expand Down Expand Up @@ -1636,7 +1628,7 @@ Pattern matching
... ...
... case Point3D(x=0, y=0, z=0):
... ...
... """), indent=4, show_empty=True))
... """), indent=4))
Module(
body=[
Match(
Expand All @@ -1649,16 +1641,13 @@ Pattern matching
MatchValue(
value=Constant(value=0)),
MatchValue(
value=Constant(value=0))],
kwd_attrs=[],
kwd_patterns=[]),
value=Constant(value=0))]),
body=[
Expr(
value=Constant(value=Ellipsis))]),
match_case(
pattern=MatchClass(
cls=Name(id='Point3D', ctx=Load()),
patterns=[],
kwd_attrs=[
'x',
'y',
Expand All @@ -1672,8 +1661,7 @@ Pattern matching
value=Constant(value=0))]),
body=[
Expr(
value=Constant(value=Ellipsis))])])],
type_ignores=[])
value=Constant(value=Ellipsis))])])])

.. versionadded:: 3.10

Expand Down Expand Up @@ -1863,21 +1851,16 @@ Function and class definitions

.. doctest::

>>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4, show_empty=True))
>>> print(ast.dump(ast.parse('lambda x,y: ...'), indent=4))
Module(
body=[
Expr(
value=Lambda(
args=arguments(
posonlyargs=[],
args=[
arg(arg='x'),
arg(arg='y')],
kwonlyargs=[],
kw_defaults=[],
defaults=[]),
body=Constant(value=Ellipsis)))],
type_ignores=[])
arg(arg='y')]),
body=Constant(value=Ellipsis)))])


.. class:: arguments(posonlyargs, args, vararg, kwonlyargs, kw_defaults, kwarg, defaults)
Expand Down Expand Up @@ -2390,6 +2373,33 @@ and classes for traversing abstract syntax trees:
.. versionchanged:: 3.13
Added the *show_empty* option.

.. doctest::

>>> print(ast.dump(ast.parse("""\
... async def f():
... await other_func()
... """), indent=4, show_empty=True))
Module(
body=[
AsyncFunctionDef(
name='f',
args=arguments(
posonlyargs=[],
args=[],
kwonlyargs=[],
kw_defaults=[],
defaults=[]),
body=[
Expr(
value=Await(
value=Call(
func=Name(id='other_func', ctx=Load()),
args=[],
keywords=[])))],
decorator_list=[],
type_params=[])],
type_ignores=[])


.. _ast-compiler-flags:

Expand Down
2 changes: 1 addition & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def dump(
integer or string, then the tree will be pretty-printed with that indent
level. None (the default) selects the single line representation.
If show_empty is False, then empty lists and fields that are None
will be ommitted from the output for better readability.
will be omitted from the output for better readability.
"""
def _format(node, level=0):
if indent is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Add ``show_empty=False`` parameter to :func:`ast.dump` to optionally enable
showing empty fields in the AST.
Don't show empty fields (value ``None`` or ``[]``)
in :func:`ast.dump` by default. Add ``show_empty=False``
parameter to optionally show them.
0