Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/ruff_linter/src/rules/fastapi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,20 @@ mod tests {
assert_messages!(snapshot, diagnostics);
Ok(())
}

// FAST002 autofixes use `typing_extensions` on Python 3.8,
// since `typing.Annotated` was added in Python 3.9
#[test_case(Rule::FastApiNonAnnotatedDependency, Path::new("FAST002.py"))]
fn rules_py38(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}_py38", rule_code.as_ref(), path.to_string_lossy());
let diagnostics = test_path(
Path::new("fastapi").join(path).as_path(),
&settings::LinterSettings {
target_version: settings::types::PythonVersion::Py38,
..settings::LinterSettings::for_rule(rule_code)
},
)?;
assert_messages!(snapshot, diagnostics);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ use crate::settings::types::PythonVersion;
/// everywhere helps ensure consistency and clarity in defining dependencies
/// and parameters.
///
/// `Annotated` was added to the `typing` module in Python 3.9; however,
/// the third-party [`typing_extensions`] package provides a backport that can be
/// used on older versions of Python.
///
/// ## Example
///
/// ```python
Expand Down Expand Up @@ -58,8 +62,11 @@ use crate::settings::types::PythonVersion;
///
/// [fastAPI documentation]: https://fastapi.tiangolo.com/tutorial/query-params-str-validations/?h=annotated#advantages-of-annotated
/// [typing.Annotated]: https://docs.python.org/3/library/typing.html#typing.Annotated
/// [typing_extensions]: https://typing-extensions.readthedocs.io/en/stable/
#[violation]
pub struct FastApiNonAnnotatedDependency;
pub struct FastApiNonAnnotatedDependency {
py_version: PythonVersion,
}

impl Violation for FastApiNonAnnotatedDependency {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes;
Expand All @@ -70,7 +77,12 @@ impl Violation for FastApiNonAnnotatedDependency {
}

fn fix_title(&self) -> Option<String> {
Some("Replace with `Annotated`".to_string())
let title = if self.py_version >= PythonVersion::Py39 {
"Replace with `typing.Annotated`"
} else {
"Replace with `typing_extensions.Annotated`"
};
Some(title.to_string())
}
}

Expand Down Expand Up @@ -137,7 +149,12 @@ fn create_diagnostic(
parameter: &ast::ParameterWithDefault,
safe_to_update: bool,
) {
let mut diagnostic = Diagnostic::new(FastApiNonAnnotatedDependency, parameter.range);
let mut diagnostic = Diagnostic::new(
FastApiNonAnnotatedDependency {
py_version: checker.settings.target_version,
},
parameter.range,
);

if safe_to_update {
if let (Some(annotation), Some(default)) =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/fastapi/mod.rs
snapshot_kind: text
---
FAST002.py:24:5: FAST002 [*] FastAPI dependency without `Annotated`
|
Expand All @@ -11,7 +10,7 @@ FAST002.py:24:5: FAST002 [*] FastAPI dependency without `Annotated`
25 | some_security_param: str = Security(get_oauth2_user),
26 | ):
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -40,7 +39,7 @@ FAST002.py:25:5: FAST002 [*] FastAPI dependency without `Annotated`
26 | ):
27 | pass
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -69,7 +68,7 @@ FAST002.py:32:5: FAST002 [*] FastAPI dependency without `Annotated`
33 | some_path_param: str = Path(),
34 | some_body_param: str = Body("foo"),
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -98,7 +97,7 @@ FAST002.py:33:5: FAST002 [*] FastAPI dependency without `Annotated`
34 | some_body_param: str = Body("foo"),
35 | some_cookie_param: str = Cookie(),
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -127,7 +126,7 @@ FAST002.py:34:5: FAST002 [*] FastAPI dependency without `Annotated`
35 | some_cookie_param: str = Cookie(),
36 | some_header_param: int = Header(default=5),
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -156,7 +155,7 @@ FAST002.py:35:5: FAST002 [*] FastAPI dependency without `Annotated`
36 | some_header_param: int = Header(default=5),
37 | some_file_param: UploadFile = File(),
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -185,7 +184,7 @@ FAST002.py:36:5: FAST002 [*] FastAPI dependency without `Annotated`
37 | some_file_param: UploadFile = File(),
38 | some_form_param: str = Form(),
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -214,7 +213,7 @@ FAST002.py:37:5: FAST002 [*] FastAPI dependency without `Annotated`
38 | some_form_param: str = Form(),
39 | ):
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -243,7 +242,7 @@ FAST002.py:38:5: FAST002 [*] FastAPI dependency without `Annotated`
39 | ):
40 | # do stuff
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -272,7 +271,7 @@ FAST002.py:47:5: FAST002 [*] FastAPI dependency without `Annotated`
48 | ):
49 | pass
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -301,7 +300,7 @@ FAST002.py:53:5: FAST002 [*] FastAPI dependency without `Annotated`
54 | skip: int = 0,
55 | limit: int = 10,
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`

ℹ Unsafe fix
12 12 | Security,
Expand Down Expand Up @@ -330,4 +329,4 @@ FAST002.py:67:5: FAST002 FastAPI dependency without `Annotated`
68 | ):
69 | pass
|
= help: Replace with `Annotated`
= help: Replace with `typing.Annotated`
Loading
Loading