ruff
214eb707 - Parenthesize expressions prior to LibCST parsing (#6742)

Commit
2 years ago
Parenthesize expressions prior to LibCST parsing (#6742) <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary This PR adds a utility for transforming expressions via LibCST that automatically wraps the expression in parentheses, applies a user-provided transformation, then strips the parentheses from the generated code. LibCST can't parse arbitrary expression ranges, since some expressions may require parenthesization in order to be parsed properly. For example: ```python option = ( '{name}={value}' .format(nam=name, value=value) ) ``` In this case, the expression range is: ```python '{name}={value}' .format(nam=name, value=value) ``` Which isn't valid on its own. So, instead, we add "fake" parentheses around the expression. We were already doing this in a few places, so this is mostly formalizing and DRYing up that pattern. Closes https://github.com/astral-sh/ruff/issues/6720.
Author
Parents
Loading