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

Added get_dependency_min_version_spec function #62

Merged
merged 1 commit into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/lightning_utilities/core/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import importlib
import operator
from functools import lru_cache
from importlib import metadata
carmocca marked this conversation as resolved.
Show resolved Hide resolved
from importlib.util import find_spec
from typing import Callable

import pkg_resources
from packaging.requirements import Requirement
from packaging.version import Version


Expand Down Expand Up @@ -107,3 +109,21 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return self.__str__()


def get_dependency_min_version_spec(package_name: str, dependency_name: str) -> str:
"""Returns the minimum version specifier of a dependency of a package.

>>> get_dependency_min_version_spec("pytorch-lightning", "jsonargparse")
'>=4.12.0'
"""
dependencies = metadata.requires(package_name) or []
for dep in dependencies:
dependency = Requirement(dep)
if dependency.name == dependency_name:
spec = [str(s) for s in dependency.specifier if str(s)[0] == ">"]
return spec[0] if spec else ""
raise ValueError(
"This is an internal error. Please file a GitHub issue with the error message. Dependency "
f"{dependency_name!r} not found in package {package_name!r}."
)
22 changes: 21 additions & 1 deletion tests/unittests/core/test_imports.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import operator
import re
from importlib.metadata import PackageNotFoundError

from lightning_utilities.core.imports import compare_version, module_available, RequirementCache
import pytest

from lightning_utilities.core.imports import (
compare_version,
get_dependency_min_version_spec,
module_available,
RequirementCache,
)


def test_module_exists():
Expand Down Expand Up @@ -38,3 +47,14 @@ def test_requirement_cache():
assert RequirementCache(f"pytest>={pytest.__version__}")
assert not RequirementCache(f"pytest<{pytest.__version__}")
assert "pip install -U '-'" in str(RequirementCache("-"))


def test_get_dependency_min_version_spec():
attrs_min_version_spec = get_dependency_min_version_spec("pytest", "attrs")
assert re.match(r"^>=[\d.]+$", attrs_min_version_spec)

with pytest.raises(ValueError, match="'invalid' not found in package 'pytest'"):
get_dependency_min_version_spec("pytest", "invalid")

with pytest.raises(PackageNotFoundError, match="invalid"):
get_dependency_min_version_spec("invalid", "invalid")