From b85c219283dcdae474642e9174352da5d9aee132 Mon Sep 17 00:00:00 2001 From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:38:02 -0700 Subject: [PATCH] [`FastAPI`] Add fix safety section to `FAST002` (#18940) ## Summary Part of #15584 This PR adds a fix safety section to [fast-api-non-annotated-dependency (FAST002)](https://docs.astral.sh/ruff/rules/fast-api-non-annotated-dependency/#fast-api-non-annotated-dependency-fast002). It also re-words the availability section since I found it confusing. The lint/fix was added in #11579 as always unsafe. No reasoning is given in the original PR/code as to why this was chosen. Example of why the fix is unsafe: https://play.ruff.rs/3bd0566e-1ef6-4cec-ae34-3b07cd308155 ```py from fastapi import Depends, FastAPI, Query app = FastAPI() # Fix will remove the parameter default value @app.get("/items/") async def read_items(commons: dict = Depends(common_parameters)): return commons # Fix will delete comment and change default parameter value @app.get("/items/") async def read_items_1(q: str = Query( # This comment will be deleted default="rick")): return q ``` After fixing both instances of `FAST002`: ```py from fastapi import Depends, FastAPI, Query from typing import Annotated app = FastAPI() # Fix will remove the parameter default value @app.get("/items/") async def read_items(commons: Annotated[dict, Depends(common_parameters)]): return commons # Fix will delete comment and change default parameter value @app.get("/items/") async def read_items_1(q: Annotated[str, Query()] = "rick"): return q ``` --- .../fastapi/rules/fastapi_non_annotated_dependency.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/src/rules/fastapi/rules/fastapi_non_annotated_dependency.rs b/crates/ruff_linter/src/rules/fastapi/rules/fastapi_non_annotated_dependency.rs index 764abe7c9f..8d0fa8b280 100644 --- a/crates/ruff_linter/src/rules/fastapi/rules/fastapi_non_annotated_dependency.rs +++ b/crates/ruff_linter/src/rules/fastapi/rules/fastapi_non_annotated_dependency.rs @@ -59,11 +59,17 @@ use ruff_python_ast::PythonVersion; /// return commons /// ``` /// +/// ## Fix safety +/// This fix is always unsafe, as adding/removing/changing a function parameter's +/// default value can change runtime behavior. Additionally, comments inside the +/// deprecated uses might be removed. +/// /// ## Availability /// /// Because this rule relies on the third-party `typing_extensions` module for Python versions -/// before 3.9, its diagnostic will not be emitted, and no fix will be offered, if -/// `typing_extensions` imports have been disabled by the [`lint.typing-extensions`] linter option. +/// before 3.9, if the target version is < 3.9 and `typing_extensions` imports have been +/// disabled by the [`lint.typing-extensions`] linter option the diagnostic will not be emitted +/// and no fix will be offered. /// /// ## Options ///