Skip to content

Comments

[flynt] Use triple quotes for joined raw strings with newlines (FLY002)#20197

Merged
ntBre merged 3 commits intoastral-sh:mainfrom
TaKO8Ki:fix-FLY002-autofix-error-with-raw-strings
Sep 18, 2025
Merged

[flynt] Use triple quotes for joined raw strings with newlines (FLY002)#20197
ntBre merged 3 commits intoastral-sh:mainfrom
TaKO8Ki:fix-FLY002-autofix-error-with-raw-strings

Conversation

@TaKO8Ki
Copy link
Contributor

@TaKO8Ki TaKO8Ki commented Sep 1, 2025

Summary

Fixes #19887

  • flynt(FLY002): When joining only string constants, upgrade raw single-quoted strings to raw triple-quoted if the resulting
    content contains a newline.
  • Choose a safe triple-quote delimiter by switching to the opposite quote style if the preferred triple appears inside the
    content.
  • Update FLY002 snapshot to include the \n'.join([r'line1','line2']) case.

Test Plan

I've added one test case to FLY002.py.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

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.

Thanks! I think we can simplify a bit, and there are also some edge cases we still need to handle.

@TaKO8Ki TaKO8Ki requested a review from ntBre September 11, 2025 18:43
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!

I'm a bit surprised that we don't offer a diagnostic at all if constructing the fix fails, but we can leave that as a separate issue too. I couldn't tell for sure from my brief read of the code, but maybe build_fstring is actually necessary to validate that the diagnostic applies too, not just the fix.

Comment on lines +97 to +105
// Prefer a delimiter that doesn't occur in the content; if both occur, bail.
if content.contains(flags.quote_str()) {
flags = flags.with_quote_style(flags.quote_style().opposite());
if content.contains(flags.quote_str()) {
// Both "'''" and "\"\"\"" are present in content; avoid emitting
// an invalid raw triple-quoted literal (or escaping). Bail on the fix.
return None;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think something like this would help with #19837 too, but it looks sufficiently different that we should leave it for a separate PR. I think we'd need this kind of check in one of the helpers below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok. I will try it in a different pull request:)

@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Sep 18, 2025
@ntBre ntBre changed the title [flynt] Use raw triple quotes for joined raw strings with newlines [flynt] Use triple quotes for joined raw strings with newlines (FLY002) Sep 18, 2025
@ntBre ntBre merged commit b4b5d67 into astral-sh:main Sep 18, 2025
37 checks passed
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.

FLY002 autofix with raw strings causes a syntax error

2 participants