Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_utils.py81 linesDownload Raw Back to tests
1from io import StringIO
2
3import pytest
4
5import numpy as np
6import numpy.lib._utils_impl as _utils_impl
7from numpy.testing import assert_raises_regex
8
9
10def test_assert_raises_regex_context_manager():
11    with assert_raises_regex(ValueError, 'no deprecation warning'):
12        raise ValueError('no deprecation warning')
13
14
15def test_info_method_heading():
16    # info(class) should only print "Methods:" heading if methods exist
17
18    class NoPublicMethods:
19        pass
20
21    class WithPublicMethods:
22        def first_method():
23            pass
24
25    def _has_method_heading(cls):
26        out = StringIO()
27        np.info(cls, output=out)
28        return 'Methods:' in out.getvalue()
29
30    assert _has_method_heading(WithPublicMethods)
31    assert not _has_method_heading(NoPublicMethods)
32
33
34def test_drop_metadata():
35    def _compare_dtypes(dt1, dt2):
36        return np.can_cast(dt1, dt2, casting='no')
37
38    # structured dtype
39    dt = np.dtype([('l1', [('l2', np.dtype('S8', metadata={'msg': 'toto'}))])],
40                  metadata={'msg': 'titi'})
41    dt_m = _utils_impl.drop_metadata(dt)
42    assert _compare_dtypes(dt, dt_m) is True
43    assert dt_m.metadata is None
44    assert dt_m['l1'].metadata is None
45    assert dt_m['l1']['l2'].metadata is None
46
47    # alignment
48    dt = np.dtype([('x', '<f8'), ('y', '<i4')],
49                  align=True,
50                  metadata={'msg': 'toto'})
51    dt_m = _utils_impl.drop_metadata(dt)
52    assert _compare_dtypes(dt, dt_m) is True
53    assert dt_m.metadata is None
54
55    # subdtype
56    dt = np.dtype('8f',
57                  metadata={'msg': 'toto'})
58    dt_m = _utils_impl.drop_metadata(dt)
59    assert _compare_dtypes(dt, dt_m) is True
60    assert dt_m.metadata is None
61
62    # scalar
63    dt = np.dtype('uint32',
64                  metadata={'msg': 'toto'})
65    dt_m = _utils_impl.drop_metadata(dt)
66    assert _compare_dtypes(dt, dt_m) is True
67    assert dt_m.metadata is None
68
69
70@pytest.mark.parametrize("dtype",
71        [np.dtype("i,i,i,i")[["f1", "f3"]],
72        np.dtype("f8"),
73        np.dtype("10i")])
74def test_drop_metadata_identity_and_copy(dtype):
75    # If there is no metadata, the identity is preserved:
76    assert _utils_impl.drop_metadata(dtype) is dtype
77
78    # If there is any, it is dropped (subforms are checked above)
79    dtype = np.dtype(dtype, metadata={1: 2})
80    assert _utils_impl.drop_metadata(dtype).metadata is None
81 
codekingpro/portable-devtools · Team Ai