Skip to content
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

Python 3.8 Constant: Support for nonexistent and other "kind" fields #44

Closed
wants to merge 1 commit into from

Conversation

tbennun
Copy link

@tbennun tbennun commented Jan 24, 2020

As kind is an optional field, it may not necessarily exist in valid AST (e.g., for numeric constants). This PR both supports this case, as well as other string kinds, such as 'b' (see python/cpython@10f8ce6)

@isidentical
Copy link

Can you post an example for generating a Constant with kind other then "u" (with ast.parse)

@tbennun
Copy link
Author

tbennun commented Jan 27, 2020

@isidentical currently, according to the above commit, this is planned but (also according to the commit) not parsed. This is unfortunate, since it requires to do type checking in an AST, which ideally should not be the case. See example below:

>>> ast.dump(ast.parse("""u"aaa" """))
"Module(body=[Expr(value=Constant(value='aaa', kind='u'))], type_ignores=[])"

>>> ast.dump(ast.parse("""b"aaa" """))
"Module(body=[Expr(value=Constant(value=b'aaa', kind=None))], type_ignores=[])"

@isidentical
Copy link

I'm aware of the u prefix behavior, but I dont see any reason to use b prefix and any activity on that.

@tbennun
Copy link
Author

tbennun commented Jan 27, 2020

well, there has to be a reason why the prefix is a string rather than a boolean.

@isidentical
Copy link

I can't see any for now, I guess this change depends on the maintainer.

@tbennun
Copy link
Author

tbennun commented Jan 27, 2020

@isidentical To see my reasoning, and also just for fun, try the following code:
ast.unparse(ast.Constant(value=b"bad", kind="u"))
This is perfectly valid Python AST. It results in "ub'bad'\n", and that is not correct python syntax.

@isidentical
Copy link

This is a manually constructed node, so if you give malformed AST you get malformed output it is pretty normal.

@simonpercivall
Copy link
Owner

Closing without action. See #43 for rationale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants