Skip to content

Commit

Permalink
PLR2004: Accept 0.0 and 1.0 as common magic values (#9964)
Browse files Browse the repository at this point in the history
## Summary

Accept 0.0 and 1.0 as common magic values. This is in line with the
pylint behaviour, and I think makes sense conceptually.


## Test Plan

Test cases were added to
`crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py`
  • Loading branch information
augustelalande authored Feb 13, 2024
1 parent 5bc0d9c commit 8fba97f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
if argc != 1: # correct
pass

if argc != -1.0: # correct
pass

if argc != 0.0: # correct
pass

if argc != 1.0: # correct
pass

if argc != 2: # [magic-value-comparison]
pass

Expand All @@ -44,6 +53,12 @@
if argc != +2: # [magic-value-comparison]
pass

if argc != -2.0: # [magic-value-comparison]
pass

if argc != +2.0: # [magic-value-comparison]
pass

if __name__ == "__main__": # correct
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ fn is_magic_value(literal_expr: LiteralExpressionRef, allowed_types: &[ConstantT
!matches!(value.to_str(), "" | "__main__")
}
LiteralExpressionRef::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => match value {
#[allow(clippy::float_cmp)]
ast::Number::Float(value) => !(*value == 0.0 || *value == 1.0),
ast::Number::Int(value) => !matches!(*value, Int::ZERO | Int::ONE),
_ => true,
ast::Number::Complex { .. } => true,
},
LiteralExpressionRef::BytesLiteral(_) => true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,67 @@ magic_value_comparison.py:5:4: PLR2004 Magic value used in comparison, consider
6 | pass
|

magic_value_comparison.py:38:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
magic_value_comparison.py:47:12: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
|
36 | pass
37 |
38 | if argc != 2: # [magic-value-comparison]
45 | pass
46 |
47 | if argc != 2: # [magic-value-comparison]
| ^ PLR2004
39 | pass
48 | pass
|

magic_value_comparison.py:41:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable
magic_value_comparison.py:50:12: PLR2004 Magic value used in comparison, consider replacing `-2` with a constant variable
|
39 | pass
40 |
41 | if argc != -2: # [magic-value-comparison]
48 | pass
49 |
50 | if argc != -2: # [magic-value-comparison]
| ^^ PLR2004
42 | pass
51 | pass
|

magic_value_comparison.py:44:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable
magic_value_comparison.py:53:12: PLR2004 Magic value used in comparison, consider replacing `+2` with a constant variable
|
42 | pass
43 |
44 | if argc != +2: # [magic-value-comparison]
51 | pass
52 |
53 | if argc != +2: # [magic-value-comparison]
| ^^ PLR2004
45 | pass
54 | pass
|

magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable
|
54 | pass
55 |
56 | if argc != -2.0: # [magic-value-comparison]
| ^^^^ PLR2004
57 | pass
|

magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable
|
57 | pass
58 |
59 | if argc != +2.0: # [magic-value-comparison]
| ^^^^ PLR2004
60 | pass
|

magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
63 | pi_estimation = 3.14
64 |
65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
78 | pi_estimation = 3.14
79 |
80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
| ^^^^^^^^^^^^^^^^^^^^ PLR2004
66 | pass
81 | pass
|

magic_value_comparison.py:71:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable
magic_value_comparison.py:86:21: PLR2004 Magic value used in comparison, consider replacing `0x3` with a constant variable
|
69 | pass
70 |
71 | if pi_estimation == 0x3: # [magic-value-comparison]
84 | pass
85 |
86 | if pi_estimation == 0x3: # [magic-value-comparison]
| ^^^ PLR2004
72 | pass
87 | pass
|


Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
---
magic_value_comparison.py:59:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable
magic_value_comparison.py:56:12: PLR2004 Magic value used in comparison, consider replacing `-2.0` with a constant variable
|
54 | pass
55 |
56 | if argc != -2.0: # [magic-value-comparison]
| ^^^^ PLR2004
57 | pass
|

magic_value_comparison.py:59:12: PLR2004 Magic value used in comparison, consider replacing `+2.0` with a constant variable
|
57 | pass
58 |
59 | if input_password == "Hunter2": # correct
| ^^^^^^^^^ PLR2004
59 | if argc != +2.0: # [magic-value-comparison]
| ^^^^ PLR2004
60 | pass
|

magic_value_comparison.py:65:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
magic_value_comparison.py:74:22: PLR2004 Magic value used in comparison, consider replacing `"Hunter2"` with a constant variable
|
72 | pass
73 |
74 | if input_password == "Hunter2": # correct
| ^^^^^^^^^ PLR2004
75 | pass
|

magic_value_comparison.py:80:21: PLR2004 Magic value used in comparison, consider replacing `3.141592653589793238` with a constant variable
|
63 | pi_estimation = 3.14
64 |
65 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
78 | pi_estimation = 3.14
79 |
80 | if pi_estimation == 3.141592653589793238: # [magic-value-comparison]
| ^^^^^^^^^^^^^^^^^^^^ PLR2004
66 | pass
81 | pass
|

magic_value_comparison.py:77:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable
magic_value_comparison.py:92:18: PLR2004 Magic value used in comparison, consider replacing `b"something"` with a constant variable
|
75 | user_input = b"Hello, There!"
76 |
77 | if user_input == b"something": # correct
90 | user_input = b"Hello, There!"
91 |
92 | if user_input == b"something": # correct
| ^^^^^^^^^^^^ PLR2004
78 | pass
93 | pass
|


0 comments on commit 8fba97f

Please sign in to comment.