Skip to content

Commit 6c02c9e

Browse files
WillAydjreback
authored andcommitted
TST: Skip if Decorators for Localpath and Pathlib (#18765)
1 parent 5b9b4a8 commit 6c02c9e

File tree

6 files changed

+43
-30
lines changed

6 files changed

+43
-30
lines changed

pandas/tests/io/sas/test_sas7bdat.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
from pandas.compat import PY2
33
import pandas.util.testing as tm
4+
import pandas.util._test_decorators as td
45
from pandas.errors import EmptyDataError
56
import os
67
import io
@@ -71,8 +72,8 @@ def test_from_iterator(self):
7172
tm.assert_frame_equal(df, df0.iloc[2:5, :])
7273
rdr.close()
7374

75+
@td.skip_if_no('pathlib')
7476
def test_path_pathlib(self):
75-
tm._skip_if_no_pathlib()
7677
from pathlib import Path
7778
for j in 0, 1:
7879
df0 = self.data[j]
@@ -82,8 +83,8 @@ def test_path_pathlib(self):
8283
df = pd.read_sas(fname, encoding='utf-8')
8384
tm.assert_frame_equal(df, df0)
8485

86+
@td.skip_if_no('py.path')
8587
def test_path_localpath(self):
86-
tm._skip_if_no_localpath()
8788
from py.path import local as LocalPath
8889
for j in 0, 1:
8990
df0 = self.data[j]

pandas/tests/io/test_common.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pandas as pd
1010
import pandas.util.testing as tm
11+
import pandas.util._test_decorators as td
1112

1213
from pandas.io import common
1314
from pandas.compat import is_platform_windows, StringIO, FileNotFoundError
@@ -67,17 +68,15 @@ def test_expand_user_normal_path(self):
6768
assert expanded_name == filename
6869
assert os.path.expanduser(filename) == expanded_name
6970

71+
@td.skip_if_no('pathlib')
7072
def test_stringify_path_pathlib(self):
71-
tm._skip_if_no_pathlib()
72-
7373
rel_path = common._stringify_path(Path('.'))
7474
assert rel_path == '.'
7575
redundant_path = common._stringify_path(Path('foo//bar'))
7676
assert redundant_path == os.path.join('foo', 'bar')
7777

78+
@td.skip_if_no('py.path')
7879
def test_stringify_path_localpath(self):
79-
tm._skip_if_no_localpath()
80-
8180
path = os.path.join('foo', 'bar')
8281
abs_path = os.path.abspath(path)
8382
lpath = LocalPath(path)

pandas/tests/io/test_excel.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import pandas as pd
1515
import pandas.util.testing as tm
16+
import pandas.util._test_decorators as td
1617
from pandas import DataFrame, Index, MultiIndex
1718
from pandas.compat import u, range, map, BytesIO, iteritems
1819
from pandas.core.config import set_option, get_option
@@ -650,11 +651,10 @@ def test_read_from_file_url(self):
650651

651652
tm.assert_frame_equal(url_table, local_table)
652653

654+
@td.skip_if_no('pathlib')
653655
def test_read_from_pathlib_path(self):
654656

655657
# GH12655
656-
tm._skip_if_no_pathlib()
657-
658658
from pathlib import Path
659659

660660
str_path = os.path.join(self.dirpath, 'test1' + self.ext)
@@ -665,11 +665,10 @@ def test_read_from_pathlib_path(self):
665665

666666
tm.assert_frame_equal(expected, actual)
667667

668+
@td.skip_if_no('py.path')
668669
def test_read_from_py_localpath(self):
669670

670671
# GH12655
671-
tm._skip_if_no_localpath()
672-
673672
from py.path import local as LocalPath
674673

675674
str_path = os.path.join(self.dirpath, 'test1' + self.ext)

pandas/tests/io/test_pytables.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -5119,11 +5119,10 @@ def test_read_nokey_empty(self):
51195119
store.close()
51205120
pytest.raises(ValueError, read_hdf, path)
51215121

5122+
@td.skip_if_no('pathlib')
51225123
def test_read_from_pathlib_path(self):
51235124

51245125
# GH11773
5125-
tm._skip_if_no_pathlib()
5126-
51275126
from pathlib import Path
51285127

51295128
expected = DataFrame(np.random.rand(4, 5),
@@ -5137,11 +5136,10 @@ def test_read_from_pathlib_path(self):
51375136

51385137
tm.assert_frame_equal(expected, actual)
51395138

5139+
@td.skip_if_no('py.path')
51405140
def test_read_from_py_localpath(self):
51415141

51425142
# GH11773
5143-
tm._skip_if_no_localpath()
5144-
51455143
from py.path import local as LocalPath
51465144

51475145
expected = DataFrame(np.random.rand(4, 5),

pandas/util/_test_decorators.py

+32
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ def _skip_if_not_us_locale():
9494
return True
9595

9696

97+
def skip_if_no(package, min_version=None):
98+
"""
99+
Generic function to help skip test functions when required packages are not
100+
present on the testing system.
101+
102+
Intended for use as a decorator, this function will wrap the decorated
103+
function with a pytest ``skip_if`` mark. During a pytest test suite
104+
execution, that mark will attempt to import the specified ``package`` and
105+
optionally ensure it meets the ``min_version``. If the import and version
106+
check are unsuccessful, then the decorated function will be skipped.
107+
108+
Parameters
109+
----------
110+
package: str
111+
The name of the package required by the decorated function
112+
min_version: str or None, default None
113+
Optional minimum version of the package required by the decorated
114+
function
115+
116+
Returns
117+
-------
118+
decorated_func: function
119+
The decorated function wrapped within a pytest ``skip_if`` mark
120+
"""
121+
def decorated_func(func):
122+
return pytest.mark.skipif(
123+
not safe_import(package, min_version=min_version),
124+
reason="Could not import '{}'".format(package)
125+
)(func)
126+
return decorated_func
127+
128+
97129
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
98130
reason="Missing matplotlib dependency")
99131
skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),

pandas/util/testing.py

-16
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,6 @@ def _skip_if_no_xarray():
359359
pytest.skip("xarray version is too low: {version}".format(version=v))
360360

361361

362-
def _skip_if_no_pathlib():
363-
try:
364-
from pathlib import Path # noqa
365-
except ImportError:
366-
import pytest
367-
pytest.skip("pathlib not available")
368-
369-
370-
def _skip_if_no_localpath():
371-
try:
372-
from py.path import local as LocalPath # noqa
373-
except ImportError:
374-
import pytest
375-
pytest.skip("py.path not installed")
376-
377-
378362
def skip_if_no_ne(engine='numexpr'):
379363
from pandas.core.computation.expressions import (
380364
_USE_NUMEXPR,

0 commit comments

Comments
 (0)