github-actions[bot] 4a8cbac06e
Create rule S6725: Equality checks should not be made against "numpy.nan" (#2955)
You can preview this rule [here](https://sonarsource.github.io/rspec/#/rspec/S6725/python) (updated a few minutes after each push).

## Review

A dedicated reviewer checked the rule description successfully for:

- [ ] logical errors and incorrect information
- [ ] information gaps and missing content
- [ ] text style and tone
- [ ] PR summary and labels follow [the guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)

Co-authored-by: maksim-grebeniuk-sonarsource <maksim-grebeniuk-sonarsource@users.noreply.github.com>
Co-authored-by: Maksim Grebeniuk <maksim.grebeniuk@sonarsource.com>
2023-09-22 12:09:04 +02:00

60 lines
1.4 KiB
Plaintext

This rule raises an issue when an equality check is made against ``++numpy.nan++``.
== Why is this an issue?
The ``++numpy.nan++`` is a floating point representation of Not a Number (NaN) used as a placeholder for undefined or missing values in numerical computations.
Equality checks of variables against ``++numpy.nan++`` in NumPy will always be ``++False++`` due to the special nature of ``++numpy.nan++``. This can lead to unexpected and incorrect results.
Instead of standard comparison the ``++numpy.isnan()++`` function should be used.
=== Code examples
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
----
import numpy as np
x = np.nan
if x == np.nan: # Noncompliant: always False
...
----
==== Compliant solution
[source,python,diff-id=1,diff-type=compliant]
----
import numpy as np
x = np.nan
if np.isnan(x):
...
----
== Resources
=== Documentation
* NumPy API Reference - https://numpy.org/doc/stable/reference/constants.html#numpy.nan[numpy.nan]
* NumPy API Reference - https://numpy.org/doc/stable/reference/generated/numpy.isnan.html[numpy.isnan()]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use 'numpy.isnan()' function instead of direct comparison.
'''
== Comments And Links
(visible only on this page)
endif::env-github,rspecator-view[]