Skip to content

Commit d19275b

Browse files
fineguyThe TensorFlow Datasets Authors
authored andcommitted
Exit early in _download_or_get_cache.
PiperOrigin-RevId: 680557149
1 parent 6682355 commit d19275b

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

tensorflow_datasets/core/download/download_manager.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ def _download_or_get_cache(
375375
assert url is not None, 'URL is undefined from resource.'
376376

377377
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+
)
378388

379389
# User has manually downloaded the file.
380390
if manually_downloaded_path := self._get_manually_downloaded_path(
@@ -449,18 +459,17 @@ def _register_or_validate_checksums(
449459
) -> epath.Path | None:
450460
"""Validates/records checksums and returns checksum path if registered."""
451461
url: str = resource.url # pytype: disable=annotation-type-mismatch
452-
self._recorded_url_infos[url] = computed_url_info
453462

454463
if self._register_checksums:
455464
# Note:
456465
# * We save even if `expected_url_info == computed_url_info` as
457466
# `expected_url_info` might have been loaded from another dataset.
458467
# * `register_checksums_path` was validated in `__init__` so this
459468
# shouldn't fail.
469+
self._recorded_url_infos[url] = computed_url_info
460470
self._record_url_infos()
461471
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):
464473
# Eventually validate checksums
465474
# Note:
466475
# * If path is cached at `url_path` but cached
@@ -476,29 +485,17 @@ def _register_or_validate_checksums(
476485
computed_url_info=computed_url_info,
477486
path=path,
478487
)
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)
481489

482490
def _validate_checksums(
483491
self,
484492
url: str,
485-
expected_url_info: checksums.UrlInfo | None,
493+
expected_url_info: checksums.UrlInfo,
486494
computed_url_info: checksums.UrlInfo,
487495
path: epath.Path,
488496
) -> None:
489497
"""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:
502499
msg = (
503500
f'Artifact {url}, downloaded to {path}, has wrong checksum:\n'
504501
f'* Expected: {expected_url_info}\n'

0 commit comments

Comments
 (0)