1{ lib
2, stdenv
3, fetchPypi
4, python
5, pythonAtLeast
6, pythonOlder
7, buildPythonPackage
8, writeTextFile
9
10# build-system
11, cython_3
12, gfortran
13, meson-python
14, pkg-config
15, xcbuild
16
17# native dependencies
18, blas
19, lapack
20
21# tests
22, hypothesis
23, pytest-xdist
24, pytestCheckHook
25, setuptools
26, typing-extensions
27}:
28
29assert (!blas.isILP64) && (!lapack.isILP64);
30
31let
32 cfg = writeTextFile {
33 name = "site.cfg";
34 text = lib.generators.toINI {} {
35 ${blas.implementation} = {
36 include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
37 library_dirs = "${blas}/lib:${lapack}/lib";
38 runtime_library_dirs = "${blas}/lib:${lapack}/lib";
39 libraries = "lapack,lapacke,blas,cblas";
40 };
41 lapack = {
42 include_dirs = "${lib.getDev lapack}/include";
43 library_dirs = "${lapack}/lib";
44 runtime_library_dirs = "${lapack}/lib";
45 };
46 blas = {
47 include_dirs = "${lib.getDev blas}/include";
48 library_dirs = "${blas}/lib";
49 runtime_library_dirs = "${blas}/lib";
50 };
51 };
52 };
53in buildPythonPackage rec {
54 pname = "numpy";
55 version = "1.26.1";
56 pyproject = true;
57 disabled = pythonOlder "3.9" || pythonAtLeast "3.13";
58
59 src = fetchPypi {
60 inherit pname version;
61 extension = "tar.gz";
62 hash = "sha256-yMbHLUqfgx8yjvsTEmQqHK+vqoiYHZq3Y2jVDQfZPL4=";
63 };
64
65 patches = [
66 # Disable `numpy/core/tests/test_umath.py::TestComplexFunctions::test_loss_of_precision[complex256]`
67 # on x86_64-darwin because it fails under Rosetta 2 due to issues with trig functions and
68 # 80-bit long double complex numbers.
69 ./disable-failing-long-double-test-Rosetta-2.patch
70 ]
71 # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
72 # Patching of numpy.distutils is needed to prevent it from undoing the
73 # patch to distutils.
74 ++ lib.optionals python.hasDistutilsCxxPatch [
75 ./numpy-distutils-C++.patch
76 ];
77
78 postPatch = ''
79 # fails with multiple errors because we are not using the pinned setuptools version
80 # see https://github.com/numpy/numpy/blob/v1.25.0/pyproject.toml#L7
81 # error: option --single-version-externally-managed not recognized
82 # TypeError: dist must be a Distribution instance
83 rm numpy/core/tests/test_cython.py
84
85 patchShebangs numpy/_build_utils/*.py
86 '';
87
88 nativeBuildInputs = [
89 cython_3
90 gfortran
91 meson-python
92 pkg-config
93 ] ++ lib.optionals (stdenv.isDarwin) [
94 xcbuild.xcrun
95 ];
96
97 buildInputs = [
98 blas
99 lapack
100 ];
101
102 # Causes `error: argument unused during compilation: '-fno-strict-overflow'` due to `-Werror`.
103 hardeningDisable = lib.optionals stdenv.cc.isClang [ "strictoverflow" ];
104
105 # we default openblas to build with 64 threads
106 # if a machine has more than 64 threads, it will segfault
107 # see https://github.com/xianyi/OpenBLAS/issues/2993
108 preConfigure = ''
109 sed -i 's/-faltivec//' numpy/distutils/system_info.py
110 export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
111 '';
112
113 preBuild = ''
114 ln -s ${cfg} site.cfg
115 '';
116
117 enableParallelBuilding = true;
118
119 nativeCheckInputs = [
120 pytest-xdist
121 pytestCheckHook
122 hypothesis
123 setuptools
124 typing-extensions
125 ];
126
127 preCheck = ''
128 cd "$out"
129 '';
130
131 # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
132 pytestFlagsArray = [
133 "-m" "not\\ slow" # fast test suite
134 ];
135
136 # https://github.com/numpy/numpy/issues/24548
137 disabledTests = lib.optionals stdenv.isi686 [
138 "test_new_policy" # AssertionError: assert False
139 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
140 "test_float_remainder_overflow" # AssertionError: FloatingPointError not raised by divmod
141 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
142 ] ++ lib.optionals stdenv.isAarch32 [
143 "test_impossible_feature_enable" # AssertionError: Failed to generate error
144 "test_features" # AssertionError: Failure Detection
145 "test_new_policy" # AssertionError: assert False
146 "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
147 "test_unary_spurious_fpexception"# AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
148 "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
149 "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
150 "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
151 "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
152 "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
153 ] ++ lib.optionals stdenv.isAarch64 [
154 "test_big_arrays" # OOM on a 16G machine
155 ];
156
157 passthru = {
158 # just for backwards compatibility
159 blas = blas.provider;
160 blasImplementation = blas.implementation;
161 inherit cfg;
162 };
163
164 # Disable test
165 # - test_large_file_support: takes a long time and can cause the machine to run out of disk space
166 env.NOSE_EXCLUDE = "test_large_file_support";
167
168 meta = {
169 changelog = "https://github.com/numpy/numpy/releases/tag/v${version}";
170 description = "Scientific tools for Python";
171 homepage = "https://numpy.org/";
172 license = lib.licenses.bsd3;
173 maintainers = with lib.maintainers; [ fridh ];
174 };
175}