cpython
cpython copied to clipboard
gh-116023: Add `show_empty=True` to `ast.dump`
My thoughts:
- This parameter should be optional and switched off by default, so we preserve the output if people rely on it: as debugging tool or somewhere in tests
- It should not touch attributes, because they already have a different flag
- Issue: gh-116023
📚 Documentation preview 📚: https://cpython-previews--116037.org.readthedocs.build/
Updated! Thank you!
I will wait for some third opinion, maybe someone else has input: whether this should be a default or not.
Thanks for the review!
Maybe @carljm or @hauntsaninja ? :)
FWIW, I agree with @hauntsaninja and @JelleZijlstra. I think in the more common use cases, seeing empty values is noisy and not useful, and show_empty=True can be passed if you want to see them. I don't think there are any guarantees around backwards-compatibility for the textual representation of an AST.
that's basically what 3.12's ast.dump will give you.
That depends on whether you are talking about ast.dump(ast.arguments()) (dumping a manually-constructed AST) or ast.dump(ast.parse("def x(): pass")) (dumping a parsed AST). For the latter case, show_empty=False is a new behavior that no previous Python version has ever had.
I still think it's the better behavior, though. I would rather see
Module(body=[FunctionDef(name='x', args=arguments(), body=[Pass()])])
by default, instead of
Module(body=[FunctionDef(name='x', args=arguments(posonlyargs=[], args=[], kwonlyargs=[], kw_defaults=[], defaults=[]), body=[Pass()], decorator_list=[], type_params=[])], type_ignores=[]).
Ok, fair enough! I've changed the default from show_empty=True to show_empty=False.
I've also updated tests and docs. Please, take a look at my new changes :)
Waiting for some time for @hauntsaninja and @carljm to provide feedback :)
Jelle, thanks a lot for the review and for finding annotate_fields bug!
(Waiting for one more day)
Thanks everyone! 🎉
Maybe we should also mention this in 3.13.rst in ast section?