Skip to content

Commit b85c219

Browse files
authored
[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 ```
1 parent b1d1cf1 commit b85c219

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

crates/ruff_linter/src/rules/fastapi/rules/fastapi_non_annotated_dependency.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ use ruff_python_ast::PythonVersion;
5959
/// return commons
6060
/// ```
6161
///
62+
/// ## Fix safety
63+
/// This fix is always unsafe, as adding/removing/changing a function parameter's
64+
/// default value can change runtime behavior. Additionally, comments inside the
65+
/// deprecated uses might be removed.
66+
///
6267
/// ## Availability
6368
///
6469
/// Because this rule relies on the third-party `typing_extensions` module for Python versions
65-
/// before 3.9, its diagnostic will not be emitted, and no fix will be offered, if
66-
/// `typing_extensions` imports have been disabled by the [`lint.typing-extensions`] linter option.
70+
/// before 3.9, if the target version is < 3.9 and `typing_extensions` imports have been
71+
/// disabled by the [`lint.typing-extensions`] linter option the diagnostic will not be emitted
72+
/// and no fix will be offered.
6773
///
6874
/// ## Options
6975
///

0 commit comments

Comments
 (0)