Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slice source code instead of generating it for EM fixes #7746

Merged
merged 1 commit into from
Nov 9, 2023

Conversation

dhruvmanila
Copy link
Member

Summary

This PR fixes the bug where the generated fix for EM* rules would replace a
triple-quoted (f-)string with a single-quoted (f-)string. This changes the
semantic of the string in case it contains a single-quoted string literal. This
is especially evident with f-strings where the expression could contain another
string within it. For example,

f"""normal {"another"} normal"""

Test Plan

Add test case for triple-quoted string and update the snapshots.

fixes: #6988
fixes: #7736

@dhruvmanila
Copy link
Member Author

dhruvmanila commented Oct 1, 2023

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@dhruvmanila dhruvmanila added the bug Something isn't working label Oct 1, 2023
@github-actions

This comment was marked as outdated.

@dhruvmanila dhruvmanila marked this pull request as draft October 1, 2023 19:36
Copy link
Contributor

github-actions bot commented Nov 9, 2023

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dhruvmanila dhruvmanila force-pushed the dhruv/use-locator-for-fix branch 2 times, most recently from ea28834 to 451acea Compare November 9, 2023 03:59
@dhruvmanila dhruvmanila marked this pull request as ready for review November 9, 2023 03:59
Comment on lines +280 to +285
format!(
"msg = ({line_ending}{stmt_indentation}{indentation}{}{line_ending}{stmt_indentation}){line_ending}{stmt_indentation}",
locator.slice(exc_arg.range()),
line_ending = stylist.line_ending().as_str(),
indentation = stylist.indentation().as_str(),
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Is this too much? 😅

We can just keep a simple format!("msg = ({}){}{}") instead.

74 74 |
75 75 | def f_multi_line_string2():
76 |- raise RuntimeError(
76 |+ msg = (
Copy link
Member

Choose a reason for hiding this comment

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

So these probably should be omitted, right? But we don't have a general way to know if the inner expression needs parentheses, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, that's correct.

@dhruvmanila dhruvmanila merged commit 9d167a1 into main Nov 9, 2023
16 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/use-locator-for-fix branch November 9, 2023 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule EM102 cause panic assertion failed: self.start_locations.is_empty() Rules EM102 cause autofix error
2 participants