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 PYI054 #4775

Merged
merged 11 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/PYI054.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
field01: int = 0xFFFFFFFF
field02: int = 0xFFFFFFFFF
field03: int = -0xFFFFFFFF
field04: int = -0xFFFFFFFFF

field05: int = 1234567890
field06: int = 12_456_890
field07: int = 12345678901
field08: int = -1234567801
field09: int = -234_567_890

field10: float = 123.456789
field11: float = 123.4567890
field12: float = -123.456789
field13: float = -123.567_890

field14: complex = 1e1234567j
field15: complex = 1e12345678j
field16: complex = -1e1234567j
field17: complex = 1e123456789j
20 changes: 20 additions & 0 deletions crates/ruff/resources/test/fixtures/flake8_pyi/PYI054.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
field01: int = 0xFFFFFFFF
field02: int = 0xFFFFFFFFF # Error: PYI054
field03: int = -0xFFFFFFFF
field04: int = -0xFFFFFFFFF # Error: PYI054

field05: int = 1234567890
field06: int = 12_456_890
field07: int = 12345678901 # Error: PYI054
field08: int = -1234567801
field09: int = -234_567_890 # Error: PYI054

field10: float = 123.456789
field11: float = 123.4567890 # Error: PYI054
field12: float = -123.456789
field13: float = -123.567_890 # Error: PYI054

field14: complex = 1e1234567j
field15: complex = 1e12345678j # Error: PYI054
field16: complex = -1e1234567j
field17: complex = 1e123456789j # Error: PYI054
9 changes: 9 additions & 0 deletions crates/ruff/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3393,6 +3393,15 @@ where
}
}
}
Expr::Constant(ast::ExprConstant {
value: Constant::Int(_) | Constant::Float(_) | Constant::Complex { .. },
range,
..
}) => {
if self.is_stub && self.enabled(Rule::LongNumericLiteralsInStub) {
flake8_pyi::rules::long_numeric_literals_in_stubs(self, *range);
}
}
Expr::Constant(ast::ExprConstant {
value: Constant::Str(value),
kind,
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 @@ -599,6 +599,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8Pyi, "045") => (RuleGroup::Unspecified, Rule::IterMethodReturnIterable),
(Flake8Pyi, "048") => (RuleGroup::Unspecified, Rule::StubBodyMultipleStatements),
(Flake8Pyi, "052") => (RuleGroup::Unspecified, Rule::UnannotatedAssignmentInStub),
(Flake8Pyi, "054") => (RuleGroup::Unspecified, Rule::LongNumericLiteralsInStub),

// flake8-pytest-style
(Flake8PytestStyle, "001") => (RuleGroup::Unspecified, Rule::PytestFixtureIncorrectParenthesesStyle),
Expand Down
1 change: 1 addition & 0 deletions crates/ruff/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ ruff_macros::register_rules!(
rules::flake8_pyi::rules::NonEmptyStubBody,
rules::flake8_pyi::rules::PassInClassBody,
rules::flake8_pyi::rules::PassStatementStubBody,
rules::flake8_pyi::rules::LongNumericLiteralsInStub,
rules::flake8_pyi::rules::QuotedAnnotationInStub,
rules::flake8_pyi::rules::SnakeCaseTypeAlias,
rules::flake8_pyi::rules::StubBodyMultipleStatements,
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 @@ -30,6 +30,8 @@ mod tests {
#[test_case(Rule::CollectionsNamedTuple, Path::new("PYI024.pyi"))]
#[test_case(Rule::IterMethodReturnIterable, Path::new("PYI045.py"))]
#[test_case(Rule::IterMethodReturnIterable, Path::new("PYI045.pyi"))]
#[test_case(Rule::LongNumericLiteralsInStub, Path::new("PYI054.py"))]
#[test_case(Rule::LongNumericLiteralsInStub, Path::new("PYI054.pyi"))]
#[test_case(Rule::NonEmptyStubBody, Path::new("PYI010.py"))]
#[test_case(Rule::NonEmptyStubBody, Path::new("PYI010.pyi"))]
#[test_case(Rule::PassInClassBody, Path::new("PYI012.py"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_text_size::{TextRange, TextSize};

use ruff_macros::{derive_message_formats, violation};

use crate::checkers::ast::Checker;

#[violation]
pub struct LongNumericLiteralsInStub;

/// ## What it does
/// Checks for numeric literals longer than 10 characters
///
/// ## Why is this bad?
/// Long hardcoded numeric values are unlikely to be useful for users. Consider replacing them with ellipses.
density marked this conversation as resolved.
Show resolved Hide resolved
impl Violation for LongNumericLiteralsInStub {
#[derive_message_formats]
fn message(&self) -> String {
format!("Numeric literals with a string representation longer than ten characters are not permitted")
}
}

/// PYI054
pub(crate) fn long_numeric_literals_in_stubs(checker: &mut Checker, range: TextRange) {
if range.len() > TextSize::new(10) {
checker
.diagnostics
.push(Diagnostic::new(LongNumericLiteralsInStub, range));
}
}
4 changes: 4 additions & 0 deletions crates/ruff/src/rules/flake8_pyi/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub(crate) use ellipsis_in_non_empty_class_body::{
pub(crate) use iter_method_return_iterable::{
iter_method_return_iterable, IterMethodReturnIterable,
};
pub(crate) use long_numeric_literals_in_stubs::{
long_numeric_literals_in_stubs, LongNumericLiteralsInStub,
};
pub(crate) use non_empty_stub_body::{non_empty_stub_body, NonEmptyStubBody};
pub(crate) use pass_in_class_body::{pass_in_class_body, PassInClassBody};
pub(crate) use pass_statement_stub_body::{pass_statement_stub_body, PassStatementStubBody};
Expand Down Expand Up @@ -39,6 +42,7 @@ mod docstring_in_stubs;
mod duplicate_union_member;
mod ellipsis_in_non_empty_class_body;
mod iter_method_return_iterable;
mod long_numeric_literals_in_stubs;
mod non_empty_stub_body;
mod pass_in_class_body;
mod pass_statement_stub_body;
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,79 @@
---
source: crates/ruff/src/rules/flake8_pyi/mod.rs
---
PYI054.pyi:2:16: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
2 | field01: int = 0xFFFFFFFF
3 | field02: int = 0xFFFFFFFFF # Error: PYI054
| ^^^^^^^^^^^ PYI054
4 | field03: int = -0xFFFFFFFF
5 | field04: int = -0xFFFFFFFFF # Error: PYI054
|

PYI054.pyi:4:17: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
4 | field02: int = 0xFFFFFFFFF # Error: PYI054
5 | field03: int = -0xFFFFFFFF
6 | field04: int = -0xFFFFFFFFF # Error: PYI054
| ^^^^^^^^^^^ PYI054
7 |
8 | field05: int = 1234567890
|

PYI054.pyi:8:16: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
8 | field05: int = 1234567890
9 | field06: int = 12_456_890
10 | field07: int = 12345678901 # Error: PYI054
| ^^^^^^^^^^^ PYI054
11 | field08: int = -1234567801
12 | field09: int = -234_567_890 # Error: PYI054
|

PYI054.pyi:10:17: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
10 | field07: int = 12345678901 # Error: PYI054
11 | field08: int = -1234567801
12 | field09: int = -234_567_890 # Error: PYI054
| ^^^^^^^^^^^ PYI054
13 |
14 | field10: float = 123.456789
|

PYI054.pyi:13:18: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
13 | field10: float = 123.456789
14 | field11: float = 123.4567890 # Error: PYI054
| ^^^^^^^^^^^ PYI054
15 | field12: float = -123.456789
16 | field13: float = -123.567_890 # Error: PYI054
|

PYI054.pyi:15:19: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
15 | field11: float = 123.4567890 # Error: PYI054
16 | field12: float = -123.456789
17 | field13: float = -123.567_890 # Error: PYI054
| ^^^^^^^^^^^ PYI054
18 |
19 | field14: complex = 1e1234567j
|

PYI054.pyi:18:20: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
18 | field14: complex = 1e1234567j
19 | field15: complex = 1e12345678j # Error: PYI054
| ^^^^^^^^^^^ PYI054
20 | field16: complex = -1e1234567j
21 | field17: complex = 1e123456789j # Error: PYI054
|

PYI054.pyi:20:20: PYI054 Numeric literals with a string representation longer than ten characters are not permitted
|
20 | field15: complex = 1e12345678j # Error: PYI054
21 | field16: complex = -1e1234567j
22 | field17: complex = 1e123456789j # Error: PYI054
| ^^^^^^^^^^^^ PYI054
|


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.