diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2024-04-24 11:02:38 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-24 11:02:38 +0300 |
commit | 692e902c742f577f9fc8ed81e60ed9dd6c994e1e (patch) | |
tree | 6d72378a639de5bb0414a38aded17d71bf5642e3 /Lib/ast.py | |
parent | 7e87d30f1f30d39c3005e03195f3d7648b38a1e2 (diff) | |
download | cpython-692e902c742f577f9fc8ed81e60ed9dd6c994e1e.tar.gz cpython-692e902c742f577f9fc8ed81e60ed9dd6c994e1e.zip |
gh-116023: Add `show_empty=False` to `ast.dump` (#116037)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Diffstat (limited to 'Lib/ast.py')
-rw-r--r-- | Lib/ast.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/Lib/ast.py b/Lib/ast.py index b8c4ce6f919..9f386051659 100644 --- a/Lib/ast.py +++ b/Lib/ast.py @@ -114,7 +114,11 @@ def literal_eval(node_or_string): return _convert(node_or_string) -def dump(node, annotate_fields=True, include_attributes=False, *, indent=None): +def dump( + node, annotate_fields=True, include_attributes=False, + *, + indent=None, show_empty=False, +): """ Return a formatted dump of the tree in node. This is mainly useful for debugging purposes. If annotate_fields is true (by default), @@ -125,6 +129,8 @@ def dump(node, annotate_fields=True, include_attributes=False, *, indent=None): include_attributes can be set to true. If indent is a non-negative 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 omitted from the output for better readability. """ def _format(node, level=0): if indent is not None: @@ -137,6 +143,7 @@ def dump(node, annotate_fields=True, include_attributes=False, *, indent=None): if isinstance(node, AST): cls = type(node) args = [] + args_buffer = [] allsimple = True keywords = annotate_fields for name in node._fields: @@ -148,6 +155,18 @@ def dump(node, annotate_fields=True, include_attributes=False, *, indent=None): 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 = [] value, simple = _format(value, level) allsimple = allsimple and simple if keywords: |