Skip to content

ast.dump( show_empty = True) does not show most None values. #134733

Closed
@hunterhogan

Description

@hunterhogan

Bug report

Bug description:

The minimal, reproducible example is to click on a link to view the ast documentation.

Desired output

The example, if it were to include None values, would read

ast.Module(
    body=[
        ast.AsyncFunctionDef(
            name='f',
            args=ast.arguments(
                posonlyargs=[],
                args=[],
                vararg=None,
                kwonlyargs=[],
                kw_defaults=[],
                kwarg=None,
                defaults=[]),
            body=[
                ast.Expr(
                    value=ast.Await(
                        value=ast.Call(
                            func=ast.Name(id='other_func', ctx=ast.Load()),
                            args=[],
                            keywords=[])))],
            decorator_list=[],
            returns=None,
            type_comment=None,
            type_params=[])],
    type_ignores=[])

Solution

Change

cpython/Lib/ast.py

Lines 147 to 149 in 7291eab

if value is None and getattr(cls, name, ...) is None:
keywords = True
continue

To

                if value is None and getattr(cls, name, ...) is None:
                    if show_empty:
                        args.append('%s=%s' % (name, value))
                    keywords = True
                    continue

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Windows, Other

Linked PRs

Metadata

Metadata

Labels

3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesdocsDocumentation in the Doc dirstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions