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
Use ExprFString
for StringLike::FString
variant
#10311
Conversation
crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs
Outdated
Show resolved
Hide resolved
|
dad0e35
to
ab4b600
Compare
# Implicit string concatenation | ||
"0.0.0.0" f"0.0.0.0{expr}0.0.0.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not exactly correct (#10308) but for now it's fine.
StringLike::FString(ast::ExprFString { value, .. }) => { | ||
for part in value { | ||
match part { | ||
ast::FStringPart::Literal(literal) => { | ||
if &**literal == "0.0.0.0" { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(HardcodedBindAllInterfaces, literal.range())); | ||
} | ||
} | ||
ast::FStringPart::FString(f_string) => { | ||
for literal in f_string.literals() { | ||
if &**literal == "0.0.0.0" { | ||
checker.diagnostics.push(Diagnostic::new( | ||
HardcodedBindAllInterfaces, | ||
literal.range(), | ||
)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is being repeated for all three rules changed in this PR. The logic being:
- Loop over each f-string part
- If it's a string literal, then directly run the check on it
- If it's an f-string, loop over each literal and expression element of it
I'm not exactly sure on how to avoid duplicating this logic or if it's possible without introducing additional complexity.
ab4b600
to
67810b6
Compare
67810b6
to
1ef5e95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the flake8-pyi changes LGTM now, thanks!
Summary
This PR updates the
StringLike::FString
variant to useExprFString
instead ofFStringLiteralElement
.For context, the reason it used
FStringLiteralElement
is that the node is actually the string part of an f-string ("foo" inf"foo{x}"
). But, this is inconsistent with other variants where the captured value is the entire string.This is also problematic w.r.t. implicitly concatenated strings. Any rules which work with
StringLike::FString
doesn't account for the string part in an implicitly concatenated f-strings. For example, we don't flag confusable character in the first part of"𝐁ad" f"𝐁ad string"
, but only the second part (https://play.ruff.rs/16071c4c-a1dd-4920-b56f-e2ce2f69c843).Update
PYI053
This is included in this PR because otherwise it requires a temporary workaround to be compatible with the old logic.
This PR also updates the
PYI053
(string-or-bytes-too-long
) rule for f-string to consider all the visible characters in a f-string, including the ones which are implicitly concatenated. This is consistent with implicitly concatenated strings and bytes.For example,
This PR fixes it to consider all visible characters inside an f-string which includes expressions as well.
fixes: #10310
fixes: #10307
Test Plan
Add new test cases and update the snapshots.
Review
To facilitate the review process, the change have been split into two commits: one which has the code change while the other has the test cases and updated snapshots.