zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
import re
 
import pytest
 
from pandas.core.indexes.frozen import FrozenList
 
 
@pytest.fixture
def lst():
    return [1, 2, 3, 4, 5]
 
 
@pytest.fixture
def container(lst):
    return FrozenList(lst)
 
 
@pytest.fixture
def unicode_container():
    return FrozenList(["\u05d0", "\u05d1", "c"])
 
 
class TestFrozenList:
    def check_mutable_error(self, *args, **kwargs):
        # Pass whatever function you normally would to pytest.raises
        # (after the Exception kind).
        mutable_regex = re.compile("does not support mutable operations")
        msg = "'(_s)?re.(SRE_)?Pattern' object is not callable"
        with pytest.raises(TypeError, match=msg):
            mutable_regex(*args, **kwargs)
 
    def test_no_mutable_funcs(self, container):
        def setitem():
            container[0] = 5
 
        self.check_mutable_error(setitem)
 
        def setslice():
            container[1:2] = 3
 
        self.check_mutable_error(setslice)
 
        def delitem():
            del container[0]
 
        self.check_mutable_error(delitem)
 
        def delslice():
            del container[0:3]
 
        self.check_mutable_error(delslice)
 
        mutable_methods = ("extend", "pop", "remove", "insert")
 
        for meth in mutable_methods:
            self.check_mutable_error(getattr(container, meth))
 
    def test_slicing_maintains_type(self, container, lst):
        result = container[1:2]
        expected = lst[1:2]
        self.check_result(result, expected)
 
    def check_result(self, result, expected):
        assert isinstance(result, FrozenList)
        assert result == expected
 
    def test_string_methods_dont_fail(self, container):
        repr(container)
        str(container)
        bytes(container)
 
    def test_tricky_container(self, unicode_container):
        repr(unicode_container)
        str(unicode_container)
 
    def test_add(self, container, lst):
        result = container + (1, 2, 3)
        expected = FrozenList(lst + [1, 2, 3])
        self.check_result(result, expected)
 
        result = (1, 2, 3) + container
        expected = FrozenList([1, 2, 3] + lst)
        self.check_result(result, expected)
 
    def test_iadd(self, container, lst):
        q = r = container
 
        q += [5]
        self.check_result(q, lst + [5])
 
        # Other shouldn't be mutated.
        self.check_result(r, lst)
 
    def test_union(self, container, lst):
        result = container.union((1, 2, 3))
        expected = FrozenList(lst + [1, 2, 3])
        self.check_result(result, expected)
 
    def test_difference(self, container):
        result = container.difference([2])
        expected = FrozenList([1, 3, 4, 5])
        self.check_result(result, expected)
 
    def test_difference_dupe(self):
        result = FrozenList([1, 2, 3, 2]).difference([2])
        expected = FrozenList([1, 3])
        self.check_result(result, expected)
 
    def test_tricky_container_to_bytes_raises(self, unicode_container):
        # GH 26447
        msg = "^'str' object cannot be interpreted as an integer$"
        with pytest.raises(TypeError, match=msg):
            bytes(unicode_container)