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

Expand asyncio-dangling-task (RUF006) to include new_event_loop #9976

Merged
merged 2 commits into from
Feb 13, 2024

Conversation

tyilo
Copy link
Contributor

@tyilo tyilo commented Feb 13, 2024

Summary

Fixes #9974

Test Plan

I added some new test cases.

Copy link
Contributor

github-actions bot commented Feb 13, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Addressed the TODO.

@charliermarsh charliermarsh added the bug Something isn't working label Feb 13, 2024
@charliermarsh charliermarsh changed the title Improve RUF006 detection Expand asyncio-dangling-task (RUF006) to include new_event_loop Feb 13, 2024
@charliermarsh charliermarsh added rule Implementing or modifying a lint rule and removed bug Something isn't working labels Feb 13, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) February 13, 2024 18:16
@charliermarsh charliermarsh merged commit 3e9d761 into astral-sh:main Feb 13, 2024
17 checks passed
@tyilo
Copy link
Contributor Author

tyilo commented Feb 13, 2024

@charliermarsh What do you think of my comments?

@charliermarsh
Copy link
Member

@tyilo - Which comments specifically? Sorry if I missed something.

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| {
Copy link
Contributor Author

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 is asyncio.AbstractEventLoop, so that the following would also be flagged:

import asyncio


def create_event_loop() -> asyncio.AbstractEventLoop:
  return asyncio.new_event_loop()


loop = create_event_loop()
loop.create_task(asyncio.sleep(1))

Copy link
Member

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.

}) {
return Some(Diagnostic::new(
AsyncioDanglingTask {
expr: compose_call_path(value).unwrap_or_else(|| "asyncio".to_string()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think loop would be a better fallback:

Suggested change
expr: compose_call_path(value).unwrap_or_else(|| "asyncio".to_string()),
expr: compose_call_path(value).unwrap_or_else(|| "loop".to_string()),

Copy link
Member

Choose a reason for hiding this comment

The 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. value here has to be a Name, and compose_call_path always returns Some given a name.

@tyilo
Copy link
Contributor Author

tyilo commented Feb 13, 2024

@tyilo - Which comments specifically? Sorry if I missed something.

Ah, sorry I haven't used GitHub's review functionality before, so my comments weren't published 😅

They should be visible now...

nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
…astral-sh#9976)

## Summary

Fixes astral-sh#9974

## Test Plan

I added some new test cases.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUF006 not emitted when using asyncio.new_event_loop
2 participants