-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
flake8-async
] Take pathlib.Path
into account when analyzing asyn…
…c functions (#9703) ## Summary This review contains a fix for [ASYNC101](https://docs.astral.sh/ruff/rules/open-sleep-or-subprocess-in-async-function/) (open-sleep-or-subprocess-in-async-function) The problem is that ruff does not take open calls from pathlib.Path into account in async functions. Path.open() call is still a blocking call. In addition, PTH123 suggests to use pathlib.Path instead of os.open. So this might create an additional confusion. See: #6892 ## Test Plan ```bash cargo test ```
- Loading branch information
1 parent
0c8d140
commit 79f0522
Showing
3 changed files
with
207 additions
and
61 deletions.
There are no files selected for viewing
70 changes: 63 additions & 7 deletions
70
crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC101.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,87 @@ | ||
import os | ||
import subprocess | ||
import time | ||
from pathlib import Path | ||
|
||
# Violation cases: | ||
|
||
async def foo(): | ||
|
||
async def func(): | ||
open("foo") | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
time.sleep(1) | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
subprocess.run("foo") | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
subprocess.call("foo") | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
subprocess.foo(0) | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
os.wait4(10) | ||
|
||
|
||
async def foo(): | ||
async def func(): | ||
os.wait(12) | ||
|
||
|
||
# Violation cases for pathlib: | ||
|
||
|
||
async def func(): | ||
Path("foo").open() # ASYNC101 | ||
|
||
|
||
async def func(): | ||
p = Path("foo") | ||
p.open() # ASYNC101 | ||
|
||
|
||
async def func(): | ||
with Path("foo").open() as f: # ASYNC101 | ||
pass | ||
|
||
|
||
async def func() -> None: | ||
p = Path("foo") | ||
|
||
async def bar(): | ||
p.open() # ASYNC101 | ||
|
||
|
||
async def func() -> None: | ||
(p1, p2) = (Path("foo"), Path("bar")) | ||
|
||
p1.open() # ASYNC101 | ||
|
||
|
||
# Non-violation cases for pathlib: | ||
|
||
|
||
class Foo: | ||
def open(self): | ||
pass | ||
|
||
|
||
async def func(): | ||
Foo().open() # OK | ||
|
||
|
||
async def func(): | ||
def open(): | ||
pass | ||
|
||
open() # OK | ||
|
||
|
||
def func(): | ||
Path("foo").open() # OK |
This file contains 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
80 changes: 59 additions & 21 deletions
80
...flake8_async/snapshots/ruff_linter__rules__flake8_async__tests__ASYNC101_ASYNC101.py.snap
This file contains 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