Fix unstable f-string formatting for expressions containing a trailing comma#15545
Merged
MichaReiser merged 1 commit intomainfrom Jan 17, 2025
Merged
Fix unstable f-string formatting for expressions containing a trailing comma#15545MichaReiser merged 1 commit intomainfrom
MichaReiser merged 1 commit intomainfrom
Conversation
bcc9793 to
9b94a2e
Compare
ffbaaf7 to
afcf589
Compare
9b94a2e to
3347f57
Compare
Contributor
|
3347f57 to
7146658
Compare
dhruvmanila
approved these changes
Jan 17, 2025
dcreager
added a commit
that referenced
this pull request
Jan 17, 2025
* main: [red-knot] Inline `SubclassOfType::as_instance_type_of_metaclass()` (#15556) [`flake8-comprehensions`] strip parentheses around generators in `unnecessary-generator-set` (`C401`) (#15553) [`pylint`] Implement `redefined-slots-in-subclass` (`W0244`) (#9640) [`flake8-bugbear`] Do not raise error if keyword argument is present and target-python version is less or equals than 3.9 (`B903`) (#15549) [red-knot] `type[T]` is disjoint from `type[S]` if the metaclass of `T` is disjoint from the metaclass of `S` (#15547) [red-knot] Pure instance variables declared in class body (#15515) Update snapshots of #15507 with new annotated snipetts rendering (#15546) [`pylint`] Do not report methods with only one `EM101`-compatible `raise` (`PLR6301`) (#15507) Fix unstable f-string formatting for expressions containing a trailing comma (#15545) Support `knot.toml` files in project discovery (#15505) Add support for configuring knot in `pyproject.toml` files (#15493) Fix bracket spacing for single-element tuples in f-string expressions (#15537) [`flake8-simplify`] Do not emit diagnostics for expressions inside string type annotations (`SIM222`, `SIM223`) (#15405) [`flake8-pytest-style`] Do not emit diagnostics for empty `for` loops (`PT012`, `PT031`) (#15542)
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
Fixes #15536
The
RemoveSoftlinesBufferremoves soft line breaks andif_group_breakselements but itdoesn't remove
expand_parentelements. This can lead to instable formattingwhen f-strings are wrapped in a
groupbecause it makes that group expand. For example,print(f"{ {}, 1, }")first gets formatted toand only then converges at
print(f"{ {}, 1 }").I first tried to also remove
expand_parentelemnents in theRemoveSoftLinesBufferbut that results in many failing tests around assignment formatting because
we rely on
Document::will_breakto determine if we should try the flatlayout and that only returns
trueif the f-string formatting contains theexpand_parentalong the trailing comma.I reviewed all
expand_parentusages and realized that the trailing comma is the onlyusage in our expression formatting. That's why I decided to adjust the trailing comma
formatting instead to omit the trailing comma if we know that we're in a flat f-string.
This is also how Dhruv implemented it originally before I removed the check in #14489 because I thought it
it no longer necessary.
Test Plan
Added regression test