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

[ruff] Hex code formatting restrictions around debug f-strings are too strict on preview #14926

Closed
MeGaGiGaGon opened this issue Dec 11, 2024 · 1 comment
Labels
formatter Related to the formatter

Comments

@MeGaGiGaGon
Copy link

With preview disabled playground link:

# input:
f"{"\xFF\N{space}"=}"
# output:
f"{"\xff\N{SPACE}"=}"

With preview enabled playground link:

# input:
f"{"\xFF\N{space}"=}"
# output:
f"{"\xFF\N{space}"=}"

This change isn't observable, since the replacement of escapes happens before the debug applies:

>>> f"{"\xFF\N{space}"=}"
'"ÿ "=\'ÿ \''
>>> f"{"\xff\N{SPACE}"=}"
'"ÿ "=\'ÿ \''

The issue with #14766 is that the change in formatting happens inside an r-string, not specifically the f-string debug. It should be fine to apply this formatting with the usual rules on string types, regardless of where the string is.
For completeness I have also previously opened the same issue in black, however it looks unlikely for black to have this fixed by the 2025 release, or anytime soon, as it would require resolving outstanding questions around how to handle f-string formatting.
Also cc @MichaReiser

@MichaReiser MichaReiser added the formatter Related to the formatter label Dec 12, 2024
@MichaReiser
Copy link
Member

Thanks for reporting this and the nice write up. This is interesting. While this is a missed opportunity, I don't think it's important for us to fix it because debug expressions are rare (especially in production code), and escapes are rare, too. It's also somewhat complicated to fix because we currently don't apply any formatting for debug expressions. We just preserve them as is and I think that's a good enough default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

No branches or pull requests

2 participants