Use ExprFString for StringLike::FString variant#10311
Merged
dhruvmanila merged 4 commits intomainfrom Mar 14, 2024
Merged
Conversation
This was
linked to
issues
Mar 9, 2024
AlexWaygood
reviewed
Mar 9, 2024
crates/ruff_linter/src/rules/flake8_pyi/rules/string_or_bytes_too_long.rs
Show resolved
Hide resolved
Contributor
|
dad0e35 to
ab4b600
Compare
dhruvmanila
commented
Mar 13, 2024
Comment on lines
+23
to
+24
| # Implicit string concatenation | ||
| "0.0.0.0" f"0.0.0.0{expr}0.0.0.0" |
Member
Author
There was a problem hiding this comment.
This is not exactly correct (#10308) but for now it's fine.
Comment on lines
+49
to
+71
| 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(), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
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
charliermarsh
approved these changes
Mar 14, 2024
67810b6 to
1ef5e95
Compare
AlexWaygood
approved these changes
Mar 14, 2024
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
the flake8-pyi changes LGTM now, thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR updates the
StringLike::FStringvariant to useExprFStringinstead ofFStringLiteralElement.For context, the reason it used
FStringLiteralElementis 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::FStringdoesn'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
PYI053This 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.