AttributeError: module 'numpy' has no attribute 'float' after upgrading to NumPy 2.0
0 reputation · 05 Mar 2020, 15:36 UTC
Context
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.
Goal
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.
Constraints
- The codebase uses
np.floatin type annotations, dtype specifications (dtype=np.float), and Cythoncdefdeclarations. - Third‑party extensions compiled against NumPy 1.x headers may still be loaded at runtime.
- Automated tooling (
numpy.check_compatibility) flags occurrences but does not distinguish between safe built‑in substitutions and cases wherenp.float64ornp.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?