Skip to content

Commit 1f7d650

Browse files
committed
use fixtures
1 parent 2885c3e commit 1f7d650

File tree

6 files changed

+49
-32
lines changed

6 files changed

+49
-32
lines changed

pandas/conftest.py

+36
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import abc
12
from datetime import date, time, timedelta, timezone
23
from decimal import Decimal
34
import operator
@@ -894,3 +895,38 @@ def index_or_series(request):
894895
See GH#29725
895896
"""
896897
return request.param
898+
899+
900+
@pytest.fixture
901+
def dict_subclass():
902+
"""
903+
Fixture for a dictionary subclass.
904+
"""
905+
906+
class TestSubDict(dict):
907+
def __init__(self, *args, **kwargs):
908+
dict.__init__(self, *args, **kwargs)
909+
910+
return TestSubDict
911+
912+
913+
@pytest.fixture
914+
def non_mapping_dict_subclass():
915+
"""
916+
Fixture for a non-mapping dictionary subclass.
917+
"""
918+
919+
class TestNonDictMapping(abc.Mapping):
920+
def __init__(self, underlying_dict):
921+
self._data = underlying_dict
922+
923+
def __getitem__(self, key):
924+
return self._data.__getitem__(key)
925+
926+
def __iter__(self):
927+
return self._data.__iter__()
928+
929+
def __len__(self):
930+
return self._data.__len__()
931+
932+
return TestNonDictMapping

pandas/tests/frame/test_constructors.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -511,17 +511,17 @@ def test_constructor_with_embedded_frames(self):
511511
result = df2.loc[1, 0]
512512
tm.assert_frame_equal(result, df1 + 10)
513513

514-
def test_constructor_subclass_dict(self, float_frame):
514+
def test_constructor_subclass_dict(self, float_frame, dict_subclass):
515515
# Test for passing dict subclass to constructor
516516
data = {
517-
"col1": tm.TestSubDict((x, 10.0 * x) for x in range(10)),
518-
"col2": tm.TestSubDict((x, 20.0 * x) for x in range(10)),
517+
"col1": dict_subclass((x, 10.0 * x) for x in range(10)),
518+
"col2": dict_subclass((x, 20.0 * x) for x in range(10)),
519519
}
520520
df = DataFrame(data)
521521
refdf = DataFrame({col: dict(val.items()) for col, val in data.items()})
522522
tm.assert_frame_equal(refdf, df)
523523

524-
data = tm.TestSubDict(data.items())
524+
data = dict_subclass(data.items())
525525
df = DataFrame(data)
526526
tm.assert_frame_equal(refdf, df)
527527

pandas/tests/series/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def test_constructor_dict(self):
126126
expected = Series([1, 2, np.nan, 0], index=["b", "c", "d", "a"])
127127
tm.assert_series_equal(result, expected)
128128

129-
def test_constructor_subclass_dict(self):
130-
data = tm.TestSubDict((x, 10.0 * x) for x in range(10))
129+
def test_constructor_subclass_dict(self, dict_subclass):
130+
data = dict_subclass((x, 10.0 * x) for x in range(10))
131131
series = Series(data)
132132
expected = Series(dict(data.items()))
133133
tm.assert_series_equal(series, expected)

pandas/tests/series/test_apply.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -627,19 +627,19 @@ class DictWithoutMissing(dict):
627627
expected = Series([np.nan, np.nan, "three"])
628628
tm.assert_series_equal(result, expected)
629629

630-
def test_map_abc_mapping(self):
630+
def test_map_abc_mapping(self, non_mapping_dict_subclass):
631631
# https://github.com/pandas-dev/pandas/issues/29733
632632
# Check collections.abc.Mapping support as mapper for Series.map
633633
s = Series([1, 2, 3])
634-
not_a_dictionary = tm.TestNonDictMapping({3: "three"})
634+
not_a_dictionary = non_mapping_dict_subclass({3: "three"})
635635
result = s.map(not_a_dictionary)
636636
expected = Series([np.nan, np.nan, "three"])
637637
tm.assert_series_equal(result, expected)
638638

639-
def test_map_abc_mapping_with_missing(self):
639+
def test_map_abc_mapping_with_missing(self, non_mapping_dict_subclass):
640640
# https://github.com/pandas-dev/pandas/issues/29733
641641
# Check collections.abc.Mapping support as mapper for Series.map
642-
class NonDictMappingWithMissing(tm.TestNonDictMapping):
642+
class NonDictMappingWithMissing(non_mapping_dict_subclass):
643643
def __missing__(self, key):
644644
return "missing"
645645

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,9 @@ def create_data(constructor):
10891089
tm.assert_series_equal(result_datetime, expected)
10901090
tm.assert_series_equal(result_Timestamp, expected)
10911091

1092-
def test_constructor_mapping(self):
1092+
def test_constructor_mapping(self, non_mapping_dict_subclass):
10931093
# GH 29788
1094-
ndm = tm.TestNonDictMapping({3: "three"})
1094+
ndm = non_mapping_dict_subclass({3: "three"})
10951095
result = Series(ndm)
10961096
expected = Series(["three"], index=[3])
10971097

pandas/util/testing.py

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import bz2
2-
from collections import Counter, abc
2+
from collections import Counter
33
from contextlib import contextmanager
44
from datetime import datetime
55
from functools import wraps
@@ -2123,25 +2123,6 @@ def makeMissingDataframe(density=0.9, random_state=None):
21232123
return df
21242124

21252125

2126-
class TestSubDict(dict):
2127-
def __init__(self, *args, **kwargs):
2128-
dict.__init__(self, *args, **kwargs)
2129-
2130-
2131-
class TestNonDictMapping(abc.Mapping):
2132-
def __init__(self, underlying_dict):
2133-
self._data = underlying_dict
2134-
2135-
def __getitem__(self, key):
2136-
return self._data.__getitem__(key)
2137-
2138-
def __iter__(self):
2139-
return self._data.__iter__()
2140-
2141-
def __len__(self):
2142-
return self._data.__len__()
2143-
2144-
21452126
def optional_args(decorator):
21462127
"""allows a decorator to take optional positional and keyword arguments.
21472128
Assumes that taking a single, callable, positional argument means that

0 commit comments

Comments
 (0)