-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 PYI054 (#4775)
- Loading branch information
Showing
14 changed files
with
364 additions
and
65 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
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 |
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 @@ | ||
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 |
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
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
58 changes: 58 additions & 0 deletions
58
crates/ruff/src/rules/flake8_pyi/rules/numeric_literal_too_long.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,58 @@ | ||
use ruff_text_size::TextSize; | ||
use rustpython_parser::ast::{Expr, Ranged}; | ||
|
||
use ruff_diagnostics::{AlwaysAutofixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
|
||
use crate::checkers::ast::Checker; | ||
use crate::registry::AsRule; | ||
|
||
#[violation] | ||
pub struct NumericLiteralTooLong; | ||
|
||
/// ## What it does | ||
/// Checks for numeric literals with a string representation longer than ten | ||
/// characters. | ||
/// | ||
/// ## Why is this bad? | ||
/// If a function has a default value where the literal representation is | ||
/// greater than 50 characters, it is likely to be an implementation detail or | ||
/// a constant that varies depending on the system you're running on. | ||
/// | ||
/// Consider replacing such constants with ellipses (`...`). | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// def foo(arg: int = 12345678901) -> None: ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// def foo(arg: int = ...) -> None: ... | ||
/// ``` | ||
impl AlwaysAutofixableViolation for NumericLiteralTooLong { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Numeric literals with a string representation longer than ten characters are not permitted") | ||
} | ||
|
||
fn autofix_title(&self) -> String { | ||
"Replace with `...`".to_string() | ||
} | ||
} | ||
|
||
/// PYI054 | ||
pub(crate) fn numeric_literal_too_long(checker: &mut Checker, expr: &Expr) { | ||
if expr.range().len() <= TextSize::new(10) { | ||
return; | ||
} | ||
|
||
let mut diagnostic = Diagnostic::new(NumericLiteralTooLong, expr.range()); | ||
if checker.patch(diagnostic.kind.rule()) { | ||
diagnostic.set_fix(Fix::suggested(Edit::range_replacement( | ||
"...".to_string(), | ||
expr.range(), | ||
))); | ||
} | ||
checker.diagnostics.push(diagnostic); | ||
} |
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
Oops, something went wrong.