Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
249 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...es/ruff/src/rules/flake8_future_annotations/rules/missing_future_annotations_new_style.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use rustpython_parser::ast::{Expr, Ranged}; | ||
use std::fmt; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of PEP 585- and PEP 604-style type annotations in Python | ||
/// modules that lack the required `from __future__ import annotations` import | ||
/// for compatibility with older Python versions. | ||
/// | ||
/// ## Why is this bad? | ||
/// Using PEP 585 and PEP 604 style annotations without a `from __future__ import | ||
/// annotations` import will cause runtime errors on Python versions prior to | ||
/// 3.9 and 3.10, respectively. | ||
/// | ||
/// By adding the `__future__` import, the interpreter will no longer interpret | ||
/// annotations at evaluation time, making the code compatible with both past | ||
/// and future Python versions. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def func(obj: dict[str, int | None]) -> None: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from __future__ import annotations | ||
/// | ||
/// | ||
/// def func(obj: dict[str, int | None]) -> None: | ||
/// ... | ||
/// ``` | ||
#[violation] | ||
pub struct MissingFutureAnnotationsImportNewStyle { | ||
reason: Reason, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub(crate) enum Reason { | ||
/// The type annotation is written in PEP 585 style (e.g., `list[int]`). | ||
PEP585, | ||
/// The type annotation is written in PEP 604 style (e.g., `int | None`). | ||
PEP604, | ||
} | ||
|
||
impl fmt::Display for Reason { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Reason::PEP585 => fmt.write_str("PEP 585 collection"), | ||
Reason::PEP604 => fmt.write_str("PEP 604 union"), | ||
} | ||
} | ||
} | ||
|
||
impl Violation for MissingFutureAnnotationsImportNewStyle { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let MissingFutureAnnotationsImportNewStyle { reason } = self; | ||
format!("Missing `from __future__ import annotations`, but uses {reason}") | ||
} | ||
} | ||
|
||
/// FA102 | ||
pub(crate) fn missing_future_annotations_new_style( | ||
checker: &mut Checker, | ||
expr: &Expr, | ||
reason: Reason, | ||
) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
MissingFutureAnnotationsImportNewStyle { reason }, | ||
expr.range(), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 7 additions & 3 deletions
10
crates/ruff/src/rules/flake8_future_annotations/rules/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
pub(crate) use missing_future_annotations::{ | ||
missing_future_annotations, MissingFutureAnnotationsImport, | ||
pub(crate) use missing_future_annotations_new_style::{ | ||
missing_future_annotations_new_style, MissingFutureAnnotationsImportNewStyle, Reason, | ||
}; | ||
pub(crate) use missing_future_annotations_old_style::{ | ||
missing_future_annotations_old_style, MissingFutureAnnotationsImportOldStyle, | ||
}; | ||
|
||
mod missing_future_annotations; | ||
mod missing_future_annotations_new_style; | ||
mod missing_future_annotations_old_style; |
12 changes: 12 additions & 0 deletions
12
...f__rules__flake8_future_annotations__tests__fa102_no_future_import_uses_lowercase.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_future_annotations/mod.rs | ||
--- | ||
no_future_import_uses_lowercase.py:2:13: FA102 Missing `from __future__ import annotations`, but uses PEP 585 collection | ||
| | ||
2 | def main() -> None: | ||
3 | a_list: list[str] = [] | ||
| ^^^^^^^^^ FA102 | ||
4 | a_list.append("hello") | ||
| | ||
|
||
|
20 changes: 20 additions & 0 deletions
20
.../ruff__rules__flake8_future_annotations__tests__fa102_no_future_import_uses_union.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_future_annotations/mod.rs | ||
--- | ||
no_future_import_uses_union.py:2:13: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union | ||
| | ||
2 | def main() -> None: | ||
3 | a_list: list[str] | None = [] | ||
| ^^^^^^^^^^^^^^^^ FA102 | ||
4 | a_list.append("hello") | ||
| | ||
|
||
no_future_import_uses_union.py:2:13: FA102 Missing `from __future__ import annotations`, but uses PEP 585 collection | ||
| | ||
2 | def main() -> None: | ||
3 | a_list: list[str] | None = [] | ||
| ^^^^^^^^^ FA102 | ||
4 | a_list.append("hello") | ||
| | ||
|
||
|
36 changes: 36 additions & 0 deletions
36
..._rules__flake8_future_annotations__tests__fa102_no_future_import_uses_union_inner.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_future_annotations/mod.rs | ||
--- | ||
no_future_import_uses_union_inner.py:2:13: FA102 Missing `from __future__ import annotations`, but uses PEP 585 collection | ||
| | ||
2 | def main() -> None: | ||
3 | a_list: list[str | None] = [] | ||
| ^^^^^^^^^^^^^^^^ FA102 | ||
4 | a_list.append("hello") | ||
| | ||
|
||
no_future_import_uses_union_inner.py:2:18: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union | ||
| | ||
2 | def main() -> None: | ||
3 | a_list: list[str | None] = [] | ||
| ^^^^^^^^^^ FA102 | ||
4 | a_list.append("hello") | ||
| | ||
|
||
no_future_import_uses_union_inner.py:7:8: FA102 Missing `from __future__ import annotations`, but uses PEP 585 collection | ||
| | ||
7 | def hello(y: dict[str | None, int]) -> None: | ||
8 | z: tuple[str, str | None, str] = tuple(y) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ FA102 | ||
9 | del z | ||
| | ||
|
||
no_future_import_uses_union_inner.py:7:19: FA102 Missing `from __future__ import annotations`, but uses PEP 604 union | ||
| | ||
7 | def hello(y: dict[str | None, int]) -> None: | ||
8 | z: tuple[str, str | None, str] = tuple(y) | ||
| ^^^^^^^^^^ FA102 | ||
9 | del z | ||
| | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
...ations/snapshots/ruff__rules__flake8_future_annotations__tests__fa102_ok_no_types.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_future_annotations/mod.rs | ||
--- | ||
|
Oops, something went wrong.