fix: preserve parentheses in expression stringification#389
Merged
pawamoy merged 11 commits intomkdocstrings:mainfrom Jul 21, 2025
abc8747:fix/expression-parentheses
Merged
fix: preserve parentheses in expression stringification#389pawamoy merged 11 commits intomkdocstrings:mainfrom abc8747:fix/expression-parentheses
pawamoy merged 11 commits intomkdocstrings:mainfrom
abc8747:fix/expression-parentheses
Conversation
Contributor
Author
|
ruff failures doesn't seem to come from my change, i ran |
previously, stringifying expressions would concatenate the string
representation of their parts without considering the order of
operations, leading to incorrect removal of semantically necessary
parentheses.
- the binding strength of operators (1 for walrus, 18 for atoms) were
added
- parentheses are added when:
- inner operation has a lower precedence than an outer one
(e.g. `(a + b) * c`)
- subexpression on the right of left associative operators have the
same precedence (e.g. `(a - b) - c`)
- subexpression on the left of right associative operators have the
same precedence (e.g. `(a ** b) ** c`)
- special cases like power operator binding less when an arithmetic or
bitwise operator on its right were implemented
pawamoy
requested changes
Jun 24, 2025
Member
pawamoy
left a comment
There was a problem hiding this comment.
Thanks!
Just a few nits/remarks/questions.
I might later have further comments on the _yield implementation.
Co-authored-by: Timothée Mazzucotelli <[email protected]>
Co-authored-by: Timothée Mazzucotelli <[email protected]>
- improve comments on `NONE` sentinel value
Contributor
Author
|
I've updated the operator binding power values to use an |
Member
|
I was worried about the perfs of the |
Contributor
Author
|
Hi, just wondering if you had the time to review this? Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While using
mkdocstrings-python, I noticed that some of my type hints are formatted incorrectly in the final documentation. It looks like griffe is dropping parentheses when stringifying expressions. Some examples:(a + b).c()a + b.c()(a - b)[0]a - b[0](a if b else c).d()a if b else c.d()The operator binding power was used to determine whether parentheses should be added.