-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-async] Implement blocking-path-method (ASYNC240)
#20264
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
[flake8-async] Implement blocking-path-method (ASYNC240)
#20264
Conversation
bc9d2a8 to
a16ea55
Compare
|
Thanks! We're pretty focused on the minor release this week, so I may not get a chance to review until next week. But this looks really good from a quick skim :) |
|
19b3ea6 to
be73d44
Compare
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Show resolved
Hide resolved
0950d6e to
20b5b46
Compare
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
blocking-path-method (ASYNC240).blocking-path-method (ASYNC240).
Adds a new rule to find and report use of `os.path` or `pathlib.Path` in async functions.
20b5b46 to
c5b5f27
Compare
This comment was marked as outdated.
This comment was marked as outdated.
blocking-path-method (ASYNC240).blocking-path-method (ASYNC240).
|
Bumping this PR for review again. |
I usually just push additional commits. That works better with GitHub's "Changes since your last review" button than comparing rebased commits. We always squash and merge PRs anyway. But it's not too big of a deal either way :) I'll give this another look now! |
ntBre
left a comment
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.
Thank you! This looks great to me overall, I just had a few minor comments. I especially liked the tests. They were really easy to read and review 😄
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_async/rules/blocking_path_methods.rs
Outdated
Show resolved
Hide resolved
| }; | ||
|
|
||
| if check_type::<PathlibPathChecker>(binding, semantic) { | ||
| if maybe_calling_io_operation(attr.id.as_str()) { |
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 the same attr as above right? I think we could do this check once before both the constructor and check_type checks, returning early if it's not one of the known attributes.
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.
Aha, good catch. I moved this a few checks above to just before the constructor check.
crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC240.py
Outdated
Show resolved
Hide resolved
| traverse_union_and_optional( | ||
| &mut |inner_expr, _| { | ||
| if Self::is_pathlib_path_constructor(semantic, inner_expr) { | ||
| found = true; | ||
| } | ||
| }, | ||
| semantic, | ||
| annotation, | ||
| ); |
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.
I do like having this check like in #20091, but I'm a little bit hesitant to add it here since it will affect other rules too. I would probably either revert this and accept that we miss option/union annotations for now, until we possibly update the TypeChecker infrastructure more generally in the future, or move this check into the rule itself, as @amyreese did for ASYNC212.
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.
Makes sense, for now I added a copy of PathlibPathChecker that has the traversing logic to this rule.
This also reverts back the changes made to `PathlibPathChecker` in the previous commit.
blocking-path-method (ASYNC240).blocking-path-method (ASYNC240).
|
Thank you for doing another pass on this! Will post another comment once this is ready for review again. |
caedfa8 to
c594fb8
Compare
This commit contains changes addressing some code reviews: - The `Violation::message` implementation doesn't mention that the linter only look for blocking path methods and non-blocking methods are allowed. I've tweaked the comments and added another code example of async function using non-blocking os/pathlib path methods. - The `is_calling_os_path_method` function could be simpler. I've used the suggested implementation and pulled it out from the function since it's only one line now. - The logic to see if an expression is calling a method form the pathlib.Path module could be simpler if it calls `PathlibPathChecker::match_initializer` directly. - The rule function can return early if `maybe_calling_io_operation` returns false when checking the expressions attribute.
This commit contains changes addressing some code reviews: - Updated `maybe_calling_io_operation` to have a more complete allow list. - ASYNC230 already handles `path.open()` case, so it is added to the allow list. There is a new test for that. - Fix typo.
Move the main rule implementation to just below the `BlockingPathMethodInAsyncFunction` implementation.
c594fb8 to
f31d0b6
Compare
blocking-path-method (ASYNC240).blocking-path-method (ASYNC240).
|
@ntBre -- Marking this as ready for you to take a look again! |
amyreese
left a comment
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.
looks good to me!
ntBre
left a comment
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.
Thank you, this looks good to me too! I just had one comment about moving the todo comment to a separate issue for a follow-up discussion.
| // match_annotation doesn't check `Path` in union | ||
| // or `Optional`. | ||
| // TODO: Once we have updated it to handle those | ||
| // cases, update `blocking_path_method.rs`(ASYNC240) | ||
| // to use this version of `PathlibPathChecker`. |
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.
I think it's good to record this TODO somewhere, but I'm kind of leaning toward opening a separate issue and deleting this comment. I feel like we should probably either always traverse unions or never traverse unions and apply that decision to all of our TypeCheckers rather than keep a mix of them.
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.
Okay. This commit removes the comment, Remove comment on PathlibPathChecker.
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.
And opened #20529 to keep track of this.
ntBre
left a comment
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.
Thank you!
blocking-path-method (ASYNC240).flake8-async] Implement blocking-path-method (ASYNC240)
|
Awesome! Thank you |
Summary
Adds a new rule to find and report use of
os.pathorpathlib.Pathinasync functions.
Issue: #8451
Test Plan
Using
cargo insta test