Skip to content

NPY001 should not replace np.bool by bool (at least in type hints) #11093

@bersbersbers

Description

@bersbersbers

bug.py

import numpy as np
import numpy.typing as npt

bad1: npt.NDArray[np.bool_] = np.array([True])
good: npt.NDArray[np.bool] = np.array([True])
bad2: npt.NDArray[bool] = np.array([True])

For mypy v1.9.0, only np.bool is acceptable:

bug.py:4: error: Name "np.bool_" is not defined  [name-defined]
bug.py:6: error: Type argument "bool" of "NDArray" must be a subtype of "generic"  [type-var]
bug.py:6: note: See https://mypy.rtfd.io/en/stable/_refs.html#code-type-var for more info
Found 2 errors in 1 file (checked 1 source file)

However, NPY001 does not like that solution:

ruff check --isolated --select NPY001 bug.py
bug.py:5:19: NPY001 [*] Type alias `np.bool` is deprecated, replace with builtin type
Found 1 error.
[*] 1 fixable with the `--fix` option.

--fix gives

import numpy as np
import numpy.typing as npt

bad1: npt.NDArray[np.bool_] = np.array([True])
good: npt.NDArray[bool] = np.array([True])
bad2: npt.NDArray[bool] = np.array([True])

Keywords: numpy 2, deprecation, bool, bool_

ruff 0.4.1
mypy 1.9.0
numpy 2.0.0rc1

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-infoMore information is needed from the issue author

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions