Skip to content
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] Update ASYNC116 to match upstream #12266

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,56 @@ async def import_from_trio():

# catch from import
await sleep(86401) # error: 116, "async"


async def import_anyio():
import anyio

# These examples are probably not meant to ever wake up:
await anyio.sleep(100000) # error: 116, "async"

# 'inf literal' overflow trick
await anyio.sleep(1e999) # error: 116, "async"

await anyio.sleep(86399)
await anyio.sleep(86400)
await anyio.sleep(86400.01) # error: 116, "async"
await anyio.sleep(86401) # error: 116, "async"

await anyio.sleep(-1) # will raise a runtime error
await anyio.sleep(0) # handled by different check

# these ones _definitely_ never wake up (TODO)
await anyio.sleep(float("inf"))
await anyio.sleep(math.inf)
await anyio.sleep(inf)

# don't require inf to be in math (TODO)
await anyio.sleep(np.inf)

# don't evaluate expressions (TODO)
one_day = 86401
await anyio.sleep(86400 + 1)
await anyio.sleep(60 * 60 * 24 + 1)
await anyio.sleep(foo())
await anyio.sleep(one_day)
await anyio.sleep(86400 + foo())
await anyio.sleep(86400 + ...)
await anyio.sleep("hello")
await anyio.sleep(...)


def not_async_fun():
import anyio

# does not require the call to be awaited, nor in an async fun
anyio.sleep(86401) # error: 116, "async"
# also checks that we don't break visit_Call
anyio.run(anyio.sleep(86401)) # error: 116, "async"


async def import_from_anyio():
from anyio import sleep

# catch from import
await sleep(86401) # error: 116, "async"
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
if checker.enabled(Rule::BlockingSleepInAsyncFunction) {
flake8_async::rules::blocking_sleep(checker, call);
}
if checker.enabled(Rule::SleepForeverCall) {
flake8_async::rules::sleep_forever_call(checker, call);
if checker.enabled(Rule::LongSleepNotForever) {
flake8_async::rules::long_sleep_not_forever(checker, call);
}
if checker.any_enabled(&[Rule::Print, Rule::PPrint]) {
flake8_print::rules::print_call(checker, call);
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8Async, "109") => (RuleGroup::Stable, rules::flake8_async::rules::AsyncFunctionWithTimeout),
(Flake8Async, "110") => (RuleGroup::Stable, rules::flake8_async::rules::AsyncBusyWait),
(Flake8Async, "115") => (RuleGroup::Stable, rules::flake8_async::rules::TrioZeroSleepCall),
(Flake8Async, "116") => (RuleGroup::Preview, rules::flake8_async::rules::SleepForeverCall),
(Flake8Async, "116") => (RuleGroup::Preview, rules::flake8_async::rules::LongSleepNotForever),
(Flake8Async, "210") => (RuleGroup::Stable, rules::flake8_async::rules::BlockingHttpCallInAsyncFunction),
(Flake8Async, "220") => (RuleGroup::Stable, rules::flake8_async::rules::CreateSubprocessInAsyncFunction),
(Flake8Async, "221") => (RuleGroup::Stable, rules::flake8_async::rules::RunProcessInAsyncFunction),
Expand Down
3 changes: 2 additions & 1 deletion crates/ruff_linter/src/rules/flake8_async/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod tests {
#[test_case(Rule::AsyncFunctionWithTimeout, Path::new("ASYNC109_1.py"))]
#[test_case(Rule::AsyncBusyWait, Path::new("ASYNC110.py"))]
#[test_case(Rule::TrioZeroSleepCall, Path::new("ASYNC115.py"))]
#[test_case(Rule::SleepForeverCall, Path::new("ASYNC116.py"))]
#[test_case(Rule::LongSleepNotForever, Path::new("ASYNC116.py"))]
#[test_case(Rule::BlockingHttpCallInAsyncFunction, Path::new("ASYNC210.py"))]
#[test_case(Rule::CreateSubprocessInAsyncFunction, Path::new("ASYNC22x.py"))]
#[test_case(Rule::RunProcessInAsyncFunction, Path::new("ASYNC22x.py"))]
Expand All @@ -42,6 +42,7 @@ mod tests {
#[test_case(Rule::AsyncFunctionWithTimeout, Path::new("ASYNC109_0.py"))]
#[test_case(Rule::AsyncFunctionWithTimeout, Path::new("ASYNC109_1.py"))]
#[test_case(Rule::AsyncBusyWait, Path::new("ASYNC110.py"))]
#[test_case(Rule::LongSleepNotForever, Path::new("ASYNC116.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast::{Expr, ExprCall, ExprNumberLiteral, Number};
use ruff_python_semantic::Modules;
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::rules::flake8_async::helpers::AsyncModule;

/// ## What it does
/// Checks for uses of `trio.sleep()` or `anyio.sleep()` with a delay greater than 24 hours.
///
/// ## Why is this bad?
/// Calling `sleep()` with a delay greater than 24 hours is usually intended
/// to sleep indefinitely. Instead of using a large delay,
/// `trio.sleep_forever()` or `anyio.sleep_forever()` better conveys the intent.
///
///
/// ## Example
/// ```python
/// import trio
///
///
/// async def func():
/// await trio.sleep(86401)
/// ```
///
/// Use instead:
/// ```python
/// import trio
///
///
/// async def func():
/// await trio.sleep_forever()
/// ```
#[violation]
pub struct LongSleepNotForever {
module: AsyncModule,
}

impl Violation for LongSleepNotForever {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
let Self { module } = self;
format!(
"`{module}.sleep()` with >24 hour interval should usually be `{module}.sleep_forever()`"
)
}

fn fix_title(&self) -> Option<String> {
let Self { module } = self;
Some(format!("Replace with `{module}.sleep_forever()`"))
}
}

/// ASYNC116
pub(crate) fn long_sleep_not_forever(checker: &mut Checker, call: &ExprCall) {
if !(checker.semantic().seen_module(Modules::TRIO)
|| checker.semantic().seen_module(Modules::ANYIO))
{
return;
}

if call.arguments.len() != 1 {
return;
}

let Some(arg) = call.arguments.find_argument("seconds", 0) else {
return;
};

let Some(qualified_name) = checker
.semantic()
.resolve_qualified_name(call.func.as_ref())
else {
return;
};

if !matches!(qualified_name.segments(), ["trio" | "anyio", "sleep"]) {
return;
}

let Expr::NumberLiteral(ExprNumberLiteral { value, .. }) = arg else {
return;
};

// TODO(ekohilas): Replace with Duration::from_days(1).as_secs(); when available.
let one_day_in_secs = 60 * 60 * 24;
match value {
Number::Int(int_value) => {
let Some(int_value) = int_value.as_u64() else {
return;
};
if int_value <= one_day_in_secs {
return;
}
}
Number::Float(float_value) =>
{
#[allow(clippy::cast_precision_loss)]
if *float_value <= one_day_in_secs as f64 {
return;
}
}
Number::Complex { .. } => return,
}

let module = AsyncModule::try_from(&qualified_name).unwrap();
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
if checker.settings.preview.is_enabled() {
let mut diagnostic = Diagnostic::new(LongSleepNotForever { module }, call.range());
let replacement_function = "sleep_forever";
diagnostic.try_set_fix(|| {
let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import_from(&module.to_string(), replacement_function),
call.func.start(),
checker.semantic(),
)?;
let reference_edit = Edit::range_replacement(binding, call.func.range());
let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.range());
Ok(Fix::unsafe_edits(import_edit, [reference_edit, arg_edit]))
});
checker.diagnostics.push(diagnostic);
} else {
if matches!(module, AsyncModule::Trio) {
let mut diagnostic = Diagnostic::new(LongSleepNotForever { module }, call.range());
let replacement_function = "sleep_forever";
diagnostic.try_set_fix(|| {
let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import_from("trio", replacement_function),
call.func.start(),
checker.semantic(),
)?;
let reference_edit = Edit::range_replacement(binding, call.func.range());
let arg_edit = Edit::range_replacement("()".to_string(), call.arguments.range());
Ok(Fix::unsafe_edits(import_edit, [reference_edit, arg_edit]))
});
checker.diagnostics.push(diagnostic);
}
}
}
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/flake8_async/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) use blocking_open_call::*;
pub(crate) use blocking_process_invocation::*;
pub(crate) use blocking_sleep::*;
pub(crate) use cancel_scope_no_checkpoint::*;
pub(crate) use sleep_forever_call::*;
pub(crate) use long_sleep_not_forever::*;
pub(crate) use sync_call::*;
pub(crate) use zero_sleep_call::*;

Expand All @@ -16,6 +16,6 @@ mod blocking_open_call;
mod blocking_process_invocation;
mod blocking_sleep;
mod cancel_scope_no_checkpoint;
mod sleep_forever_call;
mod long_sleep_not_forever;
mod sync_call;
mod zero_sleep_call;
110 changes: 0 additions & 110 deletions crates/ruff_linter/src/rules/flake8_async/rules/sleep_forever_call.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ ASYNC116.py:57:11: ASYNC116 [*] `trio.sleep()` with >24 hour interval should usu
56 57 | # catch from import
57 |- await sleep(86401) # error: 116, "async"
58 |+ await sleep_forever() # error: 116, "async"
58 59 |
59 60 |
60 61 | async def import_anyio():
Loading
Loading