-
-
Notifications
You must be signed in to change notification settings - Fork 52
fix: preserve parentheses in expression stringification #389
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
base: main
Are you sure you want to change the base?
fix: preserve parentheses in expression stringification #389
Conversation
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
70ee6d7
to
150211d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
93afde8
to
70d36fd
Compare
I've updated the operator binding power values to use an |
I was worried about the perfs of the |
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.