ruff
afcd7810 - [`pyupgrade`] Fix syntax error on string with newline escape and comment (`UP037`) (#22968)

Commit
25 days ago
[`pyupgrade`] Fix syntax error on string with newline escape and comment (`UP037`) (#22968) ## Summary Fixes a syntax error introduced by `UP037` when a quoted annotation contains a trailing comment. Fixes #19835. ## Problem The logic for detecting trailing comments in quoted annotations was brittle, relying on a fixed offset (`len() - 2`) from the end of the token stream. This failed to correctly identify comments in strings like `"A\n#"`, leading to invalid code generation where the closing parenthesis of the function definition was commented out. ## Approach Updated the logic in `crates/ruff_linter/src/rules/pyupgrade/rules/quoted_annotation.rs` to detect trailing comments. It now iterates backwards through the tokens of the annotation string, skipping any newline tokens, to find if the last significant token is a comment. ## Test Plan Added regression tests to `crates/ruff_linter/resources/test/fixtures/pyupgrade/UP037_0.py`. Verified with: `cargo test -p ruff_linter pyupgrade::tests`
Author
Parents
Loading