2323# tests/utils/pretend_missing.py
2424import functools
2525import importlib
26+ import sys
27+ from unittest .mock import patch
2628
2729import pytest
28- from packaging .requirements import Requirement
2930
30- import lighteval .utils .imports as imports
31- from lighteval .utils .imports import DummyObject , Extra
31+
32+ def purge_lighteval ():
33+ for name in list (sys .modules ):
34+ if name == "lighteval" or name .startswith ("lighteval." ):
35+ sys .modules .pop (name , None )
3236
3337
3438def pretend_missing (* names ):
3539 """
36- Decorator: pretend that certain packages are missing
37- by patching mypkg.utils.is_package_available .
40+ Decorator for individual tests: make is_package_available(...) return False
41+ for given names. After the test, restore the initial dependencies .
3842 """
3943
4044 def decorator (test_func ):
4145 @functools .wraps (test_func )
4246 def wrapper (* args , ** kwargs ):
43- from unittest .mock import patch
47+ # Ensure import-time guards run under the fake
48+ purge_lighteval ()
4449
4550 def fake (requirement ):
46- name = requirement
47- if isinstance (requirement , Requirement ):
48- name = requirement .name
49- elif isinstance (requirement , Extra ):
50- name = requirement .value
51+ name = getattr (requirement , "name" , getattr (requirement , "value" , requirement ))
5152
53+ # Probably a cleaner option here; returns False
5254 return False if name in names else (importlib .util .find_spec (name ) is not None )
5355
54- with patch .object (imports , "is_package_available" , side_effect = fake ):
55- # In case lighteval caches results at import time, reload here
56- import lighteval
56+ result = None
57+ try :
58+ # Patch dependencies to remove the one we want to remove
59+ with patch ("lighteval.utils.imports.is_package_available" , side_effect = fake ):
60+ importlib .invalidate_caches ()
61+ import lighteval # noqa
62+
63+ importlib .reload (lighteval )
64+ result = test_func (* args , ** kwargs )
65+ finally :
66+ # After the decorator, restore the initial dependencies
67+ importlib .invalidate_caches ()
68+ purge_lighteval ()
69+ import lighteval # noqa
5770
5871 importlib .reload (lighteval )
59- return test_func (* args , ** kwargs )
72+
73+ return result
6074
6175 return wrapper
6276
@@ -94,7 +108,7 @@ def test_vllm_required_for_vllm_usage():
94108
95109 with pytest .raises (
96110 ImportError ,
97- match = "Through the use of VLLMModel, you requested the use of `vllm<0.10.2,>=0.10.0` for this evaluation, but it is not available in your current environment. Please install it using pip. " ,
111+ match = "Through the use of VLLMModel, you requested the use of " ,
98112 ):
99113 vllm (model_args = "model_name=gpt2" , tasks = "lighteval|aime24|0" , max_samples = 0 )
100114
@@ -103,10 +117,12 @@ def test_vllm_required_for_vllm_usage():
103117def test_placeholder_is_returned_when_missing_dependencies ():
104118 from lighteval .metrics .utils .linguistic_tokenizers import SpaCyTokenizer
105119
106- assert isinstance (SpaCyTokenizer , DummyObject )
120+ assert SpaCyTokenizer .__name__ == "SpaCyTokenizer"
121+ assert SpaCyTokenizer .__class__ .__name__ == "DummyObject"
107122
108123
109124def test_original_object_is_returned_when_dependencies_are_satisfied ():
110125 from lighteval .metrics .utils .linguistic_tokenizers import SpaCyTokenizer
111126
112- assert not isinstance (SpaCyTokenizer , DummyObject )
127+ assert SpaCyTokenizer .__name__ == "SpaCyTokenizer"
128+ assert SpaCyTokenizer .__class__ .__name__ == "ABCMeta"
0 commit comments