Skip to content

Commit d89b2a6

Browse files
simonjayhawkinsjreback
authored andcommitted
REF/TST: remove patch from testing.py (#24515)
1 parent f49ed81 commit d89b2a6

File tree

3 files changed

+27
-81
lines changed

3 files changed

+27
-81
lines changed

pandas/tests/io/test_packers.py

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
1-
import pytest
2-
3-
from warnings import catch_warnings
4-
import os
51
import datetime
2+
from distutils.version import LooseVersion
63
import glob
4+
import os
5+
from warnings import catch_warnings
6+
77
import numpy as np
8-
from distutils.version import LooseVersion
8+
import pytest
99

10-
from pandas import compat
11-
from pandas.compat import u, PY3
12-
from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range,
13-
date_range, period_range, Index, Categorical,
14-
Period, Interval)
10+
from pandas._libs.tslib import iNaT
11+
from pandas.compat import PY3, u
1512
from pandas.errors import PerformanceWarning
16-
from pandas.io.packers import to_msgpack, read_msgpack
17-
import pandas.util.testing as tm
18-
from pandas.util.testing import (ensure_clean,
19-
assert_categorical_equal,
20-
assert_frame_equal,
21-
assert_index_equal,
22-
assert_series_equal,
23-
patch)
24-
from pandas.tests.test_panel import assert_panel_equal
2513

2614
import pandas
27-
from pandas import Timestamp, NaT
28-
from pandas._libs.tslib import iNaT
15+
from pandas import (
16+
Categorical, DataFrame, Index, Interval, MultiIndex, NaT, Panel, Period,
17+
Series, Timestamp, bdate_range, compat, date_range, period_range)
18+
from pandas.tests.test_panel import assert_panel_equal
19+
import pandas.util.testing as tm
20+
from pandas.util.testing import (
21+
assert_categorical_equal, assert_frame_equal, assert_index_equal,
22+
assert_series_equal, ensure_clean)
23+
24+
from pandas.io.packers import read_msgpack, to_msgpack
2925

3026
nan = np.nan
3127

@@ -660,7 +656,8 @@ def test_compression_blosc(self):
660656
pytest.skip('no blosc')
661657
self._test_compression('blosc')
662658

663-
def _test_compression_warns_when_decompress_caches(self, compress):
659+
def _test_compression_warns_when_decompress_caches(
660+
self, monkeypatch, compress):
664661
not_garbage = []
665662
control = [] # copied data
666663

@@ -685,9 +682,9 @@ def decompress(ob):
685682
np.dtype('timedelta64[ns]'): np.timedelta64(1, 'ns'),
686683
}
687684

688-
with patch(compress_module, 'decompress', decompress), \
685+
with monkeypatch.context() as m, \
689686
tm.assert_produces_warning(PerformanceWarning) as ws:
690-
687+
m.setattr(compress_module, 'decompress', decompress)
691688
i_rec = self.encode_decode(self.frame, compress=compress)
692689
for k in self.frame.keys():
693690

@@ -712,15 +709,17 @@ def decompress(ob):
712709
# original buffers
713710
assert buf == control_buf
714711

715-
def test_compression_warns_when_decompress_caches_zlib(self):
712+
def test_compression_warns_when_decompress_caches_zlib(self, monkeypatch):
716713
if not _ZLIB_INSTALLED:
717714
pytest.skip('no zlib')
718-
self._test_compression_warns_when_decompress_caches('zlib')
715+
self._test_compression_warns_when_decompress_caches(
716+
monkeypatch, 'zlib')
719717

720-
def test_compression_warns_when_decompress_caches_blosc(self):
718+
def test_compression_warns_when_decompress_caches_blosc(self, monkeypatch):
721719
if not _BLOSC_INSTALLED:
722720
pytest.skip('no blosc')
723-
self._test_compression_warns_when_decompress_caches('blosc')
721+
self._test_compression_warns_when_decompress_caches(
722+
monkeypatch, 'blosc')
724723

725724
def _test_small_strings_no_warn(self, compress):
726725
empty = np.array([], dtype='uint8')

pandas/util/testing.py

-52
Original file line numberDiff line numberDiff line change
@@ -2973,58 +2973,6 @@ def _constructor(self):
29732973
return SubclassedCategorical
29742974

29752975

2976-
@contextmanager
2977-
def patch(ob, attr, value):
2978-
"""Temporarily patch an attribute of an object.
2979-
2980-
Parameters
2981-
----------
2982-
ob : any
2983-
The object to patch. This must support attribute assignment for `attr`.
2984-
attr : str
2985-
The name of the attribute to patch.
2986-
value : any
2987-
The temporary attribute to assign.
2988-
2989-
Examples
2990-
--------
2991-
>>> class C(object):
2992-
... attribute = 'original'
2993-
...
2994-
>>> C.attribute
2995-
'original'
2996-
>>> with patch(C, 'attribute', 'patched'):
2997-
... in_context = C.attribute
2998-
...
2999-
>>> in_context
3000-
'patched'
3001-
>>> C.attribute # the value is reset when the context manager exists
3002-
'original'
3003-
3004-
Correctly replaces attribute when the manager exits with an exception.
3005-
>>> with patch(C, 'attribute', 'patched'):
3006-
... in_context = C.attribute
3007-
... raise ValueError()
3008-
Traceback (most recent call last):
3009-
...
3010-
ValueError
3011-
>>> in_context
3012-
'patched'
3013-
>>> C.attribute
3014-
'original'
3015-
"""
3016-
noattr = object() # mark that the attribute never existed
3017-
old = getattr(ob, attr, noattr)
3018-
setattr(ob, attr, value)
3019-
try:
3020-
yield
3021-
finally:
3022-
if old is noattr:
3023-
delattr(ob, attr)
3024-
else:
3025-
setattr(ob, attr, old)
3026-
3027-
30282976
@contextmanager
30292977
def set_timezone(tz):
30302978
"""Context manager for temporarily setting a timezone.

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ skip=
184184
pandas/tests/io/test_s3.py,
185185
pandas/tests/io/test_html.py,
186186
pandas/tests/io/test_sql.py,
187-
pandas/tests/io/test_packers.py,
188187
pandas/tests/io/test_stata.py,
189188
pandas/tests/io/conftest.py,
190189
pandas/tests/io/test_pickle.py,

0 commit comments

Comments
 (0)