Skip to content
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

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

Open
bersbersbers opened this issue Apr 22, 2024 · 5 comments
Open
Labels
needs-info More information is needed from the issue author

Comments

@bersbersbers
Copy link
Contributor

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

@bersbersbers
Copy link
Contributor Author

See also numpy/numpy#25080

@charliermarsh
Copy link
Member

Is this considered "recommended" by NumPy, or is it a bug or limitation in the type definitions?

@charliermarsh
Copy link
Member

\cc @mtsokol if you know!

@charliermarsh charliermarsh added the needs-info More information is needed from the issue author label Apr 22, 2024
@bersbersbers
Copy link
Contributor Author

Is this considered "recommended" by NumPy

This. See https://numpy.org/devdocs/release/2.0.0-notes.html#changes, 5th bullet in "Changes".

@tmke8
Copy link
Contributor

tmke8 commented Apr 30, 2024

So, it seems np.bool used to be an alias for built-in bool while np.bool_ was a separate type. The np.bool alias was then deprecated in numpy 1.20, which is what NPY001 is about. But then numpy 2.0 renamed np.bool_ to np.bool (while keeping np.bool_ as an alias for the new np.bool for backwards compatibility with numpy 1.x), so now the depreciation from numpy 1.20 conflicts with the new recommended name in numpy 2.0 for np.bool_.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info More information is needed from the issue author
Projects
None yet
Development

No branches or pull requests

3 participants