1{
2 lib,
3 stdenv,
4 fetchPypi,
5 python,
6 numpy_2,
7 pythonAtLeast,
8 pythonOlder,
9 buildPythonPackage,
10 writeTextFile,
11
12 # build-system
13 cython,
14 gfortran,
15 meson-python,
16 mesonEmulatorHook,
17 pkg-config,
18 xcbuild,
19
20 # native dependencies
21 blas,
22 lapack,
23
24 # Reverse dependency
25 sage,
26
27 # tests
28 hypothesis,
29 pytest-xdist,
30 pytestCheckHook,
31 setuptools,
32 typing-extensions,
33}:
34
35assert (!blas.isILP64) && (!lapack.isILP64);
36
37let
38 cfg = writeTextFile {
39 name = "site.cfg";
40 text = lib.generators.toINI { } {
41 ${blas.implementation} = {
42 include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
43 library_dirs = "${blas}/lib:${lapack}/lib";
44 runtime_library_dirs = "${blas}/lib:${lapack}/lib";
45 libraries = "lapack,lapacke,blas,cblas";
46 };
47 lapack = {
48 include_dirs = "${lib.getDev lapack}/include";
49 library_dirs = "${lapack}/lib";
50 runtime_library_dirs = "${lapack}/lib";
51 };
52 blas = {
53 include_dirs = "${lib.getDev blas}/include";
54 library_dirs = "${blas}/lib";
55 runtime_library_dirs = "${blas}/lib";
56 };
57 };
58 };
59in
60buildPythonPackage rec {
61 pname = "numpy";
62 version = "2.3.2";
63 pyproject = true;
64
65 disabled = pythonOlder "3.11";
66
67 src = fetchPypi {
68 inherit pname version;
69 extension = "tar.gz";
70 hash = "sha256-4EhqEewwzey1PxhNSW0caiB4bIHlXkFkAnATAFb47kg=";
71 };
72
73 patches = lib.optionals python.hasDistutilsCxxPatch [
74 # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
75 # Patching of numpy.distutils is needed to prevent it from undoing the
76 # patch to distutils.
77 ./numpy-distutils-C++.patch
78 ];
79
80 postPatch = ''
81 # remove needless reference to full Python path stored in built wheel
82 substituteInPlace numpy/meson.build \
83 --replace-fail 'py.full_path()' "'python'"
84 '';
85
86 build-system = [
87 cython
88 gfortran
89 meson-python
90 pkg-config
91 ]
92 ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild.xcrun ]
93 ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];
94
95 # we default openblas to build with 64 threads
96 # if a machine has more than 64 threads, it will segfault
97 # see https://github.com/OpenMathLib/OpenBLAS/issues/2993
98 preConfigure = ''
99 sed -i 's/-faltivec//' numpy/distutils/system_info.py
100 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
101 '';
102
103 buildInputs = [
104 blas
105 lapack
106 ];
107
108 preBuild = ''
109 ln -s ${cfg} site.cfg
110 '';
111
112 enableParallelBuilding = true;
113
114 nativeCheckInputs = [
115 hypothesis
116 pytestCheckHook
117 pytest-xdist
118 setuptools
119 typing-extensions
120 ];
121
122 preCheck = ''
123 pushd $out
124 # For numpy-config executable to be available during tests
125 export PATH=$PATH:$out/bin
126 '';
127
128 postCheck = ''
129 popd
130 '';
131
132 # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
133 disabledTestMarks = [
134 "slow" # fast test suite
135 ];
136
137 disabledTests = [
138 # Tries to import numpy.distutils.msvccompiler, removed in setuptools 74.0
139 "test_api_importable"
140 ]
141 ++ lib.optionals (pythonAtLeast "3.13") [
142 # https://github.com/numpy/numpy/issues/26713
143 "test_iter_refcount"
144 ]
145 ++ lib.optionals stdenv.hostPlatform.isAarch32 [
146 # https://github.com/numpy/numpy/issues/24548
147 "test_impossible_feature_enable" # AssertionError: Failed to generate error
148 "test_features" # AssertionError: Failure Detection
149 "test_new_policy" # AssertionError: assert False
150 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
151 "test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
152 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
153 "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
154 "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
155 "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
156 "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
157 ]
158 ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
159 # AssertionError: (np.int64(0), np.longdouble('9.9999999999999994515e-21'), np.longdouble('3.9696755572509052902e+20'), 'arctanh')
160 "test_loss_of_precision"
161 ]
162 ++ lib.optionals (stdenv.hostPlatform ? gcc.arch) [
163 # remove if https://github.com/numpy/numpy/issues/27460 is resolved
164 "test_validate_transcendentals"
165 ];
166
167 passthru = {
168 # just for backwards compatibility
169 blas = blas.provider;
170 blasImplementation = blas.implementation;
171 inherit cfg;
172 coreIncludeDir = "${numpy_2}/${python.sitePackages}/numpy/_core/include";
173 tests = {
174 inherit sage;
175 };
176 };
177
178 meta = {
179 changelog = "https://github.com/numpy/numpy/releases/tag/v${version}";
180 description = "Scientific tools for Python";
181 homepage = "https://numpy.org/";
182 license = lib.licenses.bsd3;
183 maintainers = with lib.maintainers; [ doronbehar ];
184 };
185}