Repository navigation
Closed
Description
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
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
continueCPython versions tested on:
3.13, CPython main branch
Operating systems tested on:
Windows, Other
Linked PRs
- gh-134733: fix: ast.dump( show_empty=True ) now displays all
Nonevalues #134743 - gh-134733: Fix documentation for the show_empty option of ast.dump() #134925
- [3.14] gh-134733: Fix documentation for the show_empty option of ast.dump() (GH-134925) #134940
- [3.13] gh-134733: Fix documentation for the show_empty option of ast.dump() (GH-134925) #134941
Activity
hunterhogan commentedon May 26, 2025
See also:
ast.dump()elision of empty values should use field types #134718ast.MatchStar.nameincorrectly has class-level default value #134674sharktide commentedon May 26, 2025
Interesting! Do you want to make a PR or should I?
hunterhogan commentedon May 26, 2025
Umm, I'll try.
serhiy-storchaka commentedon May 29, 2025
The
show_emptyparameter was added in PR #116037 (issue #116023).This issue can be fixed in two ways:
ast.dump()withshow_empty=Truealways showing None. This would change a lot of output, for example eachConstantwill havekind=None.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.
cc @JelleZijlstra, @sobolevn
terryjreedy commentedon May 29, 2025
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.
pythongh-134733 Fix some test fails. May need reverting
sharktide commentedon 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
pythongh-134733: Fix documentation for the show_empty option of ast.d…
serhiy-storchaka commentedon May 30, 2025
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.
sharktide commentedon May 30, 2025
I agree with @serhiy-storchaka on this one looking back
gh-134733: Fix documentation for the show_empty option of ast.dump() (G…
pythongh-134733: Fix documentation for the show_empty option of ast.d…
pythongh-134733: Fix documentation for the show_empty option of ast.d…
[3.14] gh-134733: Fix documentation for the show_empty option of ast.…
[3.13] gh-134733: Fix documentation for the show_empty option of ast.…
pythongh-134733: Fix documentation for the show_empty option of ast.d…
pythongh-134733: Fix documentation for the show_empty option of ast.d…
pythongh-134733: Fix documentation for the show_empty option of ast.d…