Skip to content

TST: Skipif decorator for matplotlib #18190 #18427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/tests/io/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pandas as pd
from pandas import DataFrame
import pandas.util.testing as tm
import pandas.util._test_decorators as td

jinja2 = pytest.importorskip('jinja2')
from pandas.io.formats.style import Styler, _get_level_lengths # noqa
Expand Down Expand Up @@ -1011,8 +1012,8 @@ def test_hide_columns_mult_levels(self):

class TestStylerMatplotlibDep(object):

@td.skip_if_no_mpl
def test_background_gradient(self):
tm._skip_if_no_mpl()
df = pd.DataFrame([[1, 2], [2, 4]], columns=['A', 'B'])

for c_map in [None, 'YlOrRd']:
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pandas.util.testing as tm
from pandas.util.testing import (ensure_clean,
assert_is_valid_plot_return_object)
import pandas.util._test_decorators as td

import numpy as np
from numpy import random
Expand All @@ -23,8 +24,6 @@
This is a common base class used for various plotting tests
"""

tm._skip_if_no_mpl()


def _skip_if_no_scipy_gaussian_kde():
try:
Expand All @@ -43,6 +42,7 @@ def _ok_for_gaussian_kde(kind):
return plotting._compat._mpl_ge_1_5_0()


@td.skip_if_no_mpl
class TestPlotBase(object):

def setup_method(self, method):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas import Series, DataFrame, MultiIndex
from pandas.compat import range, lzip
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np
from numpy import random
Expand All @@ -19,8 +20,6 @@

""" Test cases for .boxplot method """

tm._skip_if_no_mpl()


def _skip_if_mpl_14_or_dev_boxplot():
# GH 8382
Expand All @@ -31,6 +30,7 @@ def _skip_if_mpl_14_or_dev_boxplot():
pytest.skip("Matplotlib Regression in 1.4 and current dev.")


@td.skip_if_no_mpl
class TestDataFramePlots(TestPlotBase):

@pytest.mark.slow
Expand Down Expand Up @@ -174,6 +174,7 @@ def test_fontsize(self):
xlabelsize=16, ylabelsize=16)


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):

@pytest.mark.slow
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

from pandas.util.testing import assert_series_equal, ensure_clean
import pandas.util.testing as tm
import pandas.util._test_decorators as td

from pandas.tests.plotting.common import (TestPlotBase,
_skip_if_no_scipy_gaussian_kde)

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestTSPlot(TestPlotBase):

def setup_method(self, method):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pandas as pd
import pandas.util.testing as tm
import pandas.util._test_decorators as td
import pytest

from numpy.random import randn
Expand All @@ -18,9 +19,8 @@
pandas.tools.plotting
"""

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestDeprecatedNameSpace(TestPlotBase):

@pytest.mark.slow
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pandas.compat import range, lrange, lmap, lzip, u, zip, PY3
from pandas.io.formats.printing import pprint_thing
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np
from numpy.random import rand, randn
Expand All @@ -24,9 +25,8 @@
_skip_if_no_scipy_gaussian_kde,
_ok_for_gaussian_kde)

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestDataFramePlots(TestPlotBase):

def setup_method(self, method):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

from pandas import Series, DataFrame
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np

from pandas.tests.plotting.common import TestPlotBase

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):

def test_series_groupby_plotting_nominally_works(self):
Expand Down
7 changes: 4 additions & 3 deletions pandas/tests/plotting/test_hist_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pandas import Series, DataFrame
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np
from numpy.random import randn
Expand All @@ -14,9 +15,7 @@
from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works)


tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestSeriesPlots(TestPlotBase):

def setup_method(self, method):
Expand Down Expand Up @@ -141,6 +140,7 @@ def test_plot_fails_when_ax_differs_from_figure(self):
self.ts.hist(ax=ax1, figure=fig2)


@td.skip_if_no_mpl
class TestDataFramePlots(TestPlotBase):

@pytest.mark.slow
Expand Down Expand Up @@ -251,6 +251,7 @@ def test_tight_layout(self):
tm.close()


@td.skip_if_no_mpl
class TestDataFrameGroupByPlots(TestPlotBase):

@pytest.mark.slow
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pandas import DataFrame
from pandas.compat import lmap
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np
from numpy import random
Expand All @@ -15,9 +16,8 @@
import pandas.plotting as plotting
from pandas.tests.plotting.common import TestPlotBase, _check_plot_works

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestSeriesPlots(TestPlotBase):

def setup_method(self, method):
Expand Down Expand Up @@ -49,6 +49,7 @@ def test_bootstrap_plot(self):
_check_plot_works(bootstrap_plot, series=self.ts, size=10)


@td.skip_if_no_mpl
class TestDataFramePlots(TestPlotBase):

def test_scatter_matrix_axis(self):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pandas import Series, DataFrame, date_range
from pandas.compat import range, lrange
import pandas.util.testing as tm
import pandas.util._test_decorators as td

import numpy as np
from numpy.random import randn
Expand All @@ -21,9 +22,8 @@
_skip_if_no_scipy_gaussian_kde,
_ok_for_gaussian_kde)

tm._skip_if_no_mpl()


@td.skip_if_no_mpl
class TestSeriesPlots(TestPlotBase):

def setup_method(self, method):
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pandas as pd
import pandas.tseries.offsets as offsets
import pandas.util.testing as tm
import pandas.util._test_decorators as td
from pandas import (Series, DataFrame, Panel, Index, isna,
notna, Timestamp)

Expand Down Expand Up @@ -234,9 +235,8 @@ def test_groupby_resample_on_api(self):
result = df.groupby('key').resample('D', on='dates').mean()
assert_frame_equal(result, expected)

@td.skip_if_no_mpl
def test_plot_api(self):
tm._skip_if_no_mpl()

# .resample(....).plot(...)
# hitting warnings
# GH 12448
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/util/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
validate_bool_kwarg)

import pandas.util.testing as tm
from pandas.util._test_decorators import safe_import


class TestDecorators(object):
Expand Down Expand Up @@ -482,3 +483,20 @@ def test_make_signature():
assert sig == (['old_arg_name', 'new_arg_name',
'mapping=None', 'stacklevel=2'],
['old_arg_name', 'new_arg_name', 'mapping', 'stacklevel'])


def test_safe_import(monkeypatch):
assert not safe_import("foo")
assert not safe_import("pandas", min_version="99.99.99")

# Create dummy module to be imported
import types
import sys
mod_name = "hello123"
mod = types.ModuleType(mod_name)
mod.__version__ = "1.5"

assert not safe_import(mod_name)
monkeypatch.setitem(sys.modules, mod_name, mod)
assert not safe_import(mod_name, min_version="2.0")
assert safe_import(mod_name, min_version="1.0")
71 changes: 71 additions & 0 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""
This module provides decorator functions which can be applied to test objects
in order to skip those objects when certain conditions occur. A sample use case
is to detect if the platform is missing ``matplotlib``. If so, any test objects
which require ``matplotlib`` and decorated with ``@td.skip_if_no_mpl`` will be
skipped by ``pytest`` during the execution of the test suite.
To illustrate, after importing this module:
import pandas.util._test_decorators as td
The decorators can be applied to classes:
@td.skip_if_some_reason
class Foo():
...
Or individual functions:
@td.skip_if_some_reason
def test_foo():
...
For more information, refer to the ``pytest`` documentation on ``skipif``.
"""

import pytest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some text as to the purpose of this module


def safe_import(mod_name, min_version=None):
"""
Parameters:
-----------
mod_name : str
Name of the module to be imported
min_version : str, default None
Minimum required version of the specified mod_name
Returns:
--------
object
The imported module if successful, or False
"""
try:
mod = __import__(mod_name)
except ImportError:
return False

if not min_version:
return mod
else:
import sys
version = getattr(sys.modules[mod_name], '__version__')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use LooseVersion here (as we use this elsewhere)

if version:
from distutils.version import LooseVersion
if LooseVersion(version) >= LooseVersion(min_version):
return mod

return False


def _skip_if_no_mpl():
mod = safe_import("matplotlib")
if mod:
mod.use("Agg", warn=False)
else:
return True


skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall some issues with custom markers (#16680).

Do you know if this will cause similar issues with the --strict flag? Could you try pytest --collect-only --strict and see if anything errors? We may be OK since it's a skipif marker, which pytest already knows about...

Copy link
Member Author

@WillAyd WillAyd Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When running that locally I get one error that high_memory is not a registered markerbecause pandas/tests/io/parser/c_parser_only.py has @pytest.mark.high_memory on line 21, but the registered marker is just @pytest.mark.highmemory (no underscore). I believe that to be unrelated to the change here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OK. I'll make a PR for the high_memory issue. Thanks!

reason="Missing matplotlib dependency")
7 changes: 0 additions & 7 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,6 @@ def _skip_if_32bit():
pytest.skip("skipping for 32 bit")


def _skip_if_no_mpl():
import pytest

mpl = pytest.importorskip("matplotlib")
mpl.use("Agg", warn=False)


def _skip_if_mpl_1_5():
import matplotlib as mpl

Expand Down