Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
test_protocols.py47 linesDownload Raw Back to tests
1import warnings
2
3import pytest
4
5import numpy as np
6
7
8@pytest.mark.filterwarnings("error")
9def test_getattr_warning():
10    # issue gh-14735: make sure we clear only getattr errors, and let warnings
11    # through
12    class Wrapper:
13        def __init__(self, array):
14            self.array = array
15
16        def __len__(self):
17            return len(self.array)
18
19        def __getitem__(self, item):
20            return type(self)(self.array[item])
21
22        def __getattr__(self, name):
23            if name.startswith("__array_"):
24                warnings.warn("object got converted", UserWarning, stacklevel=1)
25
26            return getattr(self.array, name)
27
28        def __repr__(self):
29            return f"<Wrapper({self.array})>"
30
31    array = Wrapper(np.arange(10))
32    with pytest.raises(UserWarning, match="object got converted"):
33        np.asarray(array)
34
35
36def test_array_called():
37    class Wrapper:
38        val = '0' * 100
39
40        def __array__(self, dtype=None, copy=None):
41            return np.array([self.val], dtype=dtype, copy=copy)
42
43    wrapped = Wrapper()
44    arr = np.array(wrapped, dtype=str)
45    assert arr.dtype == 'U100'
46    assert arr[0] == Wrapper.val
47 
codekingpro/portable-devtools · Team Ai