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

Closed
bersbersbers opened this issue Apr 22, 2024 · 11 comments
Closed

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

bersbersbers opened this issue Apr 22, 2024 · 11 comments
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_.

@bersbersbers
Copy link
Contributor Author

bersbersbers commented Jun 3, 2024

fyi, numpy 2 will not be released until June 16 (numpy/numpy#24300 (comment)).

@mtsokol
Copy link
Contributor

mtsokol commented Jun 4, 2024

Right, I'll adjust the NPY001 to NumPy 2.0 changes to avoid a conflict with the NPY201 rule (namely np.bool was restored and points to NumPy's bool instead of a bool builtin, and np.long is back and points to C's long and unsigned long instead of a int builtin).

@mtsokol
Copy link
Contributor

mtsokol commented Jun 4, 2024

I opened a PR for it: #11735

@mtsokol
Copy link
Contributor

mtsokol commented Jun 4, 2024

@bersbersbers FYI we noticed that the new np.bool_ alias is missing a typing stub so we readded it (numpy/numpy#26210). If you upgrade to 2.0.0rc2 mypy shouldn't complain.

charliermarsh pushed a commit that referenced this issue Jun 4, 2024
Hi!

This PR addresses #11093.

It skips `np.bool` and `np.long` replacements as both of these names
were reintroduced in NumPy 2.0 with a different meaning
(numpy/numpy#24922,
numpy/numpy#25080).
With this change `NPY001` will no longer conflict with `NPY201`. For
projects using NumPy 1.x `np.bool` and `np.long` has been deprecated and
removed long time ago, and accessing them yields an informative error
message.
@mtsokol
Copy link
Contributor

mtsokol commented Jun 6, 2024

I think we can close it as resolved.

@bersbersbers
Copy link
Contributor Author

Yes, verified 0.4.8. Thanks!

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

4 participants