Skip to content

Commit

Permalink
TST: Ensure Matplotlib is always cleaned up (#57389)
Browse files Browse the repository at this point in the history
The seaborn test also uses Matplotlib but was not wrapped in the cleanup
fixture, As there are now 3 files that need this fixture, refactor to
reduce code duplication.
  • Loading branch information
QuLogic authored Feb 13, 2024
1 parent baf98b8 commit 1d7aedc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
34 changes: 34 additions & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
timezone,
)
from decimal import Decimal
import gc
import operator
import os
from typing import (
Expand Down Expand Up @@ -1863,6 +1864,39 @@ def ip():
return InteractiveShell(config=c)


@pytest.fixture
def mpl_cleanup():
"""
Ensure Matplotlib is cleaned up around a test.
Before a test is run:
1) Set the backend to "template" to avoid requiring a GUI.
After a test is run:
1) Reset units registry
2) Reset rc_context
3) Close all figures
See matplotlib/testing/decorators.py#L24.
"""
mpl = pytest.importorskip("matplotlib")
mpl_units = pytest.importorskip("matplotlib.units")
plt = pytest.importorskip("matplotlib.pyplot")
orig_units_registry = mpl_units.registry.copy()
try:
with mpl.rc_context():
mpl.use("template")
yield
finally:
mpl_units.registry.clear()
mpl_units.registry.update(orig_units_registry)
plt.close("all")
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close # noqa: E501
gc.collect(1)


@pytest.fixture(
params=[
getattr(pd.offsets, o)
Expand Down
22 changes: 1 addition & 21 deletions pandas/tests/io/formats/style/test_matplotlib.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc

import numpy as np
import pytest

Expand All @@ -16,25 +14,7 @@

from pandas.io.formats.style import Styler


@pytest.fixture(autouse=True)
def mpl_cleanup():
# matplotlib/testing/decorators.py#L24
# 1) Resets units registry
# 2) Resets rc_context
# 3) Closes all figures
mpl = pytest.importorskip("matplotlib")
mpl_units = pytest.importorskip("matplotlib.units")
plt = pytest.importorskip("matplotlib.pyplot")
orig_units_registry = mpl_units.registry.copy()
with mpl.rc_context():
mpl.use("template")
yield
mpl_units.registry.clear()
mpl_units.registry.update(orig_units_registry)
plt.close("all")
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close # noqa: E501
gc.collect(1)
pytestmark = pytest.mark.usefixtures("mpl_cleanup")


@pytest.fixture
Expand Down
21 changes: 2 additions & 19 deletions pandas/tests/plotting/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import gc

import numpy as np
import pytest

Expand All @@ -10,23 +8,8 @@


@pytest.fixture(autouse=True)
def mpl_cleanup():
# matplotlib/testing/decorators.py#L24
# 1) Resets units registry
# 2) Resets rc_context
# 3) Closes all figures
mpl = pytest.importorskip("matplotlib")
mpl_units = pytest.importorskip("matplotlib.units")
plt = pytest.importorskip("matplotlib.pyplot")
orig_units_registry = mpl_units.registry.copy()
with mpl.rc_context():
mpl.use("template")
yield
mpl_units.registry.clear()
mpl_units.registry.update(orig_units_registry)
plt.close("all")
# https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.6.0.html#garbage-collection-is-no-longer-run-on-figure-close # noqa: E501
gc.collect(1)
def autouse_mpl_cleanup(mpl_cleanup):
pass


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_scikit_learn():
clf.predict(digits.data[-1:])


def test_seaborn():
def test_seaborn(mpl_cleanup):
seaborn = pytest.importorskip("seaborn")
tips = DataFrame(
{"day": pd.date_range("2023", freq="D", periods=5), "total_bill": range(5)}
Expand Down

0 comments on commit 1d7aedc

Please sign in to comment.