MCPcopy Create free account
hub / github.com/huggingface/evaluate / LocalModuleTest

Class LocalModuleTest

tests/test_metric_common.py:97–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95@for_all_test_methods(skip_if_metric_requires_fairseq, skip_on_windows_if_not_windows_compatible, skip_slow_metrics)
96@local
97class LocalModuleTest(parameterized.TestCase):
98 INTENSIVE_CALLS_PATCHER = {}
99 evaluation_module_name = None
100 evaluation_module_type = None
101
102 def test_load(self, evaluation_module_name, evaluation_module_type):
103 doctest.ELLIPSIS_MARKER = "[...]"
104 evaluation_module = importlib.import_module(
105 evaluate.loading.evaluation_module_factory(
106 os.path.join(evaluation_module_type + "s", evaluation_module_name), module_type=evaluation_module_type
107 ).module_path
108 )
109 evaluation_instance = evaluate.loading.import_main_class(evaluation_module.__name__)
110 # check parameters
111 parameters = inspect.signature(evaluation_instance._compute).parameters
112 self.assertTrue(all([p.kind != p.VAR_KEYWORD for p in parameters.values()])) # no **kwargs
113 # run doctest
114 with self.patch_intensive_calls(evaluation_module_name, evaluation_module.__name__):
115 with self.use_local_metrics(evaluation_module_type):
116 try:
117 results = doctest.testmod(evaluation_module, verbose=True, raise_on_error=True)
118 except doctest.UnexpectedException as e:
119 raise e.exc_info[1] # raise the exception that doctest caught
120 self.assertEqual(results.failed, 0)
121 self.assertGreater(results.attempted, 1)
122
123 @slow
124 def test_load_real_metric(self, evaluation_module_name, evaluation_module_type):
125 doctest.ELLIPSIS_MARKER = "[...]"
126 metric_module = importlib.import_module(
127 evaluate.loading.evaluation_module_factory(
128 os.path.join(evaluation_module_type, evaluation_module_name)
129 ).module_path
130 )
131 # run doctest
132 with self.use_local_metrics():
133 results = doctest.testmod(metric_module, verbose=True, raise_on_error=True)
134 self.assertEqual(results.failed, 0)
135 self.assertGreater(results.attempted, 1)
136
137 @contextmanager
138 def patch_intensive_calls(self, evaluation_module_name, module_name):
139 if evaluation_module_name in self.INTENSIVE_CALLS_PATCHER:
140 with self.INTENSIVE_CALLS_PATCHER[evaluation_module_name](module_name):
141 yield
142 else:
143 yield
144
145 @contextmanager
146 def use_local_metrics(self, evaluation_module_type):
147 def load_local_metric(evaluation_module_name, *args, **kwargs):
148 return load(os.path.join(evaluation_module_type + "s", evaluation_module_name), *args, **kwargs)
149
150 with patch("evaluate.load") as mock_load:
151 mock_load.side_effect = load_local_metric
152 yield
153
154 @classmethod

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…