-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-36287: Make ast.dump() not output optional fields and attributes. #18843
Conversation
Values for optional fields and attributes of AST nodes are now set as class attributes (e.g. Constant.kind is set to None).
Suggestion: I think these attributes should still be printed if What do you think? |
Misc/NEWS.d/next/Library/2020-03-08-09-53-55.bpo-36287.mxr5m8.rst
Outdated
Show resolved
Hide resolved
Doc/library/ast.rst
Outdated
@@ -139,9 +139,10 @@ Literals | |||
|
|||
.. doctest:: | |||
|
|||
>>> import ast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be necessary for the doctest as we have
.. testsetup::
import ast
at the beggining of the document. Maybe I am missing something, though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not experienced with doctests. I ran ./python -m doctest Doc/library/ast.rst
and it failed. Seems doctest
does not understand testsetup
. Indeed, it is a Sphinx extension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you need to do "make doctest" in Docs
or PATH=./venv/bin:$PATH sphinx-build -b doctest -d build/doctrees -W . build/doctest FILENAME
if you want to only test one single file (the first run will be a bit longer while it builds the tree).
Currently the only attributes are "lineno", "col_offset", "end_lineno" and "end_col_offset". They have type Currently if you create an AST node manually and not set attributes, they will not be output by
If always output attributes if
Is it what you want to get? |
Sort of. What I am proposing (still don't know if its a good idea) is to have a way of "print everything, no matter if is default or not"). Feel free to ignore this if you don't see much value, I just wanted to know if you think it may make sense to preserve that behaviour under some option |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I think this is a great improvement! Feel free to ignore the remaining suggestions if you don't think is worth tackling those.
I experimented and I do not like the result. It would break an existing test In any case, this is just a first step, and there is a place for improvements. This change fixes the most annoying thing, |
In Python 3.9+, ast.dump() omits optional fields/attributes from the output if their value is None. Such defaults are defined as class attributes. See https://bugs.python.org/issue36287 And python/cpython#18843 This patch does not change the output on previous Python versions, because the class attributes are missing there. Fixes simonpercivall#56
…with default values. (GH-18843) The default values for optional fields and attributes of AST nodes are now set as class attributes (e.g. Constant.kind is set to None).
allsimple = allsimple and simple | ||
args.append('%s=%s' % (attr, value)) | ||
continue | ||
if value is None and getattr(cls, name, ...) is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to check this twice (additional getattr(cls, name, ...) is None
)? It seems like those is None
s will always be the same
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change and test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, although I did not change, now I can see that I overlooked that this is cls
and not node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this conversation can be resolved, I think
Values for optional fields and attributes of AST nodes are now set as class attributes (e.g. Constant.kind is set to None).
https://bugs.python.org/issue36287