-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2ee4e48
Implement PYI054
density 618d97d
remove s
density d70b15e
Remove check for long numeric literals from PYI015 since it is now ha…
density 67f799e
fix comments
density 7d84af7
update reasoning
density 4b8e59a
Merge branch 'main' into PYI054
charliermarsh 95e32d8
Rename rule
charliermarsh bd0232c
Merge branch 'main' into PYI054
charliermarsh b0790df
Merge
charliermarsh 17a09b3
Add fix
charliermarsh 3360343
Fix always
charliermarsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the checks on numeric literal length since it's now covered by PYI054.