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

bpo-38870: Implement Simple Preceding to AST Unparser #17377

Merged
merged 9 commits into from
Mar 1, 2020

Conversation

isidentical
Copy link
Member

@isidentical isidentical commented Nov 25, 2019

@isidentical
Copy link
Member Author

Do we need news entry for this fix?

@isidentical isidentical force-pushed the bpo-38870-precedence-algo branch from 5919a2c to 7d8648f Compare November 25, 2019 14:55
@isidentical
Copy link
Member Author

I am not sure why this exists and what it does but it seems wrong to me, at least with checking everything in ast.

def test_field_attr_existence(self):
for name, item in ast.__dict__.items():
if isinstance(item, type) and name != 'AST' and name[0].isupper():
x = item()
if isinstance(x, ast.AST):
self.assertEqual(type(x._fields), tuple)

It was failing because of both _Levels and IntEnum. I first thinked an extra condition about if name not starts with underscore but it doesnt worked because of IntEnum. So I moved instance of AST check upper.

@pablogsal pablogsal self-requested a review November 25, 2019 16:18
@pablogsal pablogsal self-assigned this Nov 25, 2019
@isidentical isidentical force-pushed the bpo-38870-precedence-algo branch from b2bdbcc to f5188b1 Compare December 3, 2019 14:55
@isidentical
Copy link
Member Author

@pablogsal is it worth to mention there will be an extra field in certain nodes called _precedence_level so after unparsing the tree will be mutated. Maybe we can strip out that field by traversing tree with ast.walk after generating result? (but that would definitely slow down)

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still didn't have time to go to the full PR but some general comments:

  • I find the diff very difficult to read. You rearranged many functions and change signatures so the diff is bigger than it should be and that makes it complicated to read the changes and how the feature is implemented. Please, try to change only the relevant things for this issue in particular.

  • The class should not modify the ast object that you pass into or mutate it in any way, so please, do not attach attributes to the nodes (like _precedence_level) or do any mutation over the tree.

  • Bringing enum into the ast module will likely introduce another performance regression (please, measure).

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@isidentical isidentical force-pushed the bpo-38870-precedence-algo branch 2 times, most recently from d517b14 to 9a81efb Compare December 3, 2019 17:59
@isidentical
Copy link
Member Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@pablogsal: please review the changes made to this pull request.

@pablogsal
Copy link
Member

I'm currently handling some urgent regressions on buildbots, so it will take me some time to come back to this :(

@isidentical
Copy link
Member Author

Thanks for reviews @vstinner, can I get an DO NOT MERGE label until the other PR I'm going to open is merged? It is going to look messy until then.

@vstinner
Copy link
Member

Thanks for reviews @vstinner, can I get an DO NOT MERGE label until the other PR I'm going to open is merged? It is going to look messy until then.

You can add [WIP] to the PR title.

@isidentical isidentical changed the title bpo-38870: dont generate parantheses when not needed [WIP] bpo-38870: dont generate parantheses when not needed Dec 14, 2019
@isidentical isidentical changed the title [WIP] bpo-38870: Implement Simple Preceding to AST Unparser bpo-38870: Implement Simple Preceding to AST Unparser Dec 24, 2019
@isidentical
Copy link
Member Author

@pablogsal @vstinner #17612 is merged and I rewrote this preceding patch with basing that delimiting changes. The diff is much more minimal right now. This patch tries to implement the preceding system that was also used in ast_unparse.c and aims to remove unnecesary parens arround the expressions.

@isidentical isidentical requested a review from vstinner January 7, 2020 13:25
@isidentical isidentical requested a review from pablogsal January 11, 2020 13:20
Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me with the latest corrections!

Great job, @isidentical! 🎉

@vstinner
Copy link
Member

vstinner commented Mar 2, 2020

Well done @isidentical. I'm happy to see that the merged change now uses the enum module :-)

colesbury referenced this pull request in colesbury/nogil Oct 6, 2021
Implement a simple precedence algorithm for ast.unparse in order to avoid redundant
parenthesis for nested structures in the final output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants