Skip to content
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

Fix fstring formatting removing overlong implicit concatenated string in expression part #14811

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Dec 6, 2024

Summary

Fixes #14778

The formatter incorrectly removed the inner implicitly concatenated string for following single-line f-string:

f"{'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' 'a' if True else ""}"

# formatted
f"{ if True else ''}"

This happened because I changed the RemoveSoftlinesBuffer in #14489 to remove any content wrapped in if_group_breaks. After all, it emulates an all flat layout. This works fine when if_group_breaks is only used to add content if the gorup breaks. It doesn't work if the same content is rendered differently depending on if the group fits using if_group_breaks and if_groups_fits because the enclosing group might still break if the entire content exceeds the line-length limit.

This PR fixes this by unwrapping any if_group_fits content by removing the if_group_fits start and end tags.

Test Plan

added test

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter preview Related to preview mode features labels Dec 6, 2024
@MichaReiser MichaReiser force-pushed the micha/fix-overlong-single-line-fstring-implicit-concatenated-string branch from 98d97b5 to 07de8f9 Compare December 6, 2024 10:31
Copy link
Contributor

github-actions bot commented Dec 6, 2024

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Copy link
Member

@dhruvmanila dhruvmanila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@MichaReiser MichaReiser merged commit 1559c73 into main Dec 6, 2024
21 checks passed
@MichaReiser MichaReiser deleted the micha/fix-overlong-single-line-fstring-implicit-concatenated-string branch December 6, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter preview Related to preview mode features
Projects
None yet
2 participants