Skip to content

Commit

Permalink
TEST: Add testing helpers for checking matplotlib version
Browse files Browse the repository at this point in the history
This encapsulates the proper use of packaging.version.Version to do
version comparisons. Use these instead of remaining direct string comparisons.
  • Loading branch information
dopplershift committed Sep 20, 2023
1 parent 90cb5cf commit b227175
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/metpy/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import contextlib
import functools

import matplotlib
import numpy as np
import numpy.testing
from packaging.version import Version
from pint import DimensionalityError
import pytest
import xarray as xr
Expand All @@ -22,6 +24,39 @@
from .units import units


MPL_VERSION = Version(matplotlib.__version__)


def mpl_version_before(ver):
"""Return whether the active matplotlib is before a certain version.
Parameters
----------
ver : str
The version string for a certain release
Returns
-------
bool : whether the current version was released before the passed in one
"""
return MPL_VERSION < Version(ver)


def mpl_version_equal(ver):
"""Return whether the active matplotlib is equal to a certain version.
Parameters
----------
ver : str
The version string for a certain release
Returns
-------
bool : whether the current version is equal to the passed in one
"""
return MPL_VERSION == Version(ver)


def needs_module(module):
"""Decorate a test function or fixture as requiring a module.
Expand Down
9 changes: 4 additions & 5 deletions tests/plots/test_skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import pytest

from metpy.plots import Hodograph, SkewT
from metpy.testing import mpl_version_equal
from metpy.units import units

MPL_VERSION = matplotlib.__version__[:5]


@pytest.mark.mpl_image_compare(remove_text=True, style='default', tolerance=0.069)
def test_skewt_api():
Expand Down Expand Up @@ -86,7 +85,7 @@ def test_skewt_default_aspect_empty():
@pytest.mark.mpl_image_compare(tolerance=0., remove_text=True, style='default')
def test_skewt_mixing_line_args():
"""Test plot_mixing_lines accepting kwargs for mixing ratio and pressure levels."""
# Explicitly pass default values as kwargs the, should recreate NWS SkewT PDF as above
# Explicitly pass default values as kwargs, should recreate NWS SkewT PDF as above
fig = plt.figure(figsize=(12, 9))
skew = SkewT(fig, rotation=43)
mlines = np.array([0.0004, 0.001, 0.002, 0.004, 0.007, 0.01, 0.016, 0.024, 0.032])
Expand Down Expand Up @@ -156,8 +155,8 @@ def test_skewt_units():
skew.ax.axvline(-10, color='orange')

# On Matplotlib <= 3.6, ax[hv]line() doesn't trigger unit labels
assert skew.ax.get_xlabel() == ('degree_Celsius' if MPL_VERSION == '3.7.0' else '')
assert skew.ax.get_ylabel() == ('hectopascal' if MPL_VERSION == '3.7.0' else '')
assert skew.ax.get_xlabel() == ('degree_Celsius' if mpl_version_equal('3.7.0') else '')
assert skew.ax.get_ylabel() == ('hectopascal' if mpl_version_equal('3.7.0') else '')

# Clear them for the image test
skew.ax.set_xlabel('')
Expand Down

0 comments on commit b227175

Please sign in to comment.