File: test_1559_fix_ufuncs_records_1439.py

package info (click to toggle)
python-awkward 2.6.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 23,088 kB
  • sloc: python: 148,689; cpp: 33,562; sh: 432; makefile: 21; javascript: 8
file content (59 lines) | stat: -rw-r--r-- 1,602 bytes parent folder | download
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
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import numpy as np
import pytest

import awkward as ak

to_list = ak.operations.to_list


def test_ufuncs_on_records_1439_raise_a_warning():
    array = ak.Array([{"x": 1.0, "y": 1.1}, {"x": 2.0, "y": 2.2}])
    with pytest.raises(ValueError):
        np.absolute(array)


def test_ufuncs_on_records_1439_without_warning():
    def overload_abs(self):
        return np.sqrt(self.x**2 + self.y**2)

    behavior = {}
    behavior[np.absolute, "Overload"] = overload_abs

    one = ak.Array(
        [
            [{"x": 4, "y": 3}, {"x": 6, "y": 8}, {"x": 5, "y": 12}],
            [],
            [{"x": 9, "y": 12}, {"x": 15, "y": 20}],
        ],
        with_name="Overload",
        behavior=behavior,
    )
    assert np.absolute(one).tolist() == [[5.0, 10.0, 13.0], [], [15.0, 25.0]]


def test_this_should_raise_a_warning():
    one = ak.Array([{"x": 1}, {"x": 2}, {"x": 3}])
    two = ak.Array([{"x": 1.1}, {"x": 2.2}, {"x": 3.3}])
    with pytest.raises(ValueError):
        one + two


def test_this_should_not():
    def overload_add(left, right):
        return ak.Array({"x": left.x + right.x})

    behavior = {}
    behavior[np.add, "Overload", "Overload"] = overload_add

    one = ak.Array(
        [{"x": 1}, {"x": 2}, {"x": 3}], with_name="Overload", behavior=behavior
    )
    two = ak.Array(
        [{"x": 1.1}, {"x": 2.2}, {"x": 3.3}], with_name="Overload", behavior=behavior
    )

    assert (one + two).to_list() == [{"x": 2.1}, {"x": 4.2}, {"x": 6.3}]