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()
This commit is contained in:
Manuel de Prada Corral 2025-04-22 11:38:10 +02:00 committed by GitHub
parent c69e23455d
commit 1cd110c6cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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")