-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
||
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__') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running that locally I get one error that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
There was a problem hiding this comment.
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