-
Notifications
You must be signed in to change notification settings - Fork 879
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
[pylint
] Implement nan-comparison
(PLW0117
)
#10401
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
155abaf
boilerplate
augustelalande a7d48b3
implement nan-comparison check
augustelalande 83717b4
format docs
augustelalande 4a53b0f
Merge branch 'main' into nan-comparison
charliermarsh caba4ea
Minor tweaks
charliermarsh bf1b1e4
typo
augustelalande File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
crates/ruff_linter/resources/test/fixtures/pylint/nan_comparison.py
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,76 @@ | ||
import math | ||
from math import nan as bad_val | ||
import numpy as np | ||
from numpy import nan as npy_nan | ||
|
||
|
||
x = float('nan') | ||
y = np.NaN | ||
|
||
# PLW0117 | ||
if x == float('nan'): | ||
pass | ||
|
||
# PLW0117 | ||
if x == float('NaN'): | ||
pass | ||
|
||
# PLW0117 | ||
if x == float('NAN'): | ||
pass | ||
|
||
# PLW0117 | ||
if x == float('Nan'): | ||
pass | ||
|
||
# PLW0117 | ||
if x == math.nan: | ||
pass | ||
|
||
# PLW0117 | ||
if x == bad_val: | ||
pass | ||
|
||
# PLW0117 | ||
if y == np.NaN: | ||
pass | ||
|
||
# PLW0117 | ||
if y == np.NAN: | ||
pass | ||
|
||
# PLW0117 | ||
if y == np.nan: | ||
pass | ||
|
||
# PLW0117 | ||
if y == npy_nan: | ||
pass | ||
|
||
# Ok | ||
if math.isnan(x): | ||
pass | ||
|
||
# Ok | ||
if np.isnan(y): | ||
pass | ||
|
||
# Ok | ||
if x == 0: | ||
pass | ||
|
||
# Ok | ||
if x == float('32'): | ||
pass | ||
|
||
# Ok | ||
if x == float(42): | ||
pass | ||
|
||
# Ok | ||
if y == np.inf: | ||
pass | ||
|
||
# Ok | ||
if x == 'nan': | ||
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
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
104 changes: 104 additions & 0 deletions
104
crates/ruff_linter/src/rules/pylint/rules/nan_comparison.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,104 @@ | ||
use ruff_python_ast::{self as ast, Expr}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for comparisons against NaN values. | ||
/// | ||
/// ## Why is this bad? | ||
/// Comparing against a NaN value will always return False even if both values are NaN. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// if x == float("NaN"): | ||
/// pass | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// import math | ||
/// | ||
/// if math.isnan(x): | ||
/// pass | ||
/// ``` | ||
/// | ||
#[violation] | ||
pub struct NanComparison { | ||
using_numpy: bool, | ||
} | ||
|
||
impl Violation for NanComparison { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
let NanComparison { using_numpy } = self; | ||
if *using_numpy { | ||
format!("Comparing against a NaN value, consider using `np.isnan`") | ||
} else { | ||
format!("Comparing against a NaN value, consider using `math.isnan`") | ||
} | ||
} | ||
} | ||
|
||
fn is_nan_float(expr: &Expr) -> bool { | ||
let Expr::Call(call) = expr else { | ||
return false; | ||
}; | ||
|
||
let Expr::Name(ast::ExprName { id, .. }) = ast::helpers::map_subscript(call.func.as_ref()) | ||
else { | ||
return false; | ||
}; | ||
|
||
if id.as_str() != "float" { | ||
return false; | ||
} | ||
|
||
let Some(arg) = call.arguments.find_positional(0) else { | ||
return false; | ||
}; | ||
|
||
if let Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) = arg { | ||
return value.to_str().to_lowercase() == "nan"; | ||
} | ||
|
||
false | ||
} | ||
|
||
/// PLW0117 | ||
pub(crate) fn nan_comparison(checker: &mut Checker, left: &Expr, comparators: &[Expr]) { | ||
for comparison_expr in std::iter::once(left).chain(comparators.iter()) { | ||
if let Some(qualified_name) = checker.semantic().resolve_qualified_name(comparison_expr) { | ||
let segments = qualified_name.segments(); | ||
match segments[0] { | ||
"numpy" => { | ||
if segments[1].to_lowercase() == "nan" { | ||
checker.diagnostics.push(Diagnostic::new( | ||
NanComparison { using_numpy: true }, | ||
comparison_expr.range(), | ||
)); | ||
} | ||
} | ||
"math" => { | ||
if segments[1] == "nan" { | ||
checker.diagnostics.push(Diagnostic::new( | ||
NanComparison { using_numpy: false }, | ||
comparison_expr.range(), | ||
)); | ||
} | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
if is_nan_float(comparison_expr) { | ||
checker.diagnostics.push(Diagnostic::new( | ||
NanComparison { using_numpy: false }, | ||
comparison_expr.range(), | ||
)); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
.../rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW0117_nan_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,82 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
nan_comparison.py:11:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
10 | # PLW0117 | ||
11 | if x == float('nan'): | ||
| ^^^^^^^^^^^^ PLW0117 | ||
12 | pass | ||
| | ||
|
||
nan_comparison.py:15:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
14 | # PLW0117 | ||
15 | if x == float('NaN'): | ||
| ^^^^^^^^^^^^ PLW0117 | ||
16 | pass | ||
| | ||
|
||
nan_comparison.py:19:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
18 | # PLW0117 | ||
19 | if x == float('NAN'): | ||
| ^^^^^^^^^^^^ PLW0117 | ||
20 | pass | ||
| | ||
|
||
nan_comparison.py:23:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
22 | # PLW0117 | ||
23 | if x == float('Nan'): | ||
| ^^^^^^^^^^^^ PLW0117 | ||
24 | pass | ||
| | ||
|
||
nan_comparison.py:27:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
26 | # PLW0117 | ||
27 | if x == math.nan: | ||
| ^^^^^^^^ PLW0117 | ||
28 | pass | ||
| | ||
|
||
nan_comparison.py:31:9: PLW0117 Comparing against a NaN value, consider using `math.isnan` | ||
| | ||
30 | # PLW0117 | ||
31 | if x == bad_val: | ||
| ^^^^^^^ PLW0117 | ||
32 | pass | ||
| | ||
|
||
nan_comparison.py:35:9: PLW0117 Comparing against a NaN value, consider using `np.isnan` | ||
| | ||
34 | # PLW0117 | ||
35 | if y == np.NaN: | ||
| ^^^^^^ PLW0117 | ||
36 | pass | ||
| | ||
|
||
nan_comparison.py:39:9: PLW0117 Comparing against a NaN value, consider using `np.isnan` | ||
| | ||
38 | # PLW0117 | ||
39 | if y == np.NAN: | ||
| ^^^^^^ PLW0117 | ||
40 | pass | ||
| | ||
|
||
nan_comparison.py:43:9: PLW0117 Comparing against a NaN value, consider using `np.isnan` | ||
| | ||
42 | # PLW0117 | ||
43 | if y == np.nan: | ||
| ^^^^^^ PLW0117 | ||
44 | pass | ||
| | ||
|
||
nan_comparison.py:47:9: PLW0117 Comparing against a NaN value, consider using `np.isnan` | ||
| | ||
46 | # PLW0117 | ||
47 | if y == npy_nan: | ||
| ^^^^^^^ PLW0117 | ||
48 | pass | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
Also interested to hear if there is a better way to do this
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.
I tweaked it a little bit but it's generally correct. Kind of tedious to write out, but correct!