Preserve required annotation parentheses in annotated assignments#23865
Merged
MichaReiser merged 1 commit intomainfrom Mar 10, 2026
Merged
Preserve required annotation parentheses in annotated assignments#23865MichaReiser merged 1 commit intomainfrom
MichaReiser merged 1 commit intomainfrom
Conversation
|
charliermarsh
added a commit
that referenced
this pull request
Mar 11, 2026
## Summary This is a follow-up to #23865 generated by prompted Codex as follows: > Can you find another bug like #23864 in the formatter for us to fix? Here are some examples that previously had their parentheses stripped (producing syntax errors) but now format correctly: ```python # Annotated assignments with named expressions x: (value := int) = 1 # was formatted as: x: value := int = 1 # Annotated assignments with yield def f(): y: (yield 1) = 1 # was formatted as: y: yield 1 = 1 z: (yield 1) # was formatted as: z: yield 1 # Annotated assignments with await async def g(): a: (await g()) = 1 # was formatted as: a: await g() = 1 b: (await g()) # was formatted as: b: await g() # Return annotations with await async def h() -> (await g()): # was formatted as: async def h() -> await g(): pass # Type aliases type X = (value := int) # was formatted as: type X = value := int ``` In each case, the parentheses are required for the expression to be syntactically valid, but the formatter was previously stripping them.
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.
Summary
Previously, we were forcing
Parentheses::Neverunless the annotation had comments.Closes #23864.