Skip to content

Commit

Permalink
[ruff] Reduce FastAPI false positives in unused-async (RUF029) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerBin committed Aug 17, 2024
1 parent 96802d6 commit 52ba941
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF029.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,13 @@ async def test():
async def test() -> str:
vals = [str(val) for val in await async_func(1)]
return ",".join(vals)


from fastapi import FastAPI

app = FastAPI()


@app.post("/count")
async def fastapi_route(): # Ok: FastApi routes can be async without actually using await
return 1
8 changes: 8 additions & 0 deletions crates/ruff_linter/src/rules/ruff/rules/unused_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use ruff_python_ast::identifier::Identifier;
use ruff_python_ast::visitor::source_order;
use ruff_python_ast::{self as ast, AnyNodeRef, Expr, Stmt};
use ruff_python_semantic::analyze::function_type::is_stub;
use ruff_python_semantic::Modules;

use crate::checkers::ast::Checker;
use crate::rules::fastapi::rules::is_fastapi_route;

/// ## What it does
/// Checks for functions declared `async` that do not await or otherwise use features requiring the
Expand Down Expand Up @@ -173,6 +175,12 @@ pub(crate) fn unused_async(
return;
}

if checker.semantic().seen_module(Modules::FASTAPI)
&& is_fastapi_route(function_def, checker.semantic())
{
return;
}

let found_await_or_async = {
let mut visitor = AsyncExprVisitor::default();
source_order::walk_body(&mut visitor, body);
Expand Down

0 comments on commit 52ba941

Please sign in to comment.