Skip to content

Commit 29ec2fe

Browse files
committed
fixing code according to coderabbitai
Signed-off-by: Enoch Mok <enochmokny@gmail.com>
1 parent 98ccecc commit 29ec2fe

2 files changed

Lines changed: 10 additions & 13 deletions

File tree

monai/apps/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ def download_url(
228228
filepath = Path(filepath)
229229
if filepath.exists():
230230
if not check_hash(filepath, hash_val, hash_type):
231-
raise RuntimeError(
232-
f"{hash_type} check of existing file failed: filepath={filepath}, expected {hash_type}={hash_val}."
231+
raise ValueError(
232+
f"{hash_type} hash check of existing file failed: filepath={filepath}, expected {hash_type}={hash_val}."
233233
)
234234
logger.info(f"File exists: {filepath}, skipped downloading.")
235235
return
@@ -259,7 +259,7 @@ def download_url(
259259
raise RuntimeError(
260260
f"Download of file from {url} to {filepath} failed due to network issue or denied permission."
261261
)
262-
if not check_hash(filepath, hash_val, hash_type):
262+
if not check_hash(tmp_name, hash_val, hash_type):
263263
raise ValueError(
264264
f"{hash_type} hash check of downloaded file failed: URL={url}, "
265265
f"filepath={filepath}, expected {hash_type}={hash_val}, "

tests/test_utils.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def skip_if_downloading_fails():
186186
if "hash check" in str(v_e):
187187
raise unittest.SkipTest(f"Hash value error while downloading: {v_e}") from v_e
188188

189+
raise v_e
190+
189191

190192
SAMPLE_TIFF = "https://huggingface.co/datasets/MONAI/testing_data/resolve/main/CMU-1.tiff"
191193
SAMPLE_TIFF_HASH = "73a7e89bc15576587c3d68e55d9bf92f09690280166240b48ff4b48230b13bcd"
@@ -202,20 +204,15 @@ def test_download_url(self):
202204
ValueError: When the downloaded file's hash does not match.
203205
"""
204206
with tempfile.TemporaryDirectory() as tempdir:
207+
model_path = os.path.join(tempdir, "model.tiff")
208+
205209
with skip_if_downloading_fails():
206210
download_url(
207-
url=SAMPLE_TIFF,
208-
filepath=os.path.join(tempdir, "model.tiff"),
209-
hash_val=SAMPLE_TIFF_HASH,
210-
hash_type=SAMPLE_TIFF_HASH_TYPE,
211+
url=SAMPLE_TIFF, filepath=model_path, hash_val=SAMPLE_TIFF_HASH, hash_type=SAMPLE_TIFF_HASH_TYPE
211212
)
212213
with self.assertRaises(ValueError):
213-
download_url(
214-
url=SAMPLE_TIFF,
215-
filepath=os.path.join(tempdir, "model_bad.tiff"),
216-
hash_val="0" * 64,
217-
hash_type=SAMPLE_TIFF_HASH_TYPE,
218-
)
214+
# checking for wrong hash
215+
download_url(filepath=model_path, hash_val="0" * 64, hash_type=SAMPLE_TIFF_HASH_TYPE)
219216

220217

221218
def test_pretrained_networks(network, input_param, device):

0 commit comments

Comments
 (0)