Skip to content

Do not match-case against np.nan #16374

@RolandSaur

Description

@RolandSaur

Summary

numpy's not-a-number (np.nan) will never be matched in a match-case statement. But neither ruff nor mypy complain.

The following prints out Found a number: nan, because np.nan does not match with np.nan.

import numpy as np


def my_func(number: float) -> None:
    match number:
        case np.nan:
            print("Found np.nan")
        case float(number):
            print(f"Found a number: {number}")


my_func(np.nan)

I believe using np.nan in a match-case statement should be considered a mistake and it would be cool to have a rule as part of the numpy-specific-rules

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions