Skip to content

Commit

Permalink
REF/TST: remove patch from testing.py (#24515)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins authored and jreback committed Dec 31, 2018
1 parent f49ed81 commit d89b2a6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 81 deletions.
55 changes: 27 additions & 28 deletions pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import pytest

from warnings import catch_warnings
import os
import datetime
from distutils.version import LooseVersion
import glob
import os
from warnings import catch_warnings

import numpy as np
from distutils.version import LooseVersion
import pytest

from pandas import compat
from pandas.compat import u, PY3
from pandas import (Series, DataFrame, Panel, MultiIndex, bdate_range,
date_range, period_range, Index, Categorical,
Period, Interval)
from pandas._libs.tslib import iNaT
from pandas.compat import PY3, u
from pandas.errors import PerformanceWarning
from pandas.io.packers import to_msgpack, read_msgpack
import pandas.util.testing as tm
from pandas.util.testing import (ensure_clean,
assert_categorical_equal,
assert_frame_equal,
assert_index_equal,
assert_series_equal,
patch)
from pandas.tests.test_panel import assert_panel_equal

import pandas
from pandas import Timestamp, NaT
from pandas._libs.tslib import iNaT
from pandas import (
Categorical, DataFrame, Index, Interval, MultiIndex, NaT, Panel, Period,
Series, Timestamp, bdate_range, compat, date_range, period_range)
from pandas.tests.test_panel import assert_panel_equal
import pandas.util.testing as tm
from pandas.util.testing import (
assert_categorical_equal, assert_frame_equal, assert_index_equal,
assert_series_equal, ensure_clean)

from pandas.io.packers import read_msgpack, to_msgpack

nan = np.nan

Expand Down Expand Up @@ -660,7 +656,8 @@ def test_compression_blosc(self):
pytest.skip('no blosc')
self._test_compression('blosc')

def _test_compression_warns_when_decompress_caches(self, compress):
def _test_compression_warns_when_decompress_caches(
self, monkeypatch, compress):
not_garbage = []
control = [] # copied data

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

with patch(compress_module, 'decompress', decompress), \
with monkeypatch.context() as m, \
tm.assert_produces_warning(PerformanceWarning) as ws:

m.setattr(compress_module, 'decompress', decompress)
i_rec = self.encode_decode(self.frame, compress=compress)
for k in self.frame.keys():

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

def test_compression_warns_when_decompress_caches_zlib(self):
def test_compression_warns_when_decompress_caches_zlib(self, monkeypatch):
if not _ZLIB_INSTALLED:
pytest.skip('no zlib')
self._test_compression_warns_when_decompress_caches('zlib')
self._test_compression_warns_when_decompress_caches(
monkeypatch, 'zlib')

def test_compression_warns_when_decompress_caches_blosc(self):
def test_compression_warns_when_decompress_caches_blosc(self, monkeypatch):
if not _BLOSC_INSTALLED:
pytest.skip('no blosc')
self._test_compression_warns_when_decompress_caches('blosc')
self._test_compression_warns_when_decompress_caches(
monkeypatch, 'blosc')

def _test_small_strings_no_warn(self, compress):
empty = np.array([], dtype='uint8')
Expand Down
52 changes: 0 additions & 52 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2973,58 +2973,6 @@ def _constructor(self):
return SubclassedCategorical


@contextmanager
def patch(ob, attr, value):
"""Temporarily patch an attribute of an object.
Parameters
----------
ob : any
The object to patch. This must support attribute assignment for `attr`.
attr : str
The name of the attribute to patch.
value : any
The temporary attribute to assign.
Examples
--------
>>> class C(object):
... attribute = 'original'
...
>>> C.attribute
'original'
>>> with patch(C, 'attribute', 'patched'):
... in_context = C.attribute
...
>>> in_context
'patched'
>>> C.attribute # the value is reset when the context manager exists
'original'
Correctly replaces attribute when the manager exits with an exception.
>>> with patch(C, 'attribute', 'patched'):
... in_context = C.attribute
... raise ValueError()
Traceback (most recent call last):
...
ValueError
>>> in_context
'patched'
>>> C.attribute
'original'
"""
noattr = object() # mark that the attribute never existed
old = getattr(ob, attr, noattr)
setattr(ob, attr, value)
try:
yield
finally:
if old is noattr:
delattr(ob, attr)
else:
setattr(ob, attr, old)


@contextmanager
def set_timezone(tz):
"""Context manager for temporarily setting a timezone.
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ skip=
pandas/tests/io/test_s3.py,
pandas/tests/io/test_html.py,
pandas/tests/io/test_sql.py,
pandas/tests/io/test_packers.py,
pandas/tests/io/test_stata.py,
pandas/tests/io/conftest.py,
pandas/tests/io/test_pickle.py,
Expand Down

0 comments on commit d89b2a6

Please sign in to comment.