From 01dd37b82d5a00b2f44932e49e37ae7afdf2c1d6 Mon Sep 17 00:00:00 2001 From: narugo1992 Date: Thu, 6 Feb 2025 09:46:02 +0800 Subject: [PATCH] dev(narugo): raise error on every unexpected branches --- src/huggingface_hub/file_download.py | 4 +--- src/huggingface_hub/utils/_http.py | 14 +++++++------- tests/test_utils_http.py | 10 +++++++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/huggingface_hub/file_download.py b/src/huggingface_hub/file_download.py index f5e4198d43..9703250c4b 100644 --- a/src/huggingface_hub/file_download.py +++ b/src/huggingface_hub/file_download.py @@ -365,9 +365,7 @@ def http_get( initial_headers = headers headers = copy.deepcopy(headers) or {} if resume_size > 0: - adjusted_range = _adjust_range_header(headers.get("Range"), resume_size) - if adjusted_range is not None: - headers["Range"] = adjusted_range + headers["Range"] = _adjust_range_header(headers.get("Range"), resume_size) r = _request_wrapper( method="GET", url=url, stream=True, proxies=proxies, headers=headers, timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT diff --git a/src/huggingface_hub/utils/_http.py b/src/huggingface_hub/utils/_http.py index 4d49cc9f05..e29c68b365 100644 --- a/src/huggingface_hub/utils/_http.py +++ b/src/huggingface_hub/utils/_http.py @@ -20,7 +20,6 @@ import threading import time import uuid -import warnings from functools import lru_cache from http import HTTPStatus from shlex import quote @@ -605,8 +604,7 @@ def _adjust_range_header(original_range: Optional[str], resume_size: int) -> Opt return f"bytes={resume_size}-" if "," in original_range: - warnings.warn(f"Multiple ranges detected - {original_range!r}, using full range after resume", UserWarning) - return f"bytes={resume_size}-" + raise ValueError(f"Multiple ranges detected - {original_range!r}, not supported yet.") match = RANGE_REGEX.match(original_range) if not match: @@ -618,16 +616,18 @@ def _adjust_range_header(original_range: Optional[str], resume_size: int) -> Opt raise RuntimeError(f"Invalid range format - {original_range!r}.") new_suffix = int(end) - resume_size + new_range = f"bytes=-{new_suffix}" if new_suffix <= 0: - return None - return f"bytes=-{new_suffix}" + raise RuntimeError(f"Empty new range - {new_range!r}.") + return new_range start = int(start) new_start = start + resume_size if end: end = int(end) + new_range = f"bytes={new_start}-{end}" if new_start > end: - return None - return f"bytes={new_start}-{end}" + raise RuntimeError(f"Empty new range - {new_range!r}.") + return new_range return f"bytes={new_start}-" diff --git a/tests/test_utils_http.py b/tests/test_utils_http.py index ee7e4df46f..07037e6aba 100644 --- a/tests/test_utils_http.py +++ b/tests/test_utils_http.py @@ -319,6 +319,7 @@ def test_adjust_range_header(): assert _adjust_range_header(None, 10) == "bytes=10-" assert _adjust_range_header("bytes=0-100", 10) == "bytes=10-100" assert _adjust_range_header("bytes=-100", 10) == "bytes=-90" + assert _adjust_range_header("bytes=100-", 10) == "bytes=110-" with pytest.raises(RuntimeError): _adjust_range_header("invalid", 10) @@ -327,8 +328,11 @@ def test_adjust_range_header(): _adjust_range_header("bytes=-", 10) # Multiple ranges - assert _adjust_range_header("bytes=0-100,200-300", 10) == "bytes=10-" + with pytest.raises(ValueError): + _adjust_range_header("bytes=0-100,200-300", 10) # Resume size exceeds range - assert _adjust_range_header("bytes=0-100", 150) is None - assert _adjust_range_header("bytes=-50", 100) is None + with pytest.raises(RuntimeError): + _adjust_range_header("bytes=0-100", 150) + with pytest.raises(RuntimeError): + _adjust_range_header("bytes=-50", 100)