wesselb / lab

A generic interface for linear algebra backends
MIT License
68 stars 5 forks source link

Incompatibility with NumPy 2.0 due to deprecated `np.sctypes` #20

Closed DaniJonesOcean closed 2 months ago

DaniJonesOcean commented 2 months ago

Summary

The lab package currently specifies numpy>=1.16 as a requirement, which leads to the installation of NumPy 2.0 when setting up a new environment or upgrading existing packages. Due to significant changes in NumPy 2.0, specifically the removal of np.sctypes, the lab package fails to import when used with the latest version of NumPy.

Environment

Steps to Reproduce

  1. Set up a Python 3.10 environment with pip.
  2. Install the lab package with the latest version of NumPy: pip install 'numpy>=1.16' lab (NumPy 2.0 will be installed).
  3. Attempt to import that uses np.sctypes, resulting in an AttributeError (e.g. import lab as B)

Expected Behavior

The lab package should be able to import successfully without any AttributeError regarding np.sctypes.

Actual Behavior

An AttributeError is raised when importing the lab package with NumPy 2.0 installed:

AttributeError: `np.sctypes` was removed in the NumPy 2.0 release. Access dtypes explicitly instead.. Did you mean: 'dtypes'?

For example, when you try to import the deepsensor package, which uses the lab package:

  File "/home/dannes/deepsensor_numpy_test/lib/python3.10/site-packages/lab/__init__.py", line 9, in <module>
    from .generic import *
  File "/home/dannes/deepsensor_numpy_test/lib/python3.10/site-packages/lab/generic.py", line 11, in <module>
    from .types import (
  File "/home/dannes/deepsensor_numpy_test/lib/python3.10/site-packages/lab/types.py", line 99, in <module>
    Int = Union[tuple([int, Dimension] + np.sctypes["int"] + np.sctypes["uint"])]
  File "/home/dannes/deepsensor_numpy_test/lib/python3.10/site-packages/numpy/__init__.py", line 397, in __getattr__
    raise AttributeError(
AttributeError: `np.sctypes` was removed in the NumPy 2.0 release. Access dtypes explicitly instead.. Did you mean: 'dtypes'?

Suggested Solution

Temporarily restrict the version of NumPy in setup.py to prevent installation of incompatible versions. I suggest changing the requirement in setup.py to:

"numpy<2"
DaniJonesOcean commented 2 months ago

Posted related issue on DeepSensor repository:

https://github.com/alan-turing-institute/deepsensor/issues/118

wesselb commented 2 months ago

Hey @DaniJonesOcean! Thanks for opening an issue. Let me look into this. I think there should be a simple fix by changing the import. I'll get back to you shortly.

wesselb commented 2 months ago

@DaniJonesOcean This should be fixed in the latest release v1.6.6.

DaniJonesOcean commented 2 months ago

Great, thanks for your quick work on this, @wesselb!