File tree Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Expand file tree Collapse file tree 2 files changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -176,7 +176,7 @@ def list_repo_templates(
176176 ]
177177 except (GatedRepoError , RepositoryNotFoundError , RevisionNotFoundError ):
178178 raise # valid errors => do not catch
179- except (HTTPError , requests .exceptions .ConnectionError ):
179+ except (HTTPError , OfflineModeIsEnabled , requests .exceptions .ConnectionError ):
180180 pass # offline mode, internet down, etc. => try local files
181181
182182 # check local files
Original file line number Diff line number Diff line change 1919from pathlib import Path
2020
2121from huggingface_hub import hf_hub_download
22+ from huggingface_hub .errors import LocalEntryNotFoundError , OfflineModeIsEnabled
2223from requests .exceptions import HTTPError
2324
2425from transformers .utils import (
2930 WEIGHTS_NAME ,
3031 cached_file ,
3132 has_file ,
33+ list_repo_templates ,
3234)
3335
3436
@@ -198,3 +200,12 @@ def test_cached_files_exception_raised(self):
198200 with self .assertRaises (ModuleNotFoundError ):
199201 # The error should be re-raised by cached_files, not caught in the exception handling block
200202 cached_file (RANDOM_BERT , "nonexistent.json" )
203+
204+
205+ class OfflineModeTests (unittest .TestCase ):
206+ def test_list_repo_templates_w_offline (self ):
207+ with mock .patch ("transformers.utils.hub.list_repo_tree" , side_effect = OfflineModeIsEnabled ()):
208+ with mock .patch (
209+ "transformers.utils.hub.snapshot_download" , side_effect = LocalEntryNotFoundError ("no snapshot found" )
210+ ):
211+ self .assertEqual (list_repo_templates (RANDOM_BERT , local_files_only = False ), [])
You can’t perform that action at this time.
0 commit comments