Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
if x == builtins.float("nan"):
pass

# https://github.com/astral-sh/ruff/issues/16374
match number:
# Errors
case np.nan: ...
case math.nan: ...

# No errors
case np.nan(): ...
case math.nan(): ...
Comment thread
InSyncWithFoo marked this conversation as resolved.
Comment thread
ntBre marked this conversation as resolved.
case float('nan'): ...
case npy_nan: ...
Comment thread
InSyncWithFoo marked this conversation as resolved.
Comment thread
ntBre marked this conversation as resolved.

# OK
if math.isnan(x):
pass
Expand Down
9 changes: 9 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,15 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
pylint::rules::useless_exception_statement(checker, expr);
}
}
Stmt::Match(ast::StmtMatch {
subject: _,
cases,
range: _,
Comment thread
ntBre marked this conversation as resolved.
}) => {
if checker.enabled(Rule::NanComparison) {
pylint::rules::nan_comparison_match(checker, cases);
}
}
_ => {}
}
}
16 changes: 15 additions & 1 deletion crates/ruff_linter/src/rules/pylint/rules/nan_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ impl Violation for NanComparison {

/// PLW0177
pub(crate) fn nan_comparison(checker: &Checker, left: &Expr, comparators: &[Expr]) {
for expr in std::iter::once(left).chain(comparators) {
nan_comparison_impl(checker, std::iter::once(left).chain(comparators));
}

/// PLW0177
pub(crate) fn nan_comparison_match(checker: &Checker, cases: &[ast::MatchCase]) {
nan_comparison_impl(
checker,
cases
.iter()
.filter_map(|case| case.pattern.as_match_value().map(|pattern| &*pattern.value)),
);
}

fn nan_comparison_impl<'a>(checker: &Checker, comparators: impl Iterator<Item = &'a Expr>) {
for expr in comparators {
if let Some(qualified_name) = checker.semantic().resolve_qualified_name(expr) {
match qualified_name.segments() {
["numpy", "nan" | "NAN" | "NaN"] => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/pylint/mod.rs
snapshot_kind: text
---
nan_comparison.py:11:9: PLW0177 Comparing against a NaN value; use `math.isnan` instead
|
Expand Down Expand Up @@ -89,3 +88,22 @@ nan_comparison.py:53:9: PLW0177 Comparing against a NaN value; use `math.isnan`
| ^^^^^^^^^^^^^^^^^^^^^ PLW0177
54 | pass
|

nan_comparison.py:59:10: PLW0177 Comparing against a NaN value; use `np.isnan` instead
|
57 | match number:
58 | # Errors
59 | case np.nan: ...
| ^^^^^^ PLW0177
60 | case math.nan: ...
|

nan_comparison.py:60:10: PLW0177 Comparing against a NaN value; use `math.isnan` instead
|
58 | # Errors
59 | case np.nan: ...
60 | case math.nan: ...
| ^^^^^^^^ PLW0177
61 |
62 | # No errors
|