Skip to content
Prev Previous commit
Next Next commit
Split docstring, avoid recursion
  • Loading branch information
StanFromIreland committed Apr 25, 2026
commit 3adc040fa3f4669f4752f9bea8ae9d2b8d3053f7
27 changes: 18 additions & 9 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,26 @@ def dump(
):
"""
Return a formatted dump of the tree in node. This is mainly useful for
debugging purposes. If annotate_fields is true (by default),
debugging purposes.

If annotate_fields is true (by default),
the returned string will show the names and the values for fields.
If annotate_fields is false, the result string will be more compact by
omitting unambiguous field names. Attributes such as line
numbers and column offsets are not dumped by default. If this is wanted,
include_attributes can be set to true. If indent is a non-negative
omitting unambiguous field names.

Attributes such as line numbers and column offsets are not dumped by default.
If this is wanted, include_attributes can be set to true.

If color is true, the returned string is syntax highlighted using ANSI
escape sequences.
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
If color is false (the default), colored output is always disabled.

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.
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

If show_empty is False, then empty lists and fields that are None
will be omitted from the output for better readability.
If color is true, the returned string is syntax highlighted using ANSI
escape sequences.
If color is false (the default), colored output is always disabled.
"""
theme = get_theme(force_color=color, force_no_color=not color).ast
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

Expand Down Expand Up @@ -166,13 +173,15 @@ def _format(node, level=0):
field_type = cls._field_types.get(name, object)
if getattr(field_type, '__origin__', ...) is list:
if not keywords:
args_buffer.append(_format(value, level)[0])
args_buffer.append('[]')
continue
elif isinstance(value, Load):
field_type = cls._field_types.get(name, object)
if field_type is expr_context:
if not keywords:
args_buffer.append(_format(value, level)[0])
args_buffer.append(
f'{theme.node}{type(value).__name__}'
f'{theme.reset}()')
continue
if not keywords:
args.extend(args_buffer)
Expand Down
Loading