Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flake8-pyi] Implement PYI017 #5895

Merged
merged 11 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
20 changes: 20 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI017.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
20 changes: 20 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI017.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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
6 changes: 5 additions & 1 deletion crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ where
tryceratops::rules::error_instead_of_exception(self, handlers);
}
}
Stmt::Assign(ast::StmtAssign { targets, value, .. }) => {
Stmt::Assign(stmt_assign @ ast::StmtAssign { targets, value, .. }) => {
if self.enabled(Rule::LambdaAssignment) {
if let [target] = &targets[..] {
pycodestyle::rules::lambda_assignment(self, target, value, None, stmt);
Expand Down Expand Up @@ -1557,6 +1557,7 @@ where
Rule::UnprefixedTypeParam,
Rule::AssignmentDefaultInStub,
Rule::UnannotatedAssignmentInStub,
Rule::ComplexAssignment,
Rule::TypeAliasWithoutAnnotation,
]) {
// Ignore assignments in function bodies; those are covered by other rules.
Expand All @@ -1576,6 +1577,9 @@ where
self, targets, value,
);
}
if self.enabled(Rule::ComplexAssignment) {
flake8_pyi::rules::complex_assignment(self, stmt_assign);
}
if self.enabled(Rule::TypeAliasWithoutAnnotation) {
flake8_pyi::rules::type_alias_without_annotation(
self, value, targets,
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8Pyi, "014") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::ArgumentDefaultInStub),
(Flake8Pyi, "015") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::AssignmentDefaultInStub),
(Flake8Pyi, "016") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::DuplicateUnionMember),
(Flake8Pyi, "017") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::ComplexAssignment),
(Flake8Pyi, "020") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::QuotedAnnotationInStub),
(Flake8Pyi, "021") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::DocstringInStub),
(Flake8Pyi, "024") => (RuleGroup::Unspecified, rules::flake8_pyi::rules::CollectionsNamedTuple),
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ mod tests {
#[test_case(Rule::BadVersionInfoComparison, Path::new("PYI006.pyi"))]
#[test_case(Rule::CollectionsNamedTuple, Path::new("PYI024.py"))]
#[test_case(Rule::CollectionsNamedTuple, Path::new("PYI024.pyi"))]
#[test_case(Rule::ComplexAssignment, Path::new("PYI017.py"))]
#[test_case(Rule::ComplexAssignment, Path::new("PYI017.pyi"))]
#[test_case(Rule::ComplexIfStatementInStub, Path::new("PYI002.py"))]
#[test_case(Rule::ComplexIfStatementInStub, Path::new("PYI002.pyi"))]
#[test_case(Rule::DocstringInStub, Path::new("PYI021.py"))]
Expand Down
50 changes: 50 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/rules/complex_assignment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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
///
/// ## Why is this bad?
/// Stub files are not intended to ever 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
/// 100% unambiguous when it comes to how the type checker is supposed to interpret it. Only
/// allowing simple assignments is one such restriction.
///
/// ## Example
/// ```python
/// a = b = int
/// a.b = int
/// ```
///
/// Use instead:
/// ```python
/// a: TypeAlias = int
/// b: TypeAlias = int
///
///
/// class a:
/// b: int
/// ```
#[violation]
pub struct ComplexAssignment;

impl Violation for ComplexAssignment {
#[derive_message_formats]
fn message(&self) -> String {
format!("Stubs should not contain assignments to attributes or multiple targets.")
}
}

/// PYI017
pub(crate) fn complex_assignment(checker: &mut Checker, stmt: &StmtAssign) {
if let [Expr::Name(_)] = stmt.targets[..] {
return;
}
checker
.diagnostics
.push(Diagnostic::new(ComplexAssignment, stmt.range));
}
2 changes: 2 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) use any_eq_ne_annotation::*;
pub(crate) use bad_version_info_comparison::*;
pub(crate) use collections_named_tuple::*;
pub(crate) use complex_assignment::*;
pub(crate) use complex_if_statement_in_stub::*;
pub(crate) use docstring_in_stubs::*;
pub(crate) use duplicate_union_member::*;
Expand Down Expand Up @@ -31,6 +32,7 @@ pub(crate) use unrecognized_version_info::*;
mod any_eq_ne_annotation;
mod bad_version_info_comparison;
mod collections_named_tuple;
mod complex_assignment;
mod complex_if_statement_in_stub;
mod docstring_in_stubs;
mod duplicate_union_member;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
source: crates/ruff/src/rules/flake8_pyi/mod.rs
---

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/ruff/src/rules/flake8_pyi/mod.rs
---
PYI017.pyi:5:1: PYI017 Stubs should not contain assignments to attributes or multiple targets.
|
5 | b = c = int # PYI017
| ^^^^^^^^^^^ PYI017
|

PYI017.pyi:8:1: PYI017 Stubs should not contain assignments to attributes or multiple targets.
|
8 | a.b = int # PYI017
| ^^^^^^^^^ PYI017
|

PYI017.pyi:11:1: PYI017 Stubs should not contain assignments to attributes or multiple targets.
|
11 | d, e = int, str # PYI017
| ^^^^^^^^^^^^^^^ PYI017
|

PYI017.pyi:14:1: PYI017 Stubs should not contain assignments to attributes or multiple targets.
|
14 | f, g, h = int, str, TypeVar("T") # PYI017
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI017
|


1 change: 1 addition & 0 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.