Skip to content

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

Closed

Description

@eric-wieser

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.

Activity

  1. tk0miya commented on Apr 17, 2020

    @tk0miya
    Member

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

  2. added this to the 3.1.0 milestone on Apr 17, 2020
  3. eric-wieser commented on Apr 17, 2020

    @eric-wieser
    ContributorAuthor

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

  4. eric-wieser commented on Apr 17, 2020

    @eric-wieser
    ContributorAuthor

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

  5. tk0miya commented on Apr 17, 2020

    @tk0miya
    Member

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

  6. eric-wieser commented on Apr 17, 2020

    @eric-wieser
    ContributorAuthor

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

  7. tk0miya commented on Apr 17, 2020

    @tk0miya
    Member

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

  8. added a commit that references this issue on Jul 19, 2020
    f6e5180
  9. tk0miya commented on Oct 4, 2020

    @tk0miya
    Member

    Fixed by #8265 now.

  10. locked as resolved and limited conversation to collaborators on Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions