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

sphinx.pycode.ast.unparse does not understand operator precedence #7498

Closed
eric-wieser opened this issue Apr 17, 2020 · 7 comments
Closed

sphinx.pycode.ast.unparse does not understand operator precedence #7498

eric-wieser opened this issue Apr 17, 2020 · 7 comments
Labels
Milestone

Comments

@eric-wieser
Copy link
Contributor

As an example:

>>> import sphinx.pycode.ast as ast
>>> ast.unparse(ast.parse("(1 + 2) * 3").body[0].value)
'1 + 2 * 3'
>>> ast.unparse(ast.parse("(1, 2) + (3, 4)").body[0].value)
'1, 2 + 3, 4'

Easiest way to fix this will be to copy some of the approach from the python 3.9 ast.unparse, which needs #7497 in order to keep track of the current precedence level.

@tk0miya
Copy link
Member

tk0miya commented Apr 17, 2020

Please mind we also need to support Callable[[str, str], str]. This type hint also uses a tuple without parenthesis.

@tk0miya tk0miya added this to the 3.1.0 milestone Apr 17, 2020
@eric-wieser
Copy link
Contributor Author

Indeed, the idea would be to add parentheses only where needed, like ast.unparse does

@eric-wieser
Copy link
Contributor Author

Although note that Callable[([str, str], str)] is perfectly correct, it just is unfortunately also ugly.

@tk0miya
Copy link
Member

tk0miya commented Apr 17, 2020

It seems ast.unparse() uses items_view() to generate common part of sequences. It's cool.

@eric-wieser
Copy link
Contributor Author

Copying the entirety of ast.unparse into sphinx may also be a sensible option.

@tk0miya
Copy link
Member

tk0miya commented Apr 17, 2020

Reasonable. But we should notice about license issue to copy it.

@tk0miya
Copy link
Member

tk0miya commented Oct 4, 2020

Fixed by #8265 now.

@tk0miya tk0miya closed this as completed Oct 4, 2020
@tk0miya tk0miya modified the milestones: some future version, 3.3.0 Oct 4, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants