-
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 4 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
30 changes: 30 additions & 0 deletions
30
crates/ruff/src/rules/flake8_pyi/rules/long_numeric_literals_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,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. | ||
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_stub(checker: &mut Checker, range: TextRange) { | ||
if range.len() > TextSize::new(10) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(LongNumericLiteralsInStub, 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
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 |
---|---|---|
|
@@ -132,55 +132,37 @@ fn is_valid_default_value_with_annotation( | |
value: Constant::Bytes(..), | ||
.. | ||
}) => return locator.slice(default.range()).len() <= 50, | ||
// Ex) `123`, `True`, `False`, `3.14` | ||
// Ex) `123`, `True`, `False`, `3.14`, `2j` | ||
Expr::Constant(ast::ExprConstant { | ||
value: Constant::Int(..) | Constant::Bool(..) | Constant::Float(..), | ||
value: | ||
Constant::Int(..) | Constant::Bool(..) | Constant::Float(..) | Constant::Complex { .. }, | ||
.. | ||
}) => { | ||
return locator.slice(default.range()).len() <= 10; | ||
} | ||
// Ex) `2j` | ||
Expr::Constant(ast::ExprConstant { | ||
value: Constant::Complex { real, .. }, | ||
.. | ||
}) => { | ||
if *real == 0.0 { | ||
return locator.slice(default.range()).len() <= 10; | ||
} | ||
return true; | ||
} | ||
Expr::UnaryOp(ast::ExprUnaryOp { | ||
op: Unaryop::USub, | ||
operand, | ||
range: _, | ||
}) => { | ||
// Ex) `-1`, `-3.14` | ||
if let Expr::Constant(ast::ExprConstant { | ||
value: Constant::Int(..) | Constant::Float(..), | ||
.. | ||
}) = operand.as_ref() | ||
{ | ||
return locator.slice(operand.range()).len() <= 10; | ||
} | ||
// Ex) `-2j` | ||
if let Expr::Constant(ast::ExprConstant { | ||
value: Constant::Complex { real, .. }, | ||
.. | ||
}) = operand.as_ref() | ||
{ | ||
if *real == 0.0 { | ||
return locator.slice(operand.range()).len() <= 10; | ||
} | ||
} | ||
// Ex) `-math.inf`, `-math.pi`, etc. | ||
if let Expr::Attribute(_) = operand.as_ref() { | ||
if model.resolve_call_path(operand).map_or(false, |call_path| { | ||
ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS.iter().any(|target| { | ||
// reject `-math.nan` | ||
call_path.as_slice() == *target && *target != ["math", "nan"] | ||
}) | ||
}) { | ||
return true; | ||
match operand.as_ref() { | ||
// Ex) `-1`, `-3.14`, `2j` | ||
Expr::Constant(ast::ExprConstant { | ||
value: Constant::Int(..) | Constant::Float(..) | Constant::Complex { .. }, | ||
.. | ||
}) => return true, | ||
// Ex) `-math.inf`, `-math.pi`, etc. | ||
Expr::Attribute(_) => { | ||
if model.resolve_call_path(operand).map_or(false, |call_path| { | ||
ALLOWED_MATH_ATTRIBUTES_IN_DEFAULTS.iter().any(|target| { | ||
// reject `-math.nan` | ||
call_path.as_slice() == *target && *target != ["math", "nan"] | ||
}) | ||
}) { | ||
return true; | ||
} | ||
Comment on lines
+129
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
_ => {} | ||
} | ||
} | ||
Expr::BinOp(ast::ExprBinOp { | ||
|
4 changes: 4 additions & 0 deletions
4
...ruff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI054_PYI054.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 | ||
--- | ||
|
79 changes: 79 additions & 0 deletions
79
...uff/src/rules/flake8_pyi/snapshots/ruff__rules__flake8_pyi__tests__PYI054_PYI054.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,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 | ||
| | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Can we add an example on how using ellipses looks? I wouldn't know what to do from reading the message itself.
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.
@MichaReiser added an example and updated the comment - lmk if looks good. thanks!