@@ -740,6 +740,31 @@ def _mocked_hf_file_metadata(*args, **kwargs):
740
740
with self .assertRaises (EnvironmentError ):
741
741
hf_hub_download (DUMMY_MODEL_ID , filename = "pytorch_model.bin" , cache_dir = cache_dir )
742
742
743
+ def test_hf_hub_download_when_tmp_file_is_complete (self ):
744
+ """Regression test for #2511.
745
+
746
+ See https://github.com/huggingface/huggingface_hub/issues/2511.
747
+
748
+ When downloading a file, we first download to a temporary file and then move it to the final location.
749
+ If the temporary file is already partially downloaded, we resume from where we left off.
750
+ However, if the temporary file is already fully downloaded, we should try to make a GET call with an empty range.
751
+ This was causing a "416 Range Not Satisfiable" error.
752
+ """
753
+ with SoftTemporaryDirectory () as tmpdir :
754
+ # Download the file once
755
+ filepath = Path (hf_hub_download (DUMMY_MODEL_ID , filename = "pytorch_model.bin" , cache_dir = tmpdir ))
756
+
757
+ # Fake tmp file
758
+ incomplete_filepath = Path (str (filepath .resolve ()) + ".incomplete" )
759
+ incomplete_filepath .write_bytes (filepath .read_bytes ()) # fake a partial download
760
+ filepath .resolve ().unlink ()
761
+
762
+ # delete snapshot folder to re-trigger a download
763
+ shutil .rmtree (filepath .parents [2 ] / "snapshots" )
764
+
765
+ # Download must not fail
766
+ hf_hub_download (DUMMY_MODEL_ID , filename = "pytorch_model.bin" , cache_dir = tmpdir )
767
+
743
768
@expect_deprecation ("cached_download" )
744
769
@expect_deprecation ("url_to_filename" )
745
770
def test_cached_download_from_github (self ):
0 commit comments