Skip to content

Commit c931fd0

Browse files
fineguyThe TensorFlow Datasets Authors
authored andcommitted
Make Resource.url a property.
PiperOrigin-RevId: 681063729
1 parent 2b58011 commit c931fd0

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

tensorflow_datasets/core/download/download_manager.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,6 @@ def _download_or_get_cache(
372372
if not isinstance(resource, resource_lib.Resource):
373373
resource = resource_lib.Resource(url=resource)
374374
url = resource.url
375-
assert url is not None, 'URL is undefined from resource.'
376375

377376
expected_url_info = self._url_infos.get(url)
378377
if (
@@ -406,7 +405,7 @@ def _download_or_get_cache(
406405
return self._download(resource)
407406

408407
# Download has been cached (checksum known)
409-
elif expected_url_info and resource_lib.Resource.exists_locally(
408+
elif expected_url_info and resource_lib.is_locally_cached(
410409
checksum_path := self._get_dl_path(resource, expected_url_info.checksum)
411410
):
412411
self._register_or_validate_checksums(
@@ -420,7 +419,7 @@ def _download_or_get_cache(
420419
return promise.Promise.resolve(checksum_path)
421420

422421
# Download has been cached (checksum unknown)
423-
elif resource_lib.Resource.exists_locally(
422+
elif resource_lib.is_locally_cached(
424423
url_path := self._get_dl_path(resource)
425424
):
426425
computed_url_info = downloader.read_url_info(url_path)
@@ -458,7 +457,7 @@ def _register_or_validate_checksums(
458457
computed_url_info: checksums.UrlInfo,
459458
) -> epath.Path | None:
460459
"""Validates/records checksums and returns checksum path if registered."""
461-
url: str = resource.url # pytype: disable=annotation-type-mismatch
460+
url = resource.url
462461

463462
if self._register_checksums:
464463
# Note:
@@ -517,7 +516,7 @@ def _download(
517516
Promise of the path to the downloaded url.
518517
"""
519518
url_path = self._get_dl_path(resource)
520-
url: str = resource.url # pytype: disable=annotation-type-mismatch
519+
url = resource.url
521520

522521
# Download in a tmp directory next to url_path (to avoid name collisions)
523522
# `download_tmp_dir` is cleaned-up in `callback`

tensorflow_datasets/core/download/resource.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ def get_info_path(path: epath.Path) -> epath.Path:
197197
return path.with_suffix(path.suffix + '.INFO')
198198

199199

200+
def is_locally_cached(path: epath.Path) -> bool:
201+
"""Returns whether the path is locally cached."""
202+
# If INFO file doesn't exist, consider path NOT cached.
203+
return path.exists() and get_info_path(path).exists()
204+
205+
200206
def _read_info(info_path: epath.Path) -> Json:
201207
"""Returns info dict."""
202208
if not info_path.exists():
@@ -298,19 +304,19 @@ def __init__(
298304
relative_download_dir: Optional directory for downloading relative to
299305
`download_dir`.
300306
"""
301-
self.url = url
307+
self._url = url
302308
self._extract_method = extract_method
303309
self.path: epath.Path = epath.Path(path) if path else None # pytype: disable=annotation-type-mismatch # attribute-variable-annotations
304310
self.relative_download_dir = relative_download_dir
305311

306-
@classmethod
307-
def exists_locally(cls, path: epath.Path) -> bool:
308-
"""Returns whether the resource exists locally, at `resource.path`."""
309-
# If INFO file doesn't exist, consider resource does NOT exist, as it would
310-
# prevent guessing the `extract_method`.
311-
return path.exists() and get_info_path(path).exists()
312+
@property
313+
def url(self) -> str:
314+
"""Returns the URL at which to download the resource."""
315+
if not self._url:
316+
raise ValueError('URL is undefined from resource.')
317+
return self._url
312318

313319
@property
314320
def extract_method(self) -> ExtractMethod:
315-
"""Returns `ExtractMethod` to use on resource. Cannot be None."""
321+
"""Returns `ExtractMethod` to use on resource."""
316322
return self._extract_method or _get_extract_method(self.path)

tensorflow_datasets/testing/dataset_builder_testing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ def _get_dl_extract_result(self, url):
315315
return self._prepare_download_results(self.DL_EXTRACT_RESULT)
316316

317317
def _get_dl_extract_only_result(self, url):
318+
del url
318319
if self.DL_EXTRACT_ONLY_RESULT:
319-
tf.nest.map_structure(self._add_url, url)
320320
return self._prepare_download_results(self.DL_EXTRACT_ONLY_RESULT)
321321

322322
def _get_dl_download_result(self, url):

0 commit comments

Comments
 (0)