-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Expand asyncio-dangling-task
(RUF006
) to include new_event_loop
#9976
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,7 @@ use std::fmt; | |||||
use ast::Stmt; | ||||||
use ruff_diagnostics::{Diagnostic, Violation}; | ||||||
use ruff_macros::{derive_message_formats, violation}; | ||||||
use ruff_python_ast::call_path::compose_call_path; | ||||||
use ruff_python_ast::{self as ast, Expr}; | ||||||
use ruff_python_semantic::{analyze::typing, Scope, SemanticModel}; | ||||||
use ruff_text_size::Ranged; | ||||||
|
@@ -52,14 +53,15 @@ use ruff_text_size::Ranged; | |||||
/// - [The Python Standard Library](https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task) | ||||||
#[violation] | ||||||
pub struct AsyncioDanglingTask { | ||||||
expr: String, | ||||||
method: Method, | ||||||
} | ||||||
|
||||||
impl Violation for AsyncioDanglingTask { | ||||||
#[derive_message_formats] | ||||||
fn message(&self) -> String { | ||||||
let AsyncioDanglingTask { method } = self; | ||||||
format!("Store a reference to the return value of `asyncio.{method}`") | ||||||
let AsyncioDanglingTask { expr, method } = self; | ||||||
format!("Store a reference to the return value of `{expr}.{method}`") | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -80,19 +82,29 @@ pub(crate) fn asyncio_dangling_task(expr: &Expr, semantic: &SemanticModel) -> Op | |||||
}) | ||||||
{ | ||||||
return Some(Diagnostic::new( | ||||||
AsyncioDanglingTask { method }, | ||||||
AsyncioDanglingTask { | ||||||
expr: "asyncio".to_string(), | ||||||
method, | ||||||
}, | ||||||
expr.range(), | ||||||
)); | ||||||
} | ||||||
|
||||||
// Ex) `loop = asyncio.get_running_loop(); loop.create_task(...)` | ||||||
// Ex) `loop = ...; loop.create_task(...)` | ||||||
if let Expr::Attribute(ast::ExprAttribute { attr, value, .. }) = func.as_ref() { | ||||||
if attr == "create_task" { | ||||||
if typing::resolve_assignment(value, semantic).is_some_and(|call_path| { | ||||||
matches!(call_path.as_slice(), ["asyncio", "get_running_loop"]) | ||||||
matches!( | ||||||
call_path.as_slice(), | ||||||
[ | ||||||
"asyncio", | ||||||
"get_event_loop" | "get_running_loop" | "new_event_loop" | ||||||
] | ||||||
) | ||||||
}) { | ||||||
return Some(Diagnostic::new( | ||||||
AsyncioDanglingTask { | ||||||
expr: compose_call_path(value).unwrap_or_else(|| "asyncio".to_string()), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change, though I think in practice this should never happen. I wish it were encoded in the type system, but e.g. |
||||||
method: Method::CreateTask, | ||||||
}, | ||||||
expr.range(), | ||||||
|
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 don't know if ruff's typing analysis is good enough for this, but it would be better to check that the type of
value
isasyncio.AbstractEventLoop
, so that the following would also be flagged: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.
Yeah we don't quite support that yet unfortunately.