-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FA102 (using new-style annotation without annotations import)
- Loading branch information
Showing
11 changed files
with
169 additions
and
0 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
44 changes: 44 additions & 0 deletions
44
...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,44 @@ | ||
use rustpython_parser::ast::{Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for missing `from __future__ import annotations` imports upon | ||
/// detecting type annotations that are written in PEP 585 or PEP 604 style but | ||
/// the target Python version doesn't support them without the import. | ||
/// | ||
/// ## Example | ||
/// | ||
/// ```python | ||
/// def function(a_dict: dict[str, int | None]) -> None: | ||
/// a_list: list[str] = [] | ||
/// a_list.append("hello") | ||
/// ``` | ||
/// would raise an exception at runtime before Python 3.9 and Python 3.10. | ||
#[violation] | ||
pub struct MissingFutureAnnotationsImportNewStyle { | ||
kind: String, | ||
expr: String, | ||
} | ||
|
||
impl Violation for MissingFutureAnnotationsImportNewStyle { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let MissingFutureAnnotationsImportNewStyle { kind, expr } = self; | ||
format!("Missing `from __future__ import annotations`, but uses {kind} `{expr}`") | ||
} | ||
} | ||
|
||
/// FA102 | ||
pub(crate) fn missing_future_annotations_new_style(checker: &mut Checker, kind: &str, expr: &Expr) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
MissingFutureAnnotationsImportNewStyle { | ||
kind: kind.to_string(), | ||
expr: checker.locator.slice(expr.range()).to_string(), | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
pub(crate) use missing_future_annotations_new_style::{ | ||
missing_future_annotations_new_style, MissingFutureAnnotationsImportNewStyle, | ||
}; | ||
pub(crate) use missing_future_annotations_old_style::{ | ||
missing_future_annotations_old_style, MissingFutureAnnotationsImportOldStyle, | ||
}; | ||
|
||
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 PEP585 collection `list[str]` | ||
| | ||
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 PEP604 union `list[str] | None` | ||
| | ||
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 PEP585 collection `list[str]` | ||
| | ||
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 PEP585 collection `list[str | None]` | ||
| | ||
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 PEP604 union `str | None` | ||
| | ||
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 PEP585 collection `tuple[str, str | None, str]` | ||
| | ||
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 PEP604 union `str | None` | ||
| | ||
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 | ||
--- | ||
|
4 changes: 4 additions & 0 deletions
4
...ons/snapshots/ruff__rules__flake8_future_annotations__tests__fa102_ok_uses_future.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 | ||
--- | ||
|