From 1cd110c6cb6a6237614130c470e9a902dbc1a4bd Mon Sep 17 00:00:00 2001 From: Manuel de Prada Corral <6536835+manueldeprada@users.noreply.github.com> Date: Tue, 22 Apr 2025 11:38:10 +0200 Subject: [PATCH] Add test to ensure unknown exceptions reraising in utils/hub.py::cached_files() (#37651) * add test to ensure unknown exceptions are reraised in utils/hub.py::cached_files() --- tests/utils/test_hub_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/utils/test_hub_utils.py b/tests/utils/test_hub_utils.py index 447e81ffd96..0bccec449f0 100644 --- a/tests/utils/test_hub_utils.py +++ b/tests/utils/test_hub_utils.py @@ -189,3 +189,12 @@ class GetFromCacheTests(unittest.TestCase): with self.assertRaisesRegex(EnvironmentError, "is a gated repository"): # All files except README.md are protected on a gated repo. has_file(GATED_REPO, "gated_file.txt", token=False) + + def test_cached_files_exception_raised(self): + """Test that unhadled exceptions, e.g. ModuleNotFoundError, is properly re-raised by cached_files when hf_hub_download fails.""" + with mock.patch( + "transformers.utils.hub.hf_hub_download", side_effect=ModuleNotFoundError("No module named 'MockModule'") + ): + with self.assertRaises(ModuleNotFoundError): + # The error should be re-raised by cached_files, not caught in the exception handling block + cached_file(RANDOM_BERT, "nonexistent.json")