@@ -375,6 +375,16 @@ def _download_or_get_cache(
375
375
assert url is not None , 'URL is undefined from resource.'
376
376
377
377
expected_url_info = self ._url_infos .get (url )
378
+ if (
379
+ not self ._register_checksums
380
+ and self ._force_checksums_validation
381
+ and not expected_url_info
382
+ ):
383
+ raise ValueError (
384
+ f'Missing checksums url: { url } , yet'
385
+ ' `force_checksums_validation=True`. Did you forget to register'
386
+ ' checksums?'
387
+ )
378
388
379
389
# User has manually downloaded the file.
380
390
if manually_downloaded_path := self ._get_manually_downloaded_path (
@@ -449,18 +459,17 @@ def _register_or_validate_checksums(
449
459
) -> epath .Path | None :
450
460
"""Validates/records checksums and returns checksum path if registered."""
451
461
url : str = resource .url # pytype: disable=annotation-type-mismatch
452
- self ._recorded_url_infos [url ] = computed_url_info
453
462
454
463
if self ._register_checksums :
455
464
# Note:
456
465
# * We save even if `expected_url_info == computed_url_info` as
457
466
# `expected_url_info` might have been loaded from another dataset.
458
467
# * `register_checksums_path` was validated in `__init__` so this
459
468
# shouldn't fail.
469
+ self ._recorded_url_infos [url ] = computed_url_info
460
470
self ._record_url_infos ()
461
471
return self ._get_dl_path (resource , computed_url_info .checksum )
462
- else :
463
- expected_url_info = self ._url_infos .get (url )
472
+ elif expected_url_info := self ._url_infos .get (url ):
464
473
# Eventually validate checksums
465
474
# Note:
466
475
# * If path is cached at `url_path` but cached
@@ -476,29 +485,17 @@ def _register_or_validate_checksums(
476
485
computed_url_info = computed_url_info ,
477
486
path = path ,
478
487
)
479
- if expected_url_info :
480
- return self ._get_dl_path (resource , expected_url_info .checksum )
488
+ return self ._get_dl_path (resource , expected_url_info .checksum )
481
489
482
490
def _validate_checksums (
483
491
self ,
484
492
url : str ,
485
- expected_url_info : checksums .UrlInfo | None ,
493
+ expected_url_info : checksums .UrlInfo ,
486
494
computed_url_info : checksums .UrlInfo ,
487
495
path : epath .Path ,
488
496
) -> None :
489
497
"""Validate computed_url_info match expected_url_info."""
490
- # If force-checksums validations, both expected and computed url_info
491
- # should exists
492
- if self ._force_checksums_validation :
493
- # Checksums have not been registered
494
- if not expected_url_info :
495
- raise ValueError (
496
- f'Missing checksums url: { url } , yet '
497
- '`force_checksums_validation=True`. '
498
- 'Did you forget to register checksums?'
499
- )
500
-
501
- if expected_url_info and expected_url_info != computed_url_info :
498
+ if expected_url_info != computed_url_info :
502
499
msg = (
503
500
f'Artifact { url } , downloaded to { path } , has wrong checksum:\n '
504
501
f'* Expected: { expected_url_info } \n '
0 commit comments