Visitar URL original
Normalize hex codes in unicode escape sequences in f-strings (#4523) by Prateek99-s · Pull Request #5526 · psf/black · GitHub
Skip to content

Normalize hex codes in unicode escape sequences in f-strings (#4523) - #5526

Merged
cobaltt7 merged 2 commits into
psf:mainfrom
Prateek99-s:fix-fstring-unicode-hex-4523
Oct 22, 2026
Merged

cobaltt7 merged 2 commits into
psf:mainfrom
Prateek99-s:fix-fstring-unicode-hex-4523

Conversation

@Prateek99-s

Copy link
Copy Markdown
Contributor

Closes #4523

Summary

When PR #4401 prevented quotes from normalizing inside f-strings containing backslashes (to avoid syntax errors like f"{'\''}" turning into f"{'''}"), it bypassed visit_STRING, completely suppressing normalize_unicode_escape_sequences() for f-strings.

This PR introduces normalize_fstring_unicode_escape_sequences() to safely normalize hex codes in unicode escape sequences (e.g. \xFF -> \xff, \u00A0 -> \u00a0, \N{...} -> uppercase) within f-strings and t-strings while preserving AST equivalence:

  • Raw f-strings: Skips template parts when the f-string prefix contains r/R (e.g. rf"...").
  • Raw string literals in replacement fields: Preserves expressions like f"{r'\xFF'}" without mutating escaped characters, preventing runtime value changes and ASTSafetyError.
  • Debug f-strings: Leaves expressions preceding = intact (e.g. f"{'\xFF'=}") since Python prints the literal source representation before = at runtime.
  • Nested templates: Recursively handles nested f-strings and t-strings.

Test Plan

  • Added cases to pep_701.py covering raw f-strings, nested f-strings, debug f-strings (=), raw string literals in fields, \N{...}, \u..., and \U....
  • Added test case to format_unicode_escape_seq.py.
  • Verified full test suite (pytest) and linters (flake8, black --check).

@Prateek99-s
Prateek99-s force-pushed the fix-fstring-unicode-hex-4523 branch from c85487b to 44edd5b Compare October 12, 2026 07:36
@github-actions

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (44edd5b) to main (11cff13):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@cobaltt7

Copy link
Copy Markdown
Collaborator

Thanks!

@cobaltt7
cobaltt7 merged commit 158aa3c into psf:main Oct 22, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hex_codes_in_unicode_sequences: restrictions around f-strings are too strict

2 participants