AttributeError: module 'numpy' has no attribute 'float' after upgrading to NumPy 2.0
0 reputation · 05 Mar 2020, 15:36 UTC
0 reputation · 05 Mar 2020, 15:36 UTC
A codebase upgraded from NumPy 1.26 to 2.0 encounters AttributeError: module 'numpy' has no attribute 'float' (and similarly for int, bool, complex, object, str). These aliases were deprecated since NumPy 1.20 and removed in 2.0 in favor of Python built‑in types.
Identify a systematic migration strategy that replaces all removed type aliases across a large, mixed Python/Cython codebase without introducing subtle type‑casting regressions, while maintaining compatibility with downstream libraries that may still expect the old scalar types in public APIs.
np.float in type annotations, dtype specifications (dtype=np.float), and Cython cdef declarations.numpy.check_compatibility) flags occurrences but does not distinguish between safe built‑in substitutions and cases where np.float64 or np.dtype('float64') is semantically required.What is the recommended pattern for replacing dtype=np.float in array‑creation calls to preserve exact precision semantics? How should Cython cdef np.float_t declarations be updated to match the new C‑API scalar typedefs? Does the numpy.compat shim layer provide any forward‑compatibility helpers for incremental migration?
34775 reputation · 05 Mar 2020, 15:55 UTC
NumPy 2.0 removed the scalar type aliases np.float, np.int, np.bool, np.complex, np.object, and np.str that had been deprecated since 1.20. In a large mixed Python/Cython codebase, these appearances fall into three contexts: dtype specifications in array-creation calls, type annotations, and Cython cdef declarations. Each requires a slightly different replacement strategy to avoid subtle precision or compatibility regressions.
dtype=np.floatdtype=np.float with dtype=np.float64 to preserve the original 64-bit IEEE 754 double semantics. Use this when the array's precision matters for downstream calculations or when the codebase previously relied on the alias's guaranteed width.float. Note that on most platforms float is also 64-bit, but the type is not tied to NumPy's internal layout. After replacement, inspect arr.dtype to confirm the expected itemsize (8 bytes).arr.itemsize, and compares values against a reference created with the original np.float (if available) using numpy.testing.assert_array_equal or np.allclose. This step catches silent casting differences introduced by downstream libraries or mixed-precision code paths.cdef np.float_t declarationsIn NumPy 2.0, the C-level typedef np.float_t maps to C double. A Cython declaration such as
cdef np.float_t xshould be updated to
cdef double xif the 64-bit width is intended. If the original code used np.float to denote a platform-dependent float width (historically always 64-bit in NumPy, but conceptually wider on some architectures), verify the target platform's sizeof(double). Recompile the Cython extension against NumPy 2.0 headers; do not rely on source-level substitution alone, because the compiled .so or .pyd still embeds the old typedef layout.
numpy.compat shim layerThe numpy.compat module provides compatibility checks and deprecation warnings but does not ship a shim that re‑installs the removed scalar aliases. Incremental migration therefore requires automated source-level replacement followed by manual verification, rather than a drop‑in compatibility layer.
If your codebase contains Cython modules compiled against NumPy 1.x headers that are still loaded at runtime, confirm whether those extensions have been rebuilt for NumPy 2.0. Runtime mixing of 1.x and 2.0 type layouts can produce type‑mismatch errors that a simple find‑and‑replace won't resolve. Rebuilding those modules against the 2.0 headers is the recommended next step.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.