Skip to content

Commit 673436e

Browse files
SaturnFromTitanjreback
authored andcommitted
moved mixins to the location where they are used (#30110)
1 parent 857b3d6 commit 673436e

File tree

2 files changed

+61
-61
lines changed

2 files changed

+61
-61
lines changed

pandas/tests/indexes/test_frozen.py

+61-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,69 @@
1+
import re
2+
13
import pytest
24

35
from pandas.core.indexes.frozen import FrozenList
4-
from pandas.tests.test_base import CheckImmutable, CheckStringMixin
56

67

7-
class TestFrozenList(CheckImmutable, CheckStringMixin):
8+
class CheckImmutableMixin:
9+
mutable_regex = re.compile("does not support mutable operations")
10+
11+
def check_mutable_error(self, *args, **kwargs):
12+
# Pass whatever function you normally would to pytest.raises
13+
# (after the Exception kind).
14+
with pytest.raises(TypeError):
15+
self.mutable_regex(*args, **kwargs)
16+
17+
def test_no_mutable_funcs(self):
18+
def setitem():
19+
self.container[0] = 5
20+
21+
self.check_mutable_error(setitem)
22+
23+
def setslice():
24+
self.container[1:2] = 3
25+
26+
self.check_mutable_error(setslice)
27+
28+
def delitem():
29+
del self.container[0]
30+
31+
self.check_mutable_error(delitem)
32+
33+
def delslice():
34+
del self.container[0:3]
35+
36+
self.check_mutable_error(delslice)
37+
mutable_methods = getattr(self, "mutable_methods", [])
38+
39+
for meth in mutable_methods:
40+
self.check_mutable_error(getattr(self.container, meth))
41+
42+
def test_slicing_maintains_type(self):
43+
result = self.container[1:2]
44+
expected = self.lst[1:2]
45+
self.check_result(result, expected)
46+
47+
def check_result(self, result, expected, klass=None):
48+
klass = klass or self.klass
49+
assert isinstance(result, klass)
50+
assert result == expected
51+
52+
53+
class CheckStringMixin:
54+
def test_string_methods_dont_fail(self):
55+
repr(self.container)
56+
str(self.container)
57+
bytes(self.container)
58+
59+
def test_tricky_container(self):
60+
if not hasattr(self, "unicode_container"):
61+
pytest.skip("Need unicode_container to test with this")
62+
repr(self.unicode_container)
63+
str(self.unicode_container)
64+
65+
66+
class TestFrozenList(CheckImmutableMixin, CheckStringMixin):
867
mutable_methods = ("extend", "pop", "remove", "insert")
968
unicode_container = FrozenList(["\u05d0", "\u05d1", "c"])
1069

pandas/tests/test_base.py

-59
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from datetime import datetime, timedelta
22
from io import StringIO
3-
import re
43
import sys
54

65
import numpy as np
@@ -40,64 +39,6 @@
4039
import pandas.util.testing as tm
4140

4241

43-
class CheckStringMixin:
44-
def test_string_methods_dont_fail(self):
45-
repr(self.container)
46-
str(self.container)
47-
bytes(self.container)
48-
49-
def test_tricky_container(self):
50-
if not hasattr(self, "unicode_container"):
51-
pytest.skip("Need unicode_container to test with this")
52-
repr(self.unicode_container)
53-
str(self.unicode_container)
54-
55-
56-
class CheckImmutable:
57-
mutable_regex = re.compile("does not support mutable operations")
58-
59-
def check_mutable_error(self, *args, **kwargs):
60-
# Pass whatever function you normally would to pytest.raises
61-
# (after the Exception kind).
62-
with pytest.raises(TypeError):
63-
self.mutable_regex(*args, **kwargs)
64-
65-
def test_no_mutable_funcs(self):
66-
def setitem():
67-
self.container[0] = 5
68-
69-
self.check_mutable_error(setitem)
70-
71-
def setslice():
72-
self.container[1:2] = 3
73-
74-
self.check_mutable_error(setslice)
75-
76-
def delitem():
77-
del self.container[0]
78-
79-
self.check_mutable_error(delitem)
80-
81-
def delslice():
82-
del self.container[0:3]
83-
84-
self.check_mutable_error(delslice)
85-
mutable_methods = getattr(self, "mutable_methods", [])
86-
87-
for meth in mutable_methods:
88-
self.check_mutable_error(getattr(self.container, meth))
89-
90-
def test_slicing_maintains_type(self):
91-
result = self.container[1:2]
92-
expected = self.lst[1:2]
93-
self.check_result(result, expected)
94-
95-
def check_result(self, result, expected, klass=None):
96-
klass = klass or self.klass
97-
assert isinstance(result, klass)
98-
assert result == expected
99-
100-
10142
class TestPandasDelegate:
10243
class Delegator:
10344
_properties = ["foo"]

0 commit comments

Comments
 (0)