Skip to content

Commit

Permalink
Implement flake8-async plugin (#4432)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdegraaf authored May 15, 2023
1 parent 2c6efc2 commit 8ba9eb8
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 3 deletions.
25 changes: 25 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,31 @@ are:
SOFTWARE.
"""

- flake8-async, licensed as follows:
"""
MIT License

Copyright (c) 2022 Cooper Lees

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

- flake8-type-checking, licensed as follows:
"""
Copyright (c) 2021, Sondre Lillebø Gundersen
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ quality tools, including:
- [eradicate](https://pypi.org/project/eradicate/)
- [flake8-2020](https://pypi.org/project/flake8-2020/)
- [flake8-annotations](https://pypi.org/project/flake8-annotations/)
- [flake8-async](https://pypi.org/project/flake8-async)
- [flake8-bandit](https://pypi.org/project/flake8-bandit/) ([#1646](https://github.com/charliermarsh/ruff/issues/1646))
- [flake8-blind-except](https://pypi.org/project/flake8-blind-except/)
- [flake8-boolean-trap](https://pypi.org/project/flake8-boolean-trap/)
Expand Down
23 changes: 23 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_async/ASYNC100.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import urllib.request
import requests
import httpx


async def foo():
urllib.request.urlopen("http://example.com/foo/bar").read()


async def foo():
requests.get()


async def foo():
httpx.get()


async def foo():
requests.post()


async def foo():
httpx.post()
31 changes: 31 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_async/ASYNC101.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import subprocess
import time


async def foo():
open("foo")


async def foo():
time.sleep(1)


async def foo():
subprocess.run("foo")


async def foo():
subprocess.call("foo")


async def foo():
subprocess.foo(0)


async def foo():
os.wait4(10)


async def foo():
os.wait(12)
13 changes: 13 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_async/ASYNC102.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os


async def foo():
os.popen()


async def foo():
os.spawnl()


async def foo():
os.fspath("foo")
29 changes: 26 additions & 3 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ use crate::importer::Importer;
use crate::noqa::NoqaMapping;
use crate::registry::{AsRule, Rule};
use crate::rules::{
flake8_2020, flake8_annotations, flake8_bandit, flake8_blind_except, flake8_boolean_trap,
flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez, flake8_debugger,
flake8_django, flake8_errmsg, flake8_future_annotations, flake8_gettext,
flake8_2020, flake8_annotations, flake8_async, flake8_bandit, flake8_blind_except,
flake8_boolean_trap, flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez,
flake8_debugger, flake8_django, flake8_errmsg, flake8_future_annotations, flake8_gettext,
flake8_implicit_str_concat, flake8_import_conventions, flake8_logging_format, flake8_pie,
flake8_print, flake8_pyi, flake8_pytest_style, flake8_raise, flake8_return, flake8_self,
flake8_simplify, flake8_tidy_imports, flake8_type_checking, flake8_unused_arguments,
Expand Down Expand Up @@ -2584,6 +2584,29 @@ where
pyupgrade::rules::use_pep604_isinstance(self, expr, func, args);
}

// flake8-async
if self
.settings
.rules
.enabled(Rule::BlockingHttpCallInAsyncFunction)
{
flake8_async::rules::blocking_http_call(self, expr);
}
if self
.settings
.rules
.enabled(Rule::OpenSleepOrSubprocessInAsyncFunction)
{
flake8_async::rules::open_sleep_or_subprocess_call(self, expr);
}
if self
.settings
.rules
.enabled(Rule::BlockingOsCallInAsyncFunction)
{
flake8_async::rules::blocking_os_call(self, expr);
}

// flake8-print
if self
.settings
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<Rule> {
(Pylint, "W3301") => Rule::NestedMinMax,
(Pylint, "E0241") => Rule::DuplicateBases,

// flake8-async
(Flake8Async, "100") => Rule::BlockingHttpCallInAsyncFunction,
(Flake8Async, "101") => Rule::OpenSleepOrSubprocessInAsyncFunction,
(Flake8Async, "102") => Rule::BlockingOsCallInAsyncFunction,

// flake8-builtins
(Flake8Builtins, "001") => Rule::BuiltinVariableShadowing,
(Flake8Builtins, "002") => Rule::BuiltinArgumentShadowing,
Expand Down
7 changes: 7 additions & 0 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ ruff_macros::register_rules!(
rules::pylint::rules::UnexpectedSpecialMethodSignature,
rules::pylint::rules::NestedMinMax,
rules::pylint::rules::DuplicateBases,
// flake8-async
rules::flake8_async::rules::BlockingHttpCallInAsyncFunction,
rules::flake8_async::rules::OpenSleepOrSubprocessInAsyncFunction,
rules::flake8_async::rules::BlockingOsCallInAsyncFunction,
// flake8-builtins
rules::flake8_builtins::rules::BuiltinVariableShadowing,
rules::flake8_builtins::rules::BuiltinArgumentShadowing,
Expand Down Expand Up @@ -735,6 +739,9 @@ pub enum Linter {
/// [flake8-annotations](https://pypi.org/project/flake8-annotations/)
#[prefix = "ANN"]
Flake8Annotations,
/// [flake8-async](https://pypi.org/project/flake8-async/)
#[prefix = "ASYNC"]
Flake8Async,
/// [flake8-bandit](https://pypi.org/project/flake8-bandit/)
#[prefix = "S"]
Flake8Bandit,
Expand Down
28 changes: 28 additions & 0 deletions crates/ruff/src/rules/flake8_async/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Rules from [flake8-async](https://pypi.org/project/flake8-async/).
pub(crate) mod rules;

#[cfg(test)]
mod tests {
use std::path::Path;

use anyhow::Result;
use test_case::test_case;

use crate::assert_messages;
use crate::registry::Rule;
use crate::settings::Settings;
use crate::test::test_path;

#[test_case(Rule::BlockingHttpCallInAsyncFunction, Path::new("ASYNC100.py"); "ASYNC100")]
#[test_case(Rule::OpenSleepOrSubprocessInAsyncFunction, Path::new("ASYNC101.py"); "ASYNC101")]
#[test_case(Rule::BlockingOsCallInAsyncFunction, Path::new("ASYNC102.py"); "ASYNC102")]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("flake8_async").join(path).as_path(),
&Settings::for_rule(rule_code),
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}
Loading

0 comments on commit 8ba9eb8

Please sign in to comment.