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 curly bracket spacing around curly f-string expressions #15471

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Jan 14, 2025

Summary

Fixes #15459

F-string expression need to leave one space between the element's { or } and any curly braces inside the expression itself or it results in invalid syntax:

f"{ {1, 2, 3} }"  # valid
f"{{ 1, 2, 3}}"  # invalid

The existing implementation handles this correctly if the expression itself has curly braces, but it doesn't add the necessary space if the expression starts or ends with an expression with curly parentheses:

-print(f"{ {1, 2, 3} - {2} }")
-print(f"{ {1: 2}.keys() }")
+print(f"{{1, 2, 3} - {2}}")
+print(f"{{1: 2}.keys()}")

This PR changes the formatter to check if the left-most sub expression (if any) starts with curly parentheses instead of just checking the expression itself.

Test Plan

Added snapshot tests.

@MichaReiser MichaReiser added bug Something isn't working formatter Related to the formatter labels Jan 14, 2025
Copy link
Contributor

github-actions bot commented Jan 14, 2025

ruff-ecosystem results

Formatter (stable)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

warning: Detected debug build without --no-cache.
error: Failed to read examples/Assistants_API_overview_python.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: expected `,` or `]` at line 197 column 8

Formatter (preview)

ℹ️ ecosystem check encountered format errors. (no format changes; 1 project error)

openai/openai-cookbook (error)

ruff format --preview

warning: Detected debug build without --no-cache.
error: Failed to read examples/Assistants_API_overview_python.ipynb: Expected a Jupyter Notebook, which must be internally stored as JSON, but this file isn't valid JSON: expected `,` or `]` at line 197 column 8

@MichaReiser MichaReiser marked this pull request as ready for review January 14, 2025 10:23
@MichaReiser MichaReiser force-pushed the micha/formatter-fix-f-string-curly-spacing branch from 41004b9 to 7315f62 Compare January 15, 2025 08:10
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.

Thank you!

@MichaReiser MichaReiser merged commit 96c2d09 into main Jan 15, 2025
21 checks passed
@MichaReiser MichaReiser deleted the micha/formatter-fix-f-string-curly-spacing branch January 15, 2025 08:22
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Broken f-string formatting after ruff 0.9
2 participants