|
1 | 1 | import sys
|
2 | 2 | import types
|
3 | 3 |
|
4 |
| -import pkg_resources |
5 | 4 | import pytest
|
6 | 5 |
|
7 | 6 | import pandas.util._test_decorators as td
|
@@ -45,40 +44,28 @@ def test_backend_can_be_set_in_plot_call(monkeypatch, restore_backend):
|
45 | 44 | assert df.plot(backend="pandas_dummy_backend") == "used_dummy"
|
46 | 45 |
|
47 | 46 |
|
48 |
| -@td.skip_if_no_mpl |
49 |
| -def test_register_entrypoint(restore_backend): |
50 |
| - |
51 |
| - dist = pkg_resources.get_distribution("pandas") |
52 |
| - if dist.module_path not in pandas.__file__: |
53 |
| - # We are running from a non-installed pandas, and this test is invalid |
54 |
| - pytest.skip("Testing a non-installed pandas") |
55 |
| - |
56 |
| - mod = types.ModuleType("my_backend") |
57 |
| - mod.plot = lambda *args, **kwargs: 1 |
| 47 | +def test_register_entrypoint(restore_backend, tmp_path, monkeypatch): |
| 48 | + monkeypatch.syspath_prepend(tmp_path) |
| 49 | + monkeypatch.setitem(sys.modules, "pandas_dummy_backend", dummy_backend) |
58 | 50 |
|
59 |
| - backends = pkg_resources.get_entry_map("pandas") |
60 |
| - my_entrypoint = pkg_resources.EntryPoint( |
61 |
| - "pandas_plotting_backend", mod.__name__, dist=dist |
| 51 | + dist_info = tmp_path / "my_backend-0.0.0.dist-info" |
| 52 | + dist_info.mkdir() |
| 53 | + # entry_point name should not match module name - otherwise pandas will |
| 54 | + # fall back to backend lookup by module name |
| 55 | + (dist_info / "entry_points.txt").write_bytes( |
| 56 | + b"[pandas_plotting_backends]\nmy_ep_backend = pandas_dummy_backend\n" |
62 | 57 | )
|
63 |
| - backends["pandas_plotting_backends"]["my_backend"] = my_entrypoint |
64 |
| - # TODO: the docs recommend importlib.util.module_from_spec. But this works for now. |
65 |
| - sys.modules["my_backend"] = mod |
66 |
| - |
67 |
| - result = pandas.plotting._core._get_plot_backend("my_backend") |
68 |
| - assert result is mod |
69 | 58 |
|
70 |
| - # TODO(GH#27517): https://github.com/pandas-dev/pandas/issues/27517 |
71 |
| - # Remove the td.skip_if_no_mpl |
72 |
| - with pandas.option_context("plotting.backend", "my_backend"): |
73 |
| - result = pandas.plotting._core._get_plot_backend() |
| 59 | + assert pandas.plotting._core._get_plot_backend("my_ep_backend") is dummy_backend |
74 | 60 |
|
75 |
| - assert result is mod |
| 61 | + with pandas.option_context("plotting.backend", "my_ep_backend"): |
| 62 | + assert pandas.plotting._core._get_plot_backend() is dummy_backend |
76 | 63 |
|
77 | 64 |
|
78 |
| -def test_setting_backend_without_plot_raises(): |
| 65 | +def test_setting_backend_without_plot_raises(monkeypatch): |
79 | 66 | # GH-28163
|
80 | 67 | module = types.ModuleType("pandas_plot_backend")
|
81 |
| - sys.modules["pandas_plot_backend"] = module |
| 68 | + monkeypatch.setitem(sys.modules, "pandas_plot_backend", module) |
82 | 69 |
|
83 | 70 | assert pandas.options.plotting.backend == "matplotlib"
|
84 | 71 | with pytest.raises(
|
|
0 commit comments