summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/numpy/2.nix
blob: 778bcdbc6f213990338a4449d4c57381e0ef1ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
{
  lib,
  stdenv,
  fetchFromGitHub,
  fetchpatch2,
  python,
  numpy_2,
  pythonAtLeast,
  pythonOlder,
  buildPythonPackage,
  writeTextFile,

  # build-system
  cython,
  gfortran,
  meson-python,
  mesonEmulatorHook,
  pkg-config,
  xcbuild,

  # native dependencies
  blas,
  coreutils,
  lapack,

  # Reverse dependency
  sage,

  # tests
  hypothesis,
  pytest-xdist,
  pytestCheckHook,
  setuptools,
  typing-extensions,
}:

assert (!blas.isILP64) && (!lapack.isILP64);

let
  cfg = writeTextFile {
    name = "site.cfg";
    text = lib.generators.toINI { } {
      ${blas.implementation} = {
        include_dirs = "${lib.getDev blas}/include:${lib.getDev lapack}/include";
        library_dirs = "${blas}/lib:${lapack}/lib";
        runtime_library_dirs = "${blas}/lib:${lapack}/lib";
        libraries = "lapack,lapacke,blas,cblas";
      };
      lapack = {
        include_dirs = "${lib.getDev lapack}/include";
        library_dirs = "${lapack}/lib";
        runtime_library_dirs = "${lapack}/lib";
      };
      blas = {
        include_dirs = "${lib.getDev blas}/include";
        library_dirs = "${blas}/lib";
        runtime_library_dirs = "${blas}/lib";
      };
    };
  };
in
buildPythonPackage rec {
  pname = "numpy";
  version = "2.3.5";
  pyproject = true;

  disabled = pythonOlder "3.11";

  src = fetchFromGitHub {
    owner = "numpy";
    repo = "numpy";
    tag = "v${version}";
    fetchSubmodules = true;
    hash = "sha256-CMgJmsjPLgMCWN2iJk0OzcKIlnRRcayrTAns51S4B6k=";
  };

  patches =
    lib.optionals python.hasDistutilsCxxPatch [
      # We patch cpython/distutils to fix https://bugs.python.org/issue1222585
      # Patching of numpy.distutils is needed to prevent it from undoing the
      # patch to distutils.
      ./numpy-distutils-C++.patch
    ]
    ++
      lib.optionals
        (pythonAtLeast "3.14" && stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isAarch64)
        [
          # don't assert RecursionError in monster dtype test
          # see https://github.com/numpy/numpy/pull/30375
          (fetchpatch2 {
            url = "https://github.com/numpy/numpy/commit/eeaf04662e07cc8e2041f3e25bbd3698949a0c02.patch?full_index=1";
            excludes = [ ".github/workflows/macos.yml" ];
            hash = "sha256-bLPLExlKnX18MXhbZxzCHniaAE0yTSyK9WuQyFyYHOI=";
          })
        ];

  postPatch = ''
    # remove needless reference to full Python path stored in built wheel
    substituteInPlace numpy/meson.build \
      --replace-fail 'py.full_path()' "'python'"

    # Test_POWER_Features::test_features - FileNotFoundError: [Errno 2] No such file or directory: '/bin/true'
    substituteInPlace numpy/_core/tests/test_cpu_features.py \
      --replace-fail '/bin/true' '${lib.getExe' coreutils "true"}'
  '';

  build-system = [
    cython
    gfortran
    meson-python
    pkg-config
  ]
  ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild.xcrun ]
  ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) [ mesonEmulatorHook ];

  # we default openblas to build with 64 threads
  # if a machine has more than 64 threads, it will segfault
  # see https://github.com/OpenMathLib/OpenBLAS/issues/2993
  preConfigure = ''
    sed -i 's/-faltivec//' numpy/distutils/system_info.py
    export OMP_NUM_THREADS=$((NIX_BUILD_CORES > 64 ? 64 : NIX_BUILD_CORES))
  '';

  buildInputs = [
    blas
    lapack
  ];

  preBuild = ''
    ln -s ${cfg} site.cfg
  '';

  enableParallelBuilding = true;

  nativeCheckInputs = [
    hypothesis
    pytestCheckHook
    pytest-xdist
    setuptools
    typing-extensions
  ];

  preCheck = ''
    pushd $out
    # For numpy-config executable to be available during tests
    export PATH=$PATH:$out/bin
  '';

  postCheck = ''
    popd
  '';

  # https://github.com/numpy/numpy/blob/a277f6210739c11028f281b8495faf7da298dbef/numpy/_pytesttester.py#L180
  disabledTestMarks = [
    "slow" # fast test suite
  ];

  disabledTests = [
    # Tries to import numpy.distutils.msvccompiler, removed in setuptools 74.0
    "test_api_importable"
  ]
  ++ lib.optionals (pythonAtLeast "3.13") [
    # https://github.com/numpy/numpy/issues/26713
    "test_iter_refcount"
  ]
  ++ lib.optionals stdenv.hostPlatform.isAarch32 [
    # https://github.com/numpy/numpy/issues/24548
    "test_impossible_feature_enable" # AssertionError: Failed to generate error
    "test_features" # AssertionError: Failure Detection
    "test_new_policy" # AssertionError: assert False
    "test_identityless_reduction_huge_array" # ValueError: Maximum allowed dimension exceeded
    "test_unary_spurious_fpexception" # AssertionError: Got warnings: [<warnings.WarningMessage object at 0xd1197430>]
    "test_int" # AssertionError: selectedintkind(19): expected 16 but got -1
    "test_real" # AssertionError: selectedrealkind(16): expected 10 but got -1
    "test_quad_precision" # AssertionError: selectedrealkind(32): expected 16 but got -1
    "test_big_arrays" # ValueError: array is too big; `arr.size * arr.dtype.itemsize` is larger tha...
    "test_multinomial_pvals_float32" # Failed: DID NOT RAISE <class 'ValueError'>
  ]
  ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
    # AssertionError: (np.int64(0), np.longdouble('9.9999999999999994515e-21'), np.longdouble('3.9696755572509052902e+20'), 'arctanh')
    "test_loss_of_precision"
  ]
  ++ lib.optionals (stdenv.hostPlatform.isPower64 && stdenv.hostPlatform.isBigEndian) [
    # https://github.com/numpy/numpy/issues/29918
    "test_sq_cases"
  ]
  ++ lib.optionals (stdenv.hostPlatform ? gcc.arch) [
    # remove if https://github.com/numpy/numpy/issues/27460 is resolved
    "test_validate_transcendentals"
  ];

  passthru = {
    # just for backwards compatibility
    blas = blas.provider;
    blasImplementation = blas.implementation;
    inherit cfg;
    coreIncludeDir = "${numpy_2}/${python.sitePackages}/numpy/_core/include";
    tests = {
      inherit sage;
    };
  };

  meta = {
    changelog = "https://github.com/numpy/numpy/releases/tag/${src.tag}";
    description = "Scientific tools for Python";
    homepage = "https://numpy.org/";
    license = lib.licenses.bsd3;
    maintainers = with lib.maintainers; [ doronbehar ];
  };
}