Skip to content

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

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

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 26, 2025
  2. sharktide commented on May 26, 2025

    @sharktide
    Contributor

    Interesting! Do you want to make a PR or should I?

  3. hunterhogan commented on May 26, 2025

    @hunterhogan
    ContributorAuthor

    Umm, I'll try.

  4. added
    3.13only security fixes
    3.14bugs and security fixes
    3.15bugs and security fixes
    on May 29, 2025
  5. serhiy-storchaka commented on May 29, 2025

    @serhiy-storchaka
    Member

    The show_empty parameter was added in PR #116037 (issue #116023).

    This issue can be fixed in two ways:

    • Make ast.dump() with show_empty=True always showing None. This would change a lot of output, for example each Constant will have kind=None.
    • Change the documentation -- document that show_empty=True doesn't affect None values. The code also may need a change, because currently it looks trying to do something with None.

    This is a different issue than #134718, because this is clearly a bug, but #134718 may be qualified as a new feature.

    cc @JelleZijlstra, @sobolevn

  6. terryjreedy commented on May 29, 2025

    @terryjreedy
    Member

    This issue is the apparent discrepancy between the 3.13 ast.dump doc line "If show_empty is False (the default), empty lists and fields that are None will be omitted from the output." and the following example output with dump argument show_empty=True and continued omission . However, the doc line does not say what is supposed to happen if show_empty is True.

    Arg show_empty and the dump example for True were added in 3.13 in #116023. The intention of the issue was to not "output optional fields that are set to None or an empty string". In other words, remove 'noise'. Instead of always doing this, the new optional parameter was added to make the existing noisy output available to code that requires it. Indeed, the 3.13 dump with show_empty True is identical the 3.12 example for section Async and await. This should is intentional and should not be changed.

    The original patch had show_empty=True. The default was changed to False (with no deprecation period) a month later after input and discussion involving multiple people. This means that old code must add the option (but can make this simple change) for 3.13+. It was felt that the primary intended use for dump is for interactive use. The doc could and maybe should say "If show_empty is True, optional fields with empty lists are shown as in 3.12 and before".

    I omit None from this suggestion because it seems that optional None fields were already omitted in 3.12 and so were properly not added for show_empty=True in 3.13. Ast.py has dumps for every node type. The 3.13 patch removed perhaps a hundred lines of form 'val=[]' but none of form 'val=None'. There are a couple 'val=None's left, and there are tests that these are retained, so I presume these are required fields. So it seems that the existing show_empty=False line should be changed to 'empty lists for optional fields are omitted from the output." Maybe it should be also be said that optional None fields are always omitted.

    @JelleZijlstra @sobolevn Please review my understanding of what you did and consequent doc change suggestions.

  7. added a commit that references this issue on May 30, 2025
  8. sharktide commented on May 30, 2025

    @sharktide
    Contributor

    @serhiy-storchaka @JelleZijlstra @sobolevn

    I am pausing all work on this PR due to this ongoing debate. In the meantime, you can see some of my test changes (so far) in the test_ast.py on the patch-134733 branch of sharktide/cpython

  9. added a commit that references this issue on May 30, 2025
  10. serhiy-storchaka commented on May 30, 2025

    @serhiy-storchaka
    Member

    This is a sound argument in favor of changing the documentation. The purpose of the show_empty option is that you can pass show_empty=False to get pre-3.13 behavior.

    #134925 fixes the documentation.

  11. sharktide commented on May 30, 2025

    @sharktide
    Contributor

    I agree with @serhiy-storchaka on this one looking back

  12. added a commit that references this issue on May 30, 2025
  13. added 2 commits that reference this issue on May 30, 2025
  14. added 2 commits that reference this issue on May 30, 2025
  15. added a commit that references this issue on Jul 12, 2025
  16. added a commit that references this issue on Aug 4, 2025
  17. added a commit that references this issue on Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.13only security fixes3.14bugs and security fixes3.15bugs and security fixesdocsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions