Skip to content

Comments

[pyupgrade] Fix handling of \N in raw strings (UP032)#22149

Merged
ntBre merged 6 commits intoastral-sh:mainfrom
denyszhak:fix/up032-raw-n-escape
Feb 20, 2026
Merged

[pyupgrade] Fix handling of \N in raw strings (UP032)#22149
ntBre merged 6 commits intoastral-sh:mainfrom
denyszhak:fix/up032-raw-n-escape

Conversation

@denyszhak
Copy link
Contributor

Summary:

Fixes UP032 autofix incorrectly converting raw strings with \N{...} to f-strings, which changes semantics and causes runtime errors.

Fixes #22060

Test Plan

  • Added test case for raw strings with \N{...}
  • Regular strings with \N{...} still autofix correctly
  • All 119 pyupgrade tests pass

@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 22, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dscorbett
Copy link

This PR suppresses the fix when a raw string contains \N{. That is suboptimal because there is no need to suppress the fix. For example, r"\N{angle}AOB = {angle}°".format(angle=180) can safely be fixed to rf"\N{180}AOB = {180}°". Instead, a better solution is to suppress for raw strings the pending_escape logic introduced in #21901.

@denyszhak denyszhak marked this pull request as draft December 25, 2025 12:08
@denyszhak denyszhak force-pushed the fix/up032-raw-n-escape branch from 25968ba to c338ba8 Compare December 31, 2025 01:27
@denyszhak denyszhak force-pushed the fix/up032-raw-n-escape branch from c338ba8 to 383d400 Compare January 3, 2026 23:37
@denyszhak denyszhak marked this pull request as ready for review January 3, 2026 23:49
@denyszhak
Copy link
Contributor Author

This PR suppresses the fix when a raw string contains \N{. That is suboptimal because there is no need to suppress the fix. For example, r"\N{angle}AOB = {angle}°".format(angle=180) can safely be fixed to rf"\N{180}AOB = {180}°". Instead, a better solution is to suppress for raw strings the pending_escape logic introduced in #21901.

@dscorbett @ntBre does it make sense now?

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 6, 2026
@ntBre ntBre self-requested a review February 6, 2026 17:02
Copy link
Contributor

@ntBre ntBre 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! This makes sense to me overall. I just had a suggestion about the FormatString API.

@ntBre ntBre changed the title fix: avoid autofix for raw strings with \N escapes [pyupgrade] Fix handling of \N in raw strings (UP032) Feb 20, 2026
@ntBre ntBre enabled auto-merge (squash) February 20, 2026 15:10
@ntBre ntBre merged commit 1d94f26 into astral-sh:main Feb 20, 2026
41 checks passed
@denyszhak
Copy link
Contributor Author

@ntBre sorry I somehow missed your response, PR stayed silent and some time after I just stopped checking and only now noticed

I agree with your comments and than you for pushing those commits!

knutwannheden pushed a commit to openrewrite/ruff that referenced this pull request Feb 20, 2026
…h#22149)

**Summary:**

Fixes UP032 autofix incorrectly converting raw strings with `\N{...}` to
f-strings, which changes semantics and causes runtime errors.

Fixes astral-sh#22060 

## Test Plan

- Added test case for raw strings with \N{...}
- Regular strings with \N{...} still autofix correctly
- All 119 pyupgrade tests pass

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UP032 fix misinterprets \N in a raw string as an escape sequence

3 participants