File: test_2179_parameter_merging_rules.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 (62 lines) | stat: -rw-r--r-- 1,930 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
60
61
62
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

from itertools import permutations

import numpy as np
import pytest  # noqa: F401

import awkward as ak


def test():
    x = ak.with_parameter([1, 2, np.datetime64("now")], "x", 100).layout
    y = ak.contents.NumpyArray(np.array([99, 88, 77], dtype=np.int64))
    result = ak.contents.UnionArray.simplified(
        ak.index.Index8(np.array([0, 0, 1, 1], dtype=np.int8)),
        ak.index.Index64(np.array([0, 1, 0, 1], dtype=np.int64)),
        [x, y],
        parameters={"x": 100},
    )
    assert result.parameters == {"x": 100}

    result2 = ak.contents.UnionArray.simplified(
        ak.index.Index8(np.array([0, 0, 1, 1])),
        ak.index.Index64(np.array([0, 1, 0, 1])),
        [x, y],
        parameters={"x": 200},
    )
    assert result2.parameters == {"x": 200}

    result3 = ak.contents.UnionArray.simplified(
        ak.index.Index8(np.array([0, 0, 1, 1])),
        ak.index.Index64(np.array([0, 1, 0, 1])),
        [x, y],
    )
    assert result3.parameters == {"x": 100}


def test_merge_optional_strings():
    a = ak.Array([{"a": "foo"}, {"a": "bar"}])
    b = ak.Array([{"a": None}])

    result = ak.concatenate([a, b])
    assert ak.almost_equal(result, [{"a": "foo"}, {"a": "bar"}, {"a": None}])


def test_merge_permutations():
    string_optional = ak.Array([{"a": "foo"}, {"a": None}])[0:0]
    kinds = {
        "optional": string_optional,
        "empty": ak.Array([{"a": None}]),
        "string": ak.Array([{"a": "baz"}]),
    }
    for perm in permutations(kinds, 3):
        assert str(ak.concatenate([kinds[k] for k in perm]).type) == "2 * {a: ?string}"


def test_merge_type_commutativity():
    one = ak.concatenate((ak.with_parameter([1], "k", "v"), [None]))
    two = ak.concatenate(([None], ak.with_parameter([1], "k", "v")))
    assert type(one) == type(two)