Skip to content

Commit

Permalink
Return type in _load_backend, changes for mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
TLouf committed Jun 1, 2021
1 parent 25b1dce commit be6f4f3
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Sequence,
)

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

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


def _load_backend(backend: str):
def _load_backend(backend: str) -> types.ModuleType:
"""
Load a pandas plotting backend.
Expand All @@ -1746,36 +1745,39 @@ def _load_backend(backend: str):
types.ModuleType
The imported backend.
"""
module: types.ModuleType | None = None

if backend == "matplotlib":
# Because matplotlib is an optional dependency and first-party backend,
# we need to attempt an import here to raise an ImportError if needed.
try:
module = __import__("pandas.plotting._matplotlib")
module = importlib.import_module("pandas.plotting._matplotlib")
except ImportError:
raise ImportError(
"matplotlib is required for plotting when the "
'default backend "matplotlib" is selected.'
) from None
return module

found_backend = False

for entry_point in pkg_resources.iter_entry_points("pandas_plotting_backends"):
if entry_point.name == backend:
found_backend = entry_point.name == backend
if found_backend:
module = entry_point.load()

if module is None:
if not found_backend:
# Fall back to unregistered, module name approach.
try:
module = importlib.import_module(backend)
found_backend = True
except ImportError:
# We re-raise later on.
pass

if hasattr(module, "plot"):
# Validate that the interface is implemented when the option is set,
# rather than at plot time.
return module
if found_backend:
if hasattr(module, "plot"):
# Validate that the interface is implemented when the option is set,
# rather than at plot time.
return module

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

0 comments on commit be6f4f3

Please sign in to comment.