Skip to content

[ruff] Treat f-string interpolation as potential side effect in RUF019#24426

Merged
MichaReiser merged 5 commits intoastral-sh:mainfrom
anishgirianish:fix-ruf019-fstring-side-effects
Apr 9, 2026
Merged

[ruff] Treat f-string interpolation as potential side effect in RUF019#24426
MichaReiser merged 5 commits intoastral-sh:mainfrom
anishgirianish:fix-ruf019-fstring-side-effects

Conversation

@anishgirianish
Copy link
Copy Markdown
Contributor

@anishgirianish anishgirianish commented Apr 6, 2026

Summary

Fixes #12953

F-string interpolation can call __format__/__str__/__repr__, which may have side effects. RUF019 was applying a safe auto-fix that collapsed two __str__ calls into one, changing behavior.

Added a tri-state SideEffect enum (No/Maybe/Yes) and a side_effect() function that reuses the existing any_over_expr traversal via a new FnMut variant (any_over_expr_mut), following the approach suggested by @ntBre in the other closed pr tagged in the issue

In RUF019, SideEffect::Maybe (non-literal f-string interpolation) now produces an unsafe fix instead of a safe one. Literal interpolations like f"{1}" remain safe.

Test Plan

  • Added f-string fixture cases to RUF019.py (non-literal → unsafe, literal → safe, no interpolation → safe).
  • cargo nextest run -p ruff_linter
  • Ecosystem check (stable + preview)

@astral-sh-bot astral-sh-bot bot requested a review from amyreese April 6, 2026 01:03
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Apr 6, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser MichaReiser force-pushed the fix-ruf019-fstring-side-effects branch from e27a024 to c2b5a83 Compare April 7, 2026 12:28
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I think this is somewhat pedantic but it's probably fine for RUF019. I left a few smaller nit comments on how we can improve the code.

@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Apr 7, 2026
@anishgirianish
Copy link
Copy Markdown
Contributor Author

@MichaReiser Thank you so much for the review! Addressed all feedback, refactored to SideEffect::from_expr with a match, renamed variants to Absent/Possible/Present, added Named as a side effect, and updated the fixture with a class that has a side-effectful str. I really appreciate the suggestions. Ready for re-review whenever you have a moment.

Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This is great. Thank you. A few more smaller nits.

@anishgirianish
Copy link
Copy Markdown
Contributor Author

anishgirianish commented Apr 9, 2026

@MichaReiser thank you so much for the review. Addressed all three feedback, removed Option from from_expr, made both matches exhaustive, added const fn. Ready for another look whenever you get a chance.

thank you

@MichaReiser MichaReiser merged commit 87a0f01 into astral-sh:main Apr 9, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fixes Related to suggested fixes for violations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

contains_effect should consider f-strings to have effects

3 participants