-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[ruff
] Reduce false positives for missing-fstring-syntax
(RUF027
) by analyzing references to the binding when the string is assigned to a variable
#13076
base: main
Are you sure you want to change the base?
Conversation
…`) by analyzing references to the binding when the string is assigned to a variable
25cd016
to
fdf8fc6
Compare
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF027 | 20 | 0 | 20 | 0 | 0 |
f09ec0b
to
d4ada3f
Compare
CodSpeed Performance ReportMerging #13076 will not alter performanceComparing Summary
|
That's 5 false positives going away, so the PR is "working". I was hoping for the impact to be a little bigger, though. I suppose cases like this aren't counted as "simple assignments" according to the logic I added because the r.h.s. is a conditional expression rather than just a string literal. This is slightly underwhelming, but I think it's still an improvement. I think it's probably worth the extra complexity, but not sure. |
45fa17a
to
09edde8
Compare
Summary
When we tried to stabilise RUF027 for the 0.6 release earlier this month, the ecosystem report revealed lots of false positives along the lines of this:
A human can see quite clearly that this isn't meant to be an f-string, since the variable it's assigned to immediately has
.format()
called on it. Because of the indirection in the code, however, the logic for RUF027 can't see that right now, since it only checks for methods being directly called on strings with braces; it doesn't check for methods being called on symbols that have strings with braces as their assigned value.This PR tackles these false positives by splitting the check into two functions. If we detect that a string literal is part of a "simple assignment" (
x: str = "foo"
orx = "foo"
), we defer checking whether it's an "f-string with thef
" until the entire AST has been visited. This enables us to iterate through all the references to the binding and check whether any of them are used in ways that make it clear the string is intended to be a template string rather than an f-string. (For string literals that are not inside simple assignments, we analyze them as we did before, while we're doing our initial traversal of the AST.)Test Plan
I added some new fixtures, but I won't consider this PR a success unless it results in some false positives going away from the ecosystem report. Otherwise, I don't think it's worth the added complexity.