cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-116023: Add `show_empty=True` to `ast.dump`

Open sobolevn opened this issue 1 year ago • 4 comments

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/

sobolevn avatar Feb 28 '24 08:02 sobolevn

Updated! Thank you!

sobolevn avatar Mar 15 '24 11:03 sobolevn

I will wait for some third opinion, maybe someone else has input: whether this should be a default or not.

Thanks for the review!

sobolevn avatar Mar 15 '24 14:03 sobolevn

Maybe @carljm or @hauntsaninja ? :)

sobolevn avatar Mar 15 '24 14:03 sobolevn

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=[]).

carljm avatar Mar 21 '24 22:03 carljm

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 :)

sobolevn avatar Mar 22 '24 10:03 sobolevn

Waiting for some time for @hauntsaninja and @carljm to provide feedback :) Jelle, thanks a lot for the review and for finding annotate_fields bug!

sobolevn avatar Apr 16 '24 14:04 sobolevn

(Waiting for one more day)

sobolevn avatar Apr 23 '24 04:04 sobolevn

Thanks everyone! 🎉

sobolevn avatar Apr 24 '24 08:04 sobolevn

Maybe we should also mention this in 3.13.rst in ast section?

sobolevn avatar Apr 24 '24 08:04 sobolevn