Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Created March 1, 2023 15:21
Show Gist options
  • Save a-recknagel/d66b0d5e8f2bf1e29f192e2d4cfa9285 to your computer and use it in GitHub Desktop.
Save a-recknagel/d66b0d5e8f2bf1e29f192e2d4cfa9285 to your computer and use it in GitHub Desktop.
warn when entering extra-install-requiring-section in a lib
import warnings
from importlib import import_module
from importlib.metadata import requires
from packaging.requirements import Requirement
MY_LIB = "my_lib"
CURRENT_EXTRA = "my_extra"
for req in map(Requirement, requires(MY_LIB)):
if req.marker is None:
continue
if CURRENT_EXTRA in {e[2].value for e in req.marker._markers if e[0].value == "extra"}:
try:
import_module(dep)
except ImportError:
warnings.warn(
f"The feature you're trying to use requires the extra '{CURRENT_EXTRA}', install it by running 'pip install "
f"{MY_LIB}[{CURRENT_EXTRA}]' or 'poetry add {MY_LIB}[{CURRENT_EXTRA}]'."
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment