Skip to content

Commit

Permalink
cleanup pkg_resources references
Browse files Browse the repository at this point in the history
and cleanup tests eg pandas-dev#27517
  • Loading branch information
graingert committed Mar 10, 2022
1 parent 0453fdf commit 2b5b3ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ def _load_backend(backend: str) -> types.ModuleType:
----------
backend : str
The identifier for the backend. Either an entrypoint item registered
with pkg_resources, "matplotlib", or a module name.
with importlib.metadata, "matplotlib", or a module name.
Returns
-------
Expand Down
41 changes: 14 additions & 27 deletions pandas/tests/plotting/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
import types

import pkg_resources
import pytest

import pandas.util._test_decorators as td
Expand Down Expand Up @@ -45,40 +44,28 @@ def test_backend_can_be_set_in_plot_call(monkeypatch, restore_backend):
assert df.plot(backend="pandas_dummy_backend") == "used_dummy"


@td.skip_if_no_mpl
def test_register_entrypoint(restore_backend):

dist = pkg_resources.get_distribution("pandas")
if dist.module_path not in pandas.__file__:
# We are running from a non-installed pandas, and this test is invalid
pytest.skip("Testing a non-installed pandas")

mod = types.ModuleType("my_backend")
mod.plot = lambda *args, **kwargs: 1
def test_register_entrypoint(restore_backend, tmp_path, monkeypatch):
monkeypatch.syspath_prepend(tmp_path)
monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend)

backends = pkg_resources.get_entry_map("pandas")
my_entrypoint = pkg_resources.EntryPoint(
"pandas_plotting_backend", mod.__name__, dist=dist
dist_info = tmp_path / "my_backend-0.0.0.dist-info"
dist_info.mkdir()
# entry_point name should not match module name - otherwise pandas will
# fall back to backend lookup by module name
(dist_info / "entry_points.txt").write_bytes(
b"[pandas_plotting_backend]\nmy_ep_backend = pandas_dummy_backend\n"
)
backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint
# TODO: the docs recommend importlib.util.module_from_spec. But this works for now.
sys.modules["my_backend"] = mod

result = pandas.plotting._core._get_plot_backend("my_backend")
assert result is mod

# TODO(GH#27517): https://github.com/pandas-dev/pandas/issues/27517
# Remove the td.skip_if_no_mpl
with pandas.option_context("plotting.backend", "my_backend"):
result = pandas.plotting._core._get_plot_backend()
assert pandas.plotting._core._get_plot_backend("my_ep_backend") is dummy_backend

assert result is mod
with pandas.option_context("plotting.backend", "my_ep_backend"):
assert pandas.plotting._core._get_plot_backend() is dummy_backend


def test_setting_backend_without_plot_raises():
def test_setting_backend_without_plot_raises(monkeypatch):
# GH-28163
module = types.ModuleType("pandas_plot_backend")
sys.modules["pandas_plot_backend"] = module
monkeypatch.setitem(sys.modules, "pandas_plot_backend", module)

assert pandas.options.plotting.backend == "matplotlib"
with pytest.raises(
Expand Down

0 comments on commit 2b5b3ca

Please sign in to comment.