-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds [Pylint `R2004`](https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/magic-value-comparison.html#magic-value-comparison-r2004) Feel free to suggest changes and additions, I have tried to maintain parity with the Pylint implementation [`magic_value.py`](https://github.com/PyCQA/pylint/blob/main/pylint/extensions/magic_value.py) See #970
- Loading branch information
Showing
10 changed files
with
206 additions
and
0 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
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,71 @@ | ||
"""Check that magic values are not used in comparisons""" | ||
import cmath | ||
|
||
user_input = 10 | ||
|
||
if 10 > user_input: # [magic-value-comparison] | ||
pass | ||
|
||
if 10 == 100: # [comparison-of-constants] R0133 | ||
pass | ||
|
||
if 1 == 3: # [comparison-of-constants] R0133 | ||
pass | ||
|
||
x = 0 | ||
if 4 == 3 == x: # [comparison-of-constants] R0133 | ||
pass | ||
|
||
time_delta = 7224 | ||
ONE_HOUR = 3600 | ||
|
||
if time_delta > ONE_HOUR: # correct | ||
pass | ||
|
||
argc = 1 | ||
|
||
if argc != -1: # correct | ||
pass | ||
|
||
if argc != 0: # correct | ||
pass | ||
|
||
if argc != 1: # correct | ||
pass | ||
|
||
if argc != 2: # [magic-value-comparison] | ||
pass | ||
|
||
if __name__ == "__main__": # correct | ||
pass | ||
|
||
ADMIN_PASSWORD = "SUPERSECRET" | ||
input_password = "password" | ||
|
||
if input_password == "": # correct | ||
pass | ||
|
||
if input_password == ADMIN_PASSWORD: # correct | ||
pass | ||
|
||
if input_password == "Hunter2": # [magic-value-comparison] | ||
pass | ||
|
||
PI = 3.141592653589793238 | ||
pi_estimation = 3.14 | ||
|
||
if pi_estimation == 3.141592653589793238: # [magic-value-comparison] | ||
pass | ||
|
||
if pi_estimation == PI: # correct | ||
pass | ||
|
||
HELLO_WORLD = b"Hello, World!" | ||
user_input = b"Hello, There!" | ||
|
||
if user_input == b"something": # [magic-value-comparison] | ||
pass | ||
|
||
if user_input == HELLO_WORLD: # correct | ||
pass | ||
|
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 |
---|---|---|
|
@@ -1445,6 +1445,10 @@ | |
"PLR1701", | ||
"PLR172", | ||
"PLR1722", | ||
"PLR2", | ||
"PLR20", | ||
"PLR200", | ||
"PLR2004", | ||
"PLW", | ||
"PLW0", | ||
"PLW01", | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use itertools::Itertools; | ||
use rustpython_ast::{Constant, Expr, ExprKind}; | ||
|
||
use crate::ast::types::Range; | ||
use crate::checkers::ast::Checker; | ||
use crate::registry::Diagnostic; | ||
use crate::violations; | ||
|
||
fn is_magic_value(constant: &Constant) -> bool { | ||
match constant { | ||
Constant::None => false, | ||
// E712 `if True == do_something():` | ||
Constant::Bool(_) => false, | ||
Constant::Str(value) => !matches!(value.as_str(), "" | "__main__"), | ||
Constant::Bytes(_) => true, | ||
Constant::Int(value) => !matches!(value.try_into(), Ok(-1 | 0 | 1)), | ||
Constant::Tuple(_) => true, | ||
Constant::Float(_) => true, | ||
Constant::Complex { .. } => true, | ||
Constant::Ellipsis => true, | ||
} | ||
} | ||
|
||
/// PLR2004 | ||
pub fn magic_value_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) { | ||
for (left, right) in std::iter::once(left) | ||
.chain(comparators.iter()) | ||
.tuple_windows() | ||
{ | ||
// If both of the comparators are constant, skip rule for the whole expression. | ||
// R0133: comparison-of-constants | ||
if matches!(left.node, ExprKind::Constant { .. }) | ||
&& matches!(right.node, ExprKind::Constant { .. }) | ||
{ | ||
return; | ||
} | ||
} | ||
|
||
for comparison_expr in std::iter::once(left).chain(comparators.iter()) { | ||
if let ExprKind::Constant { value, .. } = &comparison_expr.node { | ||
if is_magic_value(value) { | ||
let diagnostic = Diagnostic::new( | ||
violations::MagicValueComparison(value.to_string()), | ||
Range::from_located(comparison_expr), | ||
); | ||
|
||
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
55 changes: 55 additions & 0 deletions
55
src/pylint/snapshots/ruff__pylint__tests__PLR2004_magic_value_comparison.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,55 @@ | ||
--- | ||
source: src/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
MagicValueComparison: "10" | ||
location: | ||
row: 6 | ||
column: 3 | ||
end_location: | ||
row: 6 | ||
column: 5 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MagicValueComparison: "2" | ||
location: | ||
row: 36 | ||
column: 11 | ||
end_location: | ||
row: 36 | ||
column: 12 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MagicValueComparison: "'Hunter2'" | ||
location: | ||
row: 51 | ||
column: 21 | ||
end_location: | ||
row: 51 | ||
column: 30 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MagicValueComparison: "3.141592653589793" | ||
location: | ||
row: 57 | ||
column: 20 | ||
end_location: | ||
row: 57 | ||
column: 40 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
MagicValueComparison: "b'something'" | ||
location: | ||
row: 66 | ||
column: 17 | ||
end_location: | ||
row: 66 | ||
column: 29 | ||
fix: ~ | ||
parent: ~ | ||
|
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