ruff/crates/ruff_linter/resources/test/fixtures/flake8_async/ASYNC101.py
Mikko Leppänen 79f0522eb7
[flake8-async] Take pathlib.Path into account when analyzing async 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: https://github.com/astral-sh/ruff/issues/6892

## Test Plan

```bash
cargo test
```
2024-01-30 17:42:50 +00:00

87 lines
1 KiB
Python

import os
import subprocess
import time
from pathlib import Path
# Violation cases:
async def func():
open("foo")
async def func():
time.sleep(1)
async def func():
subprocess.run("foo")
async def func():
subprocess.call("foo")
async def func():
subprocess.foo(0)
async def func():
os.wait4(10)
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