Skip to content

Commit

Permalink
Refactor optional dependency constants
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybradley committed Aug 8, 2024
1 parent 8304f91 commit 8d1a4e4
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions regions/_utils/optional_deps.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Checks for optional dependencies using lazy import from `PEP 562
<https://www.python.org/dev/peps/pep-0562/>`_.
"""

try:
import matplotlib
HAS_MATPLOTLIB = True
MPL_VERSION = getattr(matplotlib, '__version__', None)
if MPL_VERSION is None:
MPL_VERSION = matplotlib._version.version
MPL_VERSION = MPL_VERSION.split('.')
MPL_VERSION = 10 * int(MPL_VERSION[0]) + int(MPL_VERSION[1])
except ImportError:
HAS_MATPLOTLIB = False
MPL_VERSION = 0
import importlib

# This list is a duplicate of the dependencies in pyproject.toml "all".
# Note that in some cases the package names are different from the
# pip-install name (e.g.k scikit-image -> skimage).
optional_deps = ['matplotlib', 'shapely']
deps = {key.upper(): key for key in optional_deps}
__all__ = [f'HAS_{pkg}' for pkg in deps]


def __getattr__(name):
if name in __all__:
try:
importlib.import_module(deps[name[4:]])
except ImportError:
return False
return True

raise AttributeError(f'Module {__name__!r} has no attribute {name!r}.')

0 comments on commit 8d1a4e4

Please sign in to comment.