Skip to content

Commit e2fe723

Browse files
committed
Docs
1 parent 2c6f61a commit e2fe723

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/lighteval/utils/imports.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,17 @@ class Extra(enum.Enum):
3434

3535
@lru_cache()
3636
def is_package_available(package: str | Requirement | Extra):
37+
"""
38+
Check if a package is installed in the environment. Returns True if that's the case, False otherwise.
39+
"""
3740
deps, deps_by_extra = required_dependencies()
3841

42+
# If the package is a string, it will get the potential required version from the pyproject.toml
3943
if isinstance(package, str):
4044
package = deps[package]
4145

46+
# If the specified package is an "Extra", we will iterate through each required dependency of that extra
47+
# and their version and check their existence.
4248
if isinstance(package, Extra):
4349
dependencies = deps_by_extra[package.value]
4450
return all(is_package_available(_package) for _package in dependencies)
@@ -57,6 +63,9 @@ def is_package_available(package: str | Requirement | Extra):
5763

5864
@lru_cache()
5965
def required_dependencies() -> Tuple[Dict[str, Requirement], Dict[str, List[Requirement]]]:
66+
"""
67+
Parse the pyproject.toml file and return a dictionary mapping package names to requirements.
68+
"""
6069
md = metadata("lighteval")
6170
requires_dist = md.get_all("Requires-Dist") or []
6271
deps_by_extra = defaultdict(list)
@@ -101,6 +110,10 @@ def raise_if_package_not_available(package: Requirement | Extra, *, language: st
101110

102111

103112
def not_installed_error_message(package: Requirement) -> str:
113+
"""
114+
Custom error messages if need be.
115+
"""
116+
104117
if package == Extra.MULTILINGUAL.value:
105118
return "You are trying to run an evaluation requiring multilingual capabilities. Please install the required extra: `pip install lighteval[multilingual]`"
106119
elif package == Extra.EXTENDED.value:

tests/test_dependencies.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def test_langdetect_required_for_ifeval():
7171

7272
@pretend_missing("spacy", "stanza")
7373
def test_multilingual_required_for_xnli():
74+
"""
75+
This checks that the Extra.MULTILINGUAL correctly raises if there are missing dependencies.
76+
"""
7477
from lighteval.main_accelerate import accelerate
7578

7679
with pytest.raises(

0 commit comments

Comments
 (0)