Description
Describe the issue:
np.bool is subscriptable during type checking but not subscriptable at run time. assert_type
involving np.bool[bool]
, np.bool[Literal[False]]
, np.bool[Any]
, etc. raises TypeErrror at run time.
Workaround: if TYPE_CHECKING: assert_type(...)
.
Reproduce the code example:
import numpy as np
from typing import assert_type
x = np.array([True, False], np.bool)
assert_type(x.dtype, np.dtype[np.bool[bool]])
Error message:
Traceback (most recent call last):
File "C:\Users\bkeryan\AppData\Local\Temp\test.py", line 4, in <module>
assert_type(x.dtype, np.dtype[np.bool[bool]])
~~~~~~~^^^^^^
TypeError: type 'numpy.bool' is not subscriptable
Python and NumPy Versions:
numpy 2.3.0
python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)]
Runtime Environment:
[{'numpy_version': '2.3.0',
'python': '3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 '
'64 bit (AMD64)]',
'uname': uname_result(system='Windows', node='USAUSLT-C1H8Q34', release='10', version='10.0.22631', machine='AMD64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': 'C:\Users\bkeryan\AppData\Local\Temp\.venv\Lib\site-packages\numpy.libs\libscipy_openblas64_-13e2df515630b4a41f92893938845698.dll',
'internal_api': 'openblas',
'num_threads': 22,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.29'}]
Context for the issue:
Writing tests for a generic class that wraps a NumPy array. The class has overloaded constructors and classmethods and I use assert_type
to verify that the overloads have the correct return type.
I also filed a TYP issue about the same use case: #29245