Fix Python 3.12 and NumPy 2.x compatibility issues#47
Open
sackio wants to merge 6 commits into
Open
Conversation
The ^ operator is Poetry-specific syntax and not valid in PEP 508 build requirements. This was causing pip install failures with: "invalid-pyproject-build-system-requires" Changed from: Cython>=^0.29 Changed to: Cython>=0.29 Fixes compatibility with modern pip versions that strictly validate pyproject.toml build-system.requires per PEP 518.
Python 3.12 removed the distutils module. This update replaces distutils imports with their setuptools equivalents: - distutils.command.build_ext -> setuptools.command.build_ext - distutils.core.Distribution -> setuptools.dist.Distribution - distutils.core.Extension -> setuptools.Extension This maintains the same functionality while being compatible with Python 3.12+.
build.py imports from setuptools (build_ext, Distribution, Extension) but setuptools was not declared in build-system.requires, causing: ModuleNotFoundError: No module named 'setuptools' This is required for the build.py script to execute successfully during the wheel build process.
np.int_ was removed in NumPy 2.0. The correct replacement is np.intp which represents a platform-specific integer pointer type (equivalent to the old np.int_ behavior). Changed in zigzag/core.pyx: - Line 107: np.zeros(t_n, dtype=np.int_) -> np.intp - Line 213: np.zeros(len(pivots), dtype=np.int_) -> np.intp This fixes the Cython compilation error: "zigzag/core.pyx:107:16: Invalid type."
Replaced deprecated int_t with cnp.intp_t throughout core.pyx. The int_t type is no longer available in modern Cython with NumPy 2.x. Changes: - Added 'cimport numpy as cnp' to imports - Removed 'int_t' from imports - Replaced all 12 occurrences of 'int_t' with 'cnp.intp_t' This resolves the Cython compilation error: "zigzag/core.pyx:107:16: Invalid type."
The get_outputs() method can fail when build_py command doesn't exist in the minimal Distribution setup. Added try/except fallback that uses glob to manually find built extension files (.so/.pyd) in build_lib.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes multiple compatibility issues preventing zigzag from building on Python 3.12 with NumPy 2.x and Cython 3.x. These changes enable zigzag to work with modern Python environments.
Problem
The current version fails to install on Python 3.12+ with errors:
error: invalid-pyproject-build-system-requires - invalid requirement: 'Cython>=^0.29'ModuleNotFoundError: No module named 'distutils'(removed in Python 3.12)Invalid typeerrors withnp.int_(removed in NumPy 2.x)Invalid typeerrors withint_t(deprecated in Cython 3.x)get_outputs()fails withoutbuild_pycommandChanges
1. Fix pyproject.toml (commit e0c62a0)
File:
pyproject.tomlBefore:
After:
Reason: The
^operator is Poetry-specific syntax and not valid in PEP 508/PEP 440 dependency specifications used bybuild-system.requires.2. Replace distutils with setuptools (commit 013d2cc)
File:
build.pyChanges:
from distutils.command.build_ext import build_ext→from setuptools.command.build_ext import build_extfrom distutils.core import Distribution, Extension→from setuptools.dist import Distribution+from setuptools import ExtensionReason: Python 3.12 removed the
distutilsmodule. Usesetuptoolsas the modern replacement.3. Add setuptools to build requirements (commit 922933f)
File:
pyproject.tomlBefore:
After:
Reason: Since
build.pynow imports from setuptools, it must be inbuild-system.requires.4. Fix NumPy 2.x type compatibility (commit 98167d0)
File:
zigzag/core.pyxChanges:
dtype=np.int_→dtype=np.intpdtype=np.int_→dtype=np.intpReason: NumPy 2.0 removed
np.int_type alias. Usenp.intp(platform-specific signed integer type used for indexing).5. Fix Cython 3.x type compatibility (commit 7a2df2e)
File:
zigzag/core.pyxChanges:
cimport numpy as cnpint_twithcnp.intp_tReason: The
int_ttype is deprecated in modern Cython with NumPy 2.x. Usecnp.intp_tfor platform-specific integer types.6. Add error handling to build script (commit 1b7bcf3)
File:
build.pyChanges: Added try/except block with glob fallback for
cmd.get_outputs():Reason:
get_outputs()can fail whenbuild_pycommand doesn't exist in the minimal Distribution setup. Fallback uses glob to find built.so/.pydfiles.Testing
Tested successfully on:
Build output:
Import test:
Impact
This fixes installation for:
smartmoneyconceptsthat require zigzagBackward Compatibility
All changes maintain backward compatibility:
Cython>=0.29still accepts older versionssetuptoolsis widely availablenp.intpworks on all NumPy versionscnp.intp_tis the recommended modern Cython typeReferences