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(
176
176
]
177
177
except (GatedRepoError , RepositoryNotFoundError , RevisionNotFoundError ):
178
178
raise # valid errors => do not catch
179
- except (HTTPError , requests .exceptions .ConnectionError ):
179
+ except (HTTPError , OfflineModeIsEnabled , requests .exceptions .ConnectionError ):
180
180
pass # offline mode, internet down, etc. => try local files
181
181
182
182
# check local files
Original file line number Diff line number Diff line change 19
19
from pathlib import Path
20
20
21
21
from huggingface_hub import hf_hub_download
22
+ from huggingface_hub .errors import LocalEntryNotFoundError , OfflineModeIsEnabled
22
23
from requests .exceptions import HTTPError
23
24
24
25
from transformers .utils import (
29
30
WEIGHTS_NAME ,
30
31
cached_file ,
31
32
has_file ,
33
+ list_repo_templates ,
32
34
)
33
35
34
36
@@ -198,3 +200,12 @@ def test_cached_files_exception_raised(self):
198
200
with self .assertRaises (ModuleNotFoundError ):
199
201
# The error should be re-raised by cached_files, not caught in the exception handling block
200
202
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