Repository navigation
ast.dump( show_empty = True) does not show most None values. #134733
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 26, 2025 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on May 26, 2025 Interesting! Do you want to make a PR or should I?
Reacted by Hunter HoganUmm, I'll try.
- added3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixes3.15bugs and security fixesbugs and security fixes
on May 29, 2025 The
show_emptyparameter was added in PR #116037 (issue #116023).This issue can be fixed in two ways:
- Make
ast.dump()withshow_empty=Truealways showing None. This would change a lot of output, for example eachConstantwill havekind=None. - Change the documentation -- document that
show_empty=Truedoesn't affectNonevalues. The code also may need a change, because currently it looks trying to do something withNone.
This is a different issue than #134718, because this is clearly a bug, but #134718 may be qualified as a new feature.
- Make
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 areNonewill be omitted from the output." and the following example output with dump argumentshow_empty=Trueand continued omission . However, the doc line does not say what is supposed to happen if show_empty isTrue.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 toFalse(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=Falseline 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.
- added a commit that references this issue
on May 30, 2025 @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
This is a sound argument in favor of changing the documentation. The purpose of the
show_emptyoption is that you can passshow_empty=Falseto get pre-3.13 behavior.#134925 fixes the documentation.
I agree with @serhiy-storchaka on this one looking back
- added a commit that references this issue
on May 30, 2025
Bug report
Bug description:
The minimal, reproducible example is to click on a link to view the
astdocumentation.Desired output
The example, if it were to include
Nonevalues, would readSolution
Change
cpython/Lib/ast.py
Lines 147 to 149 in 7291eab
To
CPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
Windows, Other
Linked PRs
Nonevalues #134743