From 4929b357898524696b88bb68a8859e5a4f864d3c Mon Sep 17 00:00:00 2001 From: px39n <53490111+px39n@users.noreply.github.com> Date: Mon, 8 Jul 2024 16:09:21 +0800 Subject: [PATCH] Fix ZFPY import error (#540) * Update pyproject.toml When numpy >=2.0.0 from numcodecs.zfpy import ZFPY raise error [ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject] The error is caused by zfpy library crashing with recent ABI breaking of numpy udpate. So we'd better force numpy <2.0 unless zfpy update again. Reference: (https://stackoverflow.com/questions/66060487/valueerror-numpy-ndarray-size-changed-may-indicate-binary-incompatibility-exp) * Update zfpy.py Add warning when user import zfpy but with numpy >=2.0.0 * Update pyproject.toml ensure that the zfpy optional dependency has this constraint. * Update zfpy.py * Update numcodecs/zfpy.py Co-authored-by: jakirkham * Update numcodecs/zfpy.py Co-authored-by: jakirkham * Update zfpy.py * Update release.rst * Update release.rst * style: pre-commit fixes * Update zfpy.py --------- Co-authored-by: jakirkham Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- docs/release.rst | 2 ++ numcodecs/zfpy.py | 21 +++++++++++++++++++-- pyproject.toml | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/release.rst b/docs/release.rst index 8d36c444..b569015c 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -31,6 +31,8 @@ Fix By :user:`Elliott Sales de Andrade `, :issue:`487`. * Fix Upgrade to Zstd 1.5.5 due to potential corruption. By :user:`Mark Kittisopikul `, :issue:`429` +* Add version constraint(<2.0) for numpy in zfpy. + By :user:`Tom Liang `, :issue:`540`. Maintenance ~~~~~~~~~~~ diff --git a/numcodecs/zfpy.py b/numcodecs/zfpy.py index 788b1f24..40d6b8e4 100644 --- a/numcodecs/zfpy.py +++ b/numcodecs/zfpy.py @@ -1,9 +1,26 @@ from contextlib import suppress +from importlib.metadata import PackageNotFoundError, version +import warnings _zfpy = None -with suppress(ImportError): - import zfpy as _zfpy +_zfpy_version: tuple = () +with suppress(PackageNotFoundError): + _zfpy_version = tuple(map(int, version("zfpy").split("."))) + +if _zfpy_version: + # Check NumPy version + _numpy_version: tuple = tuple(map(int, version("numpy").split('.'))) + if _numpy_version >= (2, 0, 0) and _zfpy_version <= (1, 0, 1): + _zfpy_version = () + warnings.warn( + "NumPy version >= 2.0.0 detected. The zfpy library is incompatible with this version of NumPy. " + "Please downgrade to NumPy < 2.0.0 or wait for an update from zfpy.", + UserWarning, + ) + else: + with suppress(ImportError): + import zfpy as _zfpy if _zfpy: from .abc import Codec diff --git a/pyproject.toml b/pyproject.toml index c1868168..9826168f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ msgpack = [ ] zfpy = [ "zfpy>=1.0.0", + "numpy<2.0.0", ] pcodec = [ "pcodec>=0.1.0",