-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN/DOC: fix test_register_entrypoint #46302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test was invalid because the underlying code uses importlib.metadata and not pkg_resources - so the however it passed because the entrypoint name matched the module name and so the code fell back to a regular import |
||
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_backends]\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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pandas_plot_backend in |
||
monkeypatch.setitem(sys.modules, "pandas_plot_backend", module) | ||
|
||
assert pandas.options.plotting.backend == "matplotlib" | ||
with pytest.raises( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test was invalid because the underlying code uses
importlib.metadata
and notpkg_resources
- so thispkg_resources.get_entry_map
monkeypatch was unused.It only passed because
_get_plot_backend
falls back toimportlib.import_module