diff --git a/lib/matplotlib/tests/test_backend_pdf.py b/lib/matplotlib/tests/test_backend_pdf.py index d6989066b264..11b4936f7f86 100644 --- a/lib/matplotlib/tests/test_backend_pdf.py +++ b/lib/matplotlib/tests/test_backend_pdf.py @@ -3,7 +3,6 @@ from pathlib import Path import sys import tempfile -import warnings import numpy as np import pytest @@ -14,11 +13,9 @@ from matplotlib.testing.decorators import image_comparison -with warnings.catch_warnings(): - warnings.simplefilter('ignore') - needs_usetex = pytest.mark.skipif( - not checkdep_usetex(True), - reason="This test needs a TeX installation") +needs_usetex = pytest.mark.skipif( + not checkdep_usetex(True), + reason="This test needs a TeX installation") @image_comparison(['pdf_use14corefonts.pdf']) diff --git a/lib/matplotlib/tests/test_backend_ps.py b/lib/matplotlib/tests/test_backend_ps.py index 1ce38531af73..5d8d4656b13c 100644 --- a/lib/matplotlib/tests/test_backend_ps.py +++ b/lib/matplotlib/tests/test_backend_ps.py @@ -3,7 +3,6 @@ from pathlib import Path import re import tempfile -import warnings import pytest @@ -13,14 +12,12 @@ from matplotlib.testing.decorators import image_comparison -with warnings.catch_warnings(): - warnings.simplefilter('ignore') - needs_ghostscript = pytest.mark.skipif( - "eps" not in mpl.testing.compare.converter, - reason="This test needs a ghostscript installation") - needs_usetex = pytest.mark.skipif( - not mpl.checkdep_usetex(True), - reason="This test needs a TeX installation") +needs_ghostscript = pytest.mark.skipif( + "eps" not in mpl.testing.compare.converter, + reason="This test needs a ghostscript installation") +needs_usetex = pytest.mark.skipif( + not mpl.checkdep_usetex(True), + reason="This test needs a TeX installation") # This tests tends to hit a TeX cache lock on AppVeyor. diff --git a/lib/matplotlib/tests/test_backend_svg.py b/lib/matplotlib/tests/test_backend_svg.py index 642975b68177..5c27149b725d 100644 --- a/lib/matplotlib/tests/test_backend_svg.py +++ b/lib/matplotlib/tests/test_backend_svg.py @@ -2,7 +2,6 @@ from io import BytesIO import re import tempfile -import warnings import xml.parsers.expat import pytest @@ -14,11 +13,9 @@ from matplotlib.testing.decorators import image_comparison -with warnings.catch_warnings(): - warnings.simplefilter('ignore') - needs_usetex = pytest.mark.skipif( - not mpl.checkdep_usetex(True), - reason="This test needs a TeX installation") +needs_usetex = pytest.mark.skipif( + not mpl.checkdep_usetex(True), + reason="This test needs a TeX installation") def test_visibility(): diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 3c46550c9176..82aec8495845 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -434,7 +434,7 @@ def test_step_fails(args): def test_grouper(): - class dummy(): + class dummy: pass a, b, c, d, e = objs = [dummy() for j in range(5)] g = cbook.Grouper() @@ -454,7 +454,7 @@ class dummy(): def test_grouper_private(): - class dummy(): + class dummy: pass objs = [dummy() for j in range(5)] g = cbook.Grouper() @@ -484,7 +484,7 @@ def test_flatiter(): def test_reshape2d(): - class dummy(): + class dummy: pass xnew = cbook._reshape_2D([], 'x') diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index f10a97844e6c..fb2d62e075a3 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -12,11 +12,9 @@ from matplotlib.testing.decorators import image_comparison -with warnings.catch_warnings(): - warnings.simplefilter('ignore') - needs_usetex = pytest.mark.skipif( - not matplotlib.checkdep_usetex(True), - reason="This test needs a TeX installation") +needs_usetex = pytest.mark.skipif( + not matplotlib.checkdep_usetex(True), + reason="This test needs a TeX installation") @image_comparison(['font_styles']) diff --git a/lib/matplotlib/tests/test_ticker.py b/lib/matplotlib/tests/test_ticker.py index 05c553ae2b07..2dfe2329a139 100644 --- a/lib/matplotlib/tests/test_ticker.py +++ b/lib/matplotlib/tests/test_ticker.py @@ -1,4 +1,7 @@ -import warnings +try: + from contextlib import nullcontext +except ImportError: + from contextlib import ExitStack as nullcontext # Py 3.6. import re import numpy as np @@ -242,9 +245,8 @@ def test_set_params(self): class _LogitHelper: @staticmethod def isclose(x, y): - if x >= 1 or x <= 0 or y >= 1 or y <= 0: - return False - return np.isclose(-np.log(1/x-1), -np.log(1/y-1)) + return (np.isclose(-np.log(1/x-1), -np.log(1/y-1)) + if 0 < x < 1 and 0 < y < 1 else False) @staticmethod def assert_almost_equal(x, y): @@ -460,19 +462,15 @@ def test_offset_value(self, left, right, offset): fig, ax = plt.subplots() formatter = ax.get_xaxis().get_major_formatter() - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', 'Attempting to set identical', - UserWarning) + with (pytest.warns(UserWarning, match='Attempting to set identical') + if left == right else nullcontext()): ax.set_xlim(left, right) - assert len(w) == (1 if left == right else 0) ax.get_xaxis()._update_ticks() assert formatter.offset == offset - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', 'Attempting to set identical', - UserWarning) + with (pytest.warns(UserWarning, match='Attempting to set identical') + if left == right else nullcontext()): ax.set_xlim(right, left) - assert len(w) == (1 if left == right else 0) ax.get_xaxis()._update_ticks() assert formatter.offset == offset @@ -484,8 +482,7 @@ def test_use_offset(self, use_offset): @pytest.mark.parametrize( 'sci_type, scilimits, lim, orderOfMag, fewticks', scilimits_data) - def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, - fewticks): + def test_scilimits(self, sci_type, scilimits, lim, orderOfMag, fewticks): tmp_form = mticker.ScalarFormatter() tmp_form.set_scientific(sci_type) tmp_form.set_powerlimits(scilimits) @@ -542,7 +539,7 @@ def test_blank(self): assert formatter(10**0.1) == '' -class TestLogFormatterMathtext(): +class TestLogFormatterMathtext: fmt = mticker.LogFormatterMathtext() test_data = [ (0, 1, '$\\mathdefault{10^{0}}$'), diff --git a/lib/matplotlib/tests/test_transforms.py b/lib/matplotlib/tests/test_transforms.py index b0cb3b653c7b..f35467ee7206 100644 --- a/lib/matplotlib/tests/test_transforms.py +++ b/lib/matplotlib/tests/test_transforms.py @@ -228,7 +228,7 @@ def transform_path_non_affine(self, path): return self.real_trans.transform_path(path) -class TestBasicTransform(): +class TestBasicTransform: def setup_method(self): self.ta1 = mtransforms.Affine2D(shorthand_name='ta1').rotate(np.pi / 2) diff --git a/lib/matplotlib/tight_layout.py b/lib/matplotlib/tight_layout.py index 9cddeed3d5c5..f613450a93a7 100644 --- a/lib/matplotlib/tight_layout.py +++ b/lib/matplotlib/tight_layout.py @@ -220,9 +220,8 @@ def get_renderer(fig): if canvas and hasattr(canvas, "get_renderer"): renderer = canvas.get_renderer() - else: - # not sure if this can happen - cbook._warn_external("tight_layout : falling back to Agg renderer") + else: # Some noninteractive backends have no renderer until draw time. + cbook._warn_external("tight_layout: falling back to Agg renderer") from matplotlib.backends.backend_agg import FigureCanvasAgg canvas = FigureCanvasAgg(fig) renderer = canvas.get_renderer() diff --git a/lib/matplotlib/tri/triinterpolate.py b/lib/matplotlib/tri/triinterpolate.py index e23c23ce35c4..9188d6d2643b 100644 --- a/lib/matplotlib/tri/triinterpolate.py +++ b/lib/matplotlib/tri/triinterpolate.py @@ -588,7 +588,7 @@ def _compute_tri_eccentricities(tris_pts): # FEM element used for interpolation and for solving minimisation # problem (Reduced HCT element) -class _ReducedHCT_Element(): +class _ReducedHCT_Element: """ Implementation of reduced HCT triangular element with explicit shape functions. @@ -1006,7 +1006,7 @@ def get_Kff_and_Ff(self, J, ecc, triangles, Uc): # _DOF_estimator_min_E # Private classes used to compute the degree of freedom of each triangular # element for the TriCubicInterpolator. -class _DOF_estimator(): +class _DOF_estimator: """ Abstract base class for classes used to perform estimation of a function first derivatives, and deduce the dofs for a CubicTriInterpolator using a