Skip to content

Commit be6f4f3

Browse files
committed
Return type in _load_backend, changes for mypy
1 parent 25b1dce commit be6f4f3

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pandas/plotting/_core.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
Sequence,
88
)
99

10-
# TODO: replace with `importlib.metadata` when python_requires >= 3.8.
1110
import pkg_resources
1211

1312
from pandas._config import get_option
@@ -1731,7 +1730,7 @@ def hexbin(self, x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs):
17311730
_backends: dict[str, types.ModuleType] = {}
17321731

17331732

1734-
def _load_backend(backend: str):
1733+
def _load_backend(backend: str) -> types.ModuleType:
17351734
"""
17361735
Load a pandas plotting backend.
17371736
@@ -1746,36 +1745,39 @@ def _load_backend(backend: str):
17461745
types.ModuleType
17471746
The imported backend.
17481747
"""
1749-
module: types.ModuleType | None = None
1750-
17511748
if backend == "matplotlib":
17521749
# Because matplotlib is an optional dependency and first-party backend,
17531750
# we need to attempt an import here to raise an ImportError if needed.
17541751
try:
1755-
module = __import__("pandas.plotting._matplotlib")
1752+
module = importlib.import_module("pandas.plotting._matplotlib")
17561753
except ImportError:
17571754
raise ImportError(
17581755
"matplotlib is required for plotting when the "
17591756
'default backend "matplotlib" is selected.'
17601757
) from None
17611758
return module
17621759

1760+
found_backend = False
1761+
17631762
for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
1764-
if entry_point.name == backend:
1763+
found_backend = entry_point.name == backend
1764+
if found_backend:
17651765
module = entry_point.load()
17661766

1767-
if module is None:
1767+
if not found_backend:
17681768
# Fall back to unregistered, module name approach.
17691769
try:
17701770
module = importlib.import_module(backend)
1771+
found_backend = True
17711772
except ImportError:
17721773
# We re-raise later on.
17731774
pass
17741775

1775-
if hasattr(module, "plot"):
1776-
# Validate that the interface is implemented when the option is set,
1777-
# rather than at plot time.
1778-
return module
1776+
if found_backend:
1777+
if hasattr(module, "plot"):
1778+
# Validate that the interface is implemented when the option is set,
1779+
# rather than at plot time.
1780+
return module
17791781

17801782
raise ValueError(
17811783
f"Could not find plotting backend '{backend}'. Ensure that you've "

0 commit comments

Comments
 (0)