-
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.
[
flake8-pyi
] Implement PYI017 (#5895)
## Summary Implements `PYI017` or `Y017` from `flake8-pyi` plug-in. Mirrors [upstream implementation](https://github.com/PyCQA/flake8-pyi/blob/ceab86d16b822d12abae1d8087850d0bc66d2f52/pyi.py#L1039-L1048). It checks for any assignment with more than 1 target or an assignment to anything other than a name, and raises a violation for these in stub files. Couldn't find a clear and concise explanation for why this is to be avoided and what is preferred for attribute cases like: ```python a.b = int ``` So welcome some input there, to learn and to finish up the docs. ## Test Plan Added test cases from upstream plug-in in a fixture (both `.py` and `.pyi`). Added a few more. ## Issue link Refers: #848
- Loading branch information
Showing
10 changed files
with
141 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var: int | ||
a = var # OK | ||
|
||
b = c = int # OK | ||
|
||
a.b = int # OK | ||
|
||
d, e = int, str # OK | ||
|
||
f, g, h = int, str, TypeVar("T") # OK | ||
|
||
i: TypeAlias = int | str # OK | ||
|
||
j: TypeAlias = int # OK |
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,14 @@ | ||
var: int | ||
a = var # OK | ||
|
||
b = c = int # PYI017 | ||
|
||
a.b = int # PYI017 | ||
|
||
d, e = int, str # PYI017 | ||
|
||
f, g, h = int, str, TypeVar("T") # PYI017 | ||
|
||
i: TypeAlias = int | str # OK | ||
|
||
j: TypeAlias = int # OK |
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
54 changes: 54 additions & 0 deletions
54
crates/ruff/src/rules/flake8_pyi/rules/complex_assignment_in_stub.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,54 @@ | ||
use rustpython_parser::ast::{Expr, StmtAssign}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for assignments with multiple or non-name targets in stub files. | ||
/// | ||
/// ## Why is this bad? | ||
/// In general, stub files should be thought of as "data files" for a type | ||
/// checker, and are not intended to be executed. As such, it's useful to | ||
/// enforce that only a subset of Python syntax is allowed in a stub file, to | ||
/// ensure that everything in the stub is unambiguous for the type checker. | ||
/// | ||
/// The need to perform multi-assignment, or assignment to a non-name target, | ||
/// likely indicates a misunderstanding of how stub files are intended to be | ||
/// used. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// a = b = int | ||
/// a.b = int | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// a: TypeAlias = int | ||
/// b: TypeAlias = int | ||
/// | ||
/// | ||
/// class a: | ||
/// b: int | ||
/// ``` | ||
#[violation] | ||
pub struct ComplexAssignmentInStub; | ||
|
||
impl Violation for ComplexAssignmentInStub { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Stubs should not contain assignments to attributes or multiple targets") | ||
} | ||
} | ||
|
||
/// PYI017 | ||
pub(crate) fn complex_assignment_in_stub(checker: &mut Checker, stmt: &StmtAssign) { | ||
if matches!(stmt.targets.as_slice(), [Expr::Name(_)]) { | ||
return; | ||
} | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(ComplexAssignmentInStub, stmt.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
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI017_PYI017.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_pyi/mod.rs | ||
--- | ||
|
44 changes: 44 additions & 0 deletions
44
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI017_PYI017.pyi.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,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/flake8_pyi/mod.rs | ||
--- | ||
PYI017.pyi:4:1: PYI017 Stubs should not contain assignments to attributes or multiple targets | ||
| | ||
2 | a = var # OK | ||
3 | | ||
4 | b = c = int # PYI017 | ||
| ^^^^^^^^^^^ PYI017 | ||
5 | | ||
6 | a.b = int # PYI017 | ||
| | ||
|
||
PYI017.pyi:6:1: PYI017 Stubs should not contain assignments to attributes or multiple targets | ||
| | ||
4 | b = c = int # PYI017 | ||
5 | | ||
6 | a.b = int # PYI017 | ||
| ^^^^^^^^^ PYI017 | ||
7 | | ||
8 | d, e = int, str # PYI017 | ||
| | ||
|
||
PYI017.pyi:8:1: PYI017 Stubs should not contain assignments to attributes or multiple targets | ||
| | ||
6 | a.b = int # PYI017 | ||
7 | | ||
8 | d, e = int, str # PYI017 | ||
| ^^^^^^^^^^^^^^^ PYI017 | ||
9 | | ||
10 | f, g, h = int, str, TypeVar("T") # PYI017 | ||
| | ||
|
||
PYI017.pyi:10:1: PYI017 Stubs should not contain assignments to attributes or multiple targets | ||
| | ||
8 | d, e = int, str # PYI017 | ||
9 | | ||
10 | f, g, h = int, str, TypeVar("T") # PYI017 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI017 | ||
11 | | ||
12 | i: TypeAlias = int | str # OK | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.