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+ PATCH_TARGET = "lighteval.utils.imports.is_package_available" # adjust if needed
33+
34+
35+ def purge_lighteval ():
36+ for name in list (sys .modules ):
37+ if name == "lighteval" or name .startswith ("lighteval." ):
38+ sys .modules .pop (name , None )
3239
3340
3441def pretend_missing (* names ):
3542 """
36- Decorator: pretend that certain packages are missing
37- by patching mypkg.utils.is_package_available .
43+ Decorator for individual tests: make is_package_available(...) return False
44+ for given names. After the test, restore the initial dependencies .
3845 """
3946
4047 def decorator (test_func ):
4148 @functools .wraps (test_func )
4249 def wrapper (* args , ** kwargs ):
43- from unittest .mock import patch
50+ # Ensure import-time guards run under the fake
51+ purge_lighteval ()
4452
4553 def fake (requirement ):
46- name = requirement
47- if isinstance (requirement , Requirement ):
48- name = requirement .name
49- elif isinstance (requirement , Extra ):
50- name = requirement .value
54+ name = getattr (requirement , "name" , getattr (requirement , "value" , requirement ))
5155
56+ # Probably a cleaner option here; returns False
5257 return False if name in names else (importlib .util .find_spec (name ) is not None )
5358
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
59+ result = None
60+ try :
61+ # Patch dependencies to remove the one we want to remove
62+ with patch (PATCH_TARGET , side_effect = fake ):
63+ importlib .invalidate_caches ()
64+ import lighteval # noqa
65+
66+ importlib .reload (lighteval )
67+ result = test_func (* args , ** kwargs )
68+ finally :
69+ # After the decorator, restore the initial dependencies
70+ importlib .invalidate_caches ()
71+ purge_lighteval ()
72+ import lighteval # noqa
5773
5874 importlib .reload (lighteval )
59- return test_func (* args , ** kwargs )
75+
76+ return result
6077
6178 return wrapper
6279
@@ -94,7 +111,7 @@ def test_vllm_required_for_vllm_usage():
94111
95112 with pytest .raises (
96113 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. " ,
114+ match = "Through the use of VLLMModel, you requested the use of " ,
98115 ):
99116 vllm (model_args = "model_name=gpt2" , tasks = "lighteval|aime24|0" , max_samples = 0 )
100117
@@ -103,10 +120,12 @@ def test_vllm_required_for_vllm_usage():
103120def test_placeholder_is_returned_when_missing_dependencies ():
104121 from lighteval .metrics .utils .linguistic_tokenizers import SpaCyTokenizer
105122
106- assert isinstance (SpaCyTokenizer , DummyObject )
123+ assert SpaCyTokenizer .__name__ == "SpaCyTokenizer"
124+ assert SpaCyTokenizer .__class__ .__name__ == "DummyObject"
107125
108126
109127def test_original_object_is_returned_when_dependencies_are_satisfied ():
110128 from lighteval .metrics .utils .linguistic_tokenizers import SpaCyTokenizer
111129
112- assert not isinstance (SpaCyTokenizer , DummyObject )
130+ assert SpaCyTokenizer .__name__ == "SpaCyTokenizer"
131+ assert SpaCyTokenizer .__class__ .__name__ == "ABCMeta"
0 commit comments