Skip to content
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

[App] Add utility to get install command for package extras #15809

Merged
merged 11 commits into from
Nov 24, 2022
Merged
16 changes: 16 additions & 0 deletions src/lightning_app/utilities/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@
from typing import List, Union

from lightning_utilities.core.imports import module_available
from packaging.requirements import Marker, Requirement

try:
from importlib import metadata
except ImportError:
# Python < 3.8
import importlib_metadata as metadata # type: ignore


def _get_extras(extras: str) -> List[str]:
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
"""Get the list of installable packages in the given extras."""
from lightning_app import __package_name__

requirements = {r: Requirement(r) for r in metadata.requires(__package_name__)}
carmocca marked this conversation as resolved.
Show resolved Hide resolved
marker = Marker(f'extra == "{extras}"')
return [r.split(";")[0].strip() for r, req in requirements.items() if str(req.marker) == str(marker)]
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved


def requires(module_paths: Union[str, List]):
Expand Down
9 changes: 8 additions & 1 deletion tests/tests_app/utilities/test_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

import pytest

from lightning_app.utilities.imports import requires
from lightning_app.utilities.imports import _get_extras, requires


@mock.patch("lightning_app.utilities.imports.metadata.requires")
ethanwharris marked this conversation as resolved.
Show resolved Hide resolved
def test_get_extras(mock_requires):
mock_requires.return_value = ["docker (>=5.0.0) ; extra == 'test'"]

assert _get_extras("test") == ["docker (>=5.0.0)"]


@mock.patch.dict(os.environ, {"LIGHTING_TESTING": "0"})
Expand Down