`buildDotnetModule`: add support for installing pre-release tools (#374663)
[NixPkgs.git] / pkgs / development / python-modules / numpy / 2.nix
blob90f1fc68e3c0f4e759518a7fa8d0dfefc5d6c3ac
2   lib,
3   stdenv,
4   fetchPypi,
5   python,
6   numpy_2,
7   pythonAtLeast,
8   pythonOlder,
9   buildPythonPackage,
10   writeTextFile,
12   # build-system
13   cython,
14   gfortran,
15   meson-python,
16   mesonEmulatorHook,
17   pkg-config,
18   xcbuild,
20   # native dependencies
21   blas,
22   lapack,
24   # Reverse dependency
25   sage,
27   # tests
28   hypothesis,
29   pytest-xdist,
30   pytestCheckHook,
31   setuptools,
32   typing-extensions,
35 assert (!blas.isILP64) && (!lapack.isILP64);
37 let
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   };
60 buildPythonPackage rec {
61   pname = "numpy";
62   version = "2.2.0";
63   pyproject = true;
65   disabled = pythonOlder "3.10";
67   src = fetchPypi {
68     inherit pname version;
69     extension = "tar.gz";
70     hash = "sha256-FA3YD/iYGlg6YJgL4aZVBo+K3r96RaBqaFjIc/zc1KA=";
71   };
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   ];
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   '';
86   build-system =
87     [
88       cython
89       gfortran
90       meson-python
91       pkg-config
92     ]
93     ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild.xcrun ]
94     ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];
96   # we default openblas to build with 64 threads
97   # if a machine has more than 64 threads, it will segfault
98   # see https://github.com/OpenMathLib/OpenBLAS/issues/2993
99   preConfigure = ''
100     sed -i 's/-faltivec//' numpy/distutils/system_info.py
101     export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
102   '';
104   buildInputs = [
105     blas
106     lapack
107   ];
109   preBuild = ''
110     ln -s ${cfg} site.cfg
111   '';
113   enableParallelBuilding = true;
115   nativeCheckInputs = [
116     hypothesis
117     pytestCheckHook
118     pytest-xdist
119     setuptools
120     typing-extensions
121   ];
123   preCheck = ''
124     pushd $out
125   '';
127   postCheck = ''
128     popd
129   '';
131   # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
132   pytestFlagsArray = [
133     "-m"
134     "not\\ slow" # fast test suite
135   ];
137   disabledTests =
138     [
139       # Tries to import numpy.distutils.msvccompiler, removed in setuptools 74.0
140       "test_api_importable"
141     ]
142     ++ lib.optionals (pythonAtLeast "3.13") [
143       # https://github.com/numpy/numpy/issues/26713
144       "test_iter_refcount"
145     ]
146     ++ lib.optionals stdenv.hostPlatform.isAarch32 [
147       # https://github.com/numpy/numpy/issues/24548
148       "test_impossible_feature_enable" # AssertionError: Failed to generate error
149       "test_features" # AssertionError: Failure Detection
150       "test_new_policy" # AssertionError: assert False
151       "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
152       "test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
153       "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
154       "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
155       "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
156       "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
157       "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
158     ]
159     ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
160       # AssertionError: (np.int64(0), np.longdouble('9.9999999999999994515e-21'), np.longdouble('3.9696755572509052902e+20'), 'arctanh')
161       "test_loss_of_precision"
162     ];
164   passthru = {
165     # just for backwards compatibility
166     blas = blas.provider;
167     blasImplementation = blas.implementation;
168     inherit cfg;
169     coreIncludeDir = "${numpy_2}/${python.sitePackages}/numpy/_core/include";
170     tests = {
171       inherit sage;
172     };
173   };
175   meta = {
176     changelog = "https://github.com/numpy/numpy/releases/tag/v${version}";
177     description = "Scientific tools for Python";
178     homepage = "https://numpy.org/";
179     license = lib.licenses.bsd3;
180     maintainers = with lib.maintainers; [ doronbehar ];
181   };