Skip to content

Commit 0e0eaaa

Browse files
authored
Set back feature to create a repo when using clone_from (#1187)
* Set back feature to create a repo when using clone_from * fix repo_id vs repo_name * expect deprecation ++ run dataset repository tests separately
1 parent b1a11c2 commit 0e0eaaa

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

.github/workflows/python-tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ jobs:
7575
case "${{ matrix.test_name }}" in
7676
7777
"Repository only")
78-
eval "$PYTEST ../tests -k 'RepositoryTest'"
78+
eval "$PYTEST ../tests -k 'RepositoryTest or RepositoryDatasetTest'"
7979
;;
8080
8181
"Everything else")
82-
eval "$PYTEST ../tests -k 'not RepositoryTest'"
82+
eval "$PYTEST ../tests -k 'not RepositoryTest and not RepositoryDatasetTest'"
8383
;;
8484
8585
lfs)

src/huggingface_hub/repository.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import threading
77
import time
8+
import warnings
89
from contextlib import contextmanager
910
from pathlib import Path
1011
from typing import Callable, Dict, Iterator, List, Optional, Tuple, Union
@@ -15,7 +16,14 @@
1516

1617
from .hf_api import HfApi, repo_type_and_id_from_hf_id
1718
from .lfs import LFS_MULTIPART_UPLOAD_COMMAND
18-
from .utils import HfFolder, logging, run_subprocess, tqdm, validate_hf_hub_args
19+
from .utils import (
20+
HfFolder,
21+
RepositoryNotFoundError,
22+
logging,
23+
run_subprocess,
24+
tqdm,
25+
validate_hf_hub_args,
26+
)
1927
from .utils._deprecation import _deprecate_arguments, _deprecate_method
2028
from .utils._typing import TypedDict
2129

@@ -684,9 +692,10 @@ def clone_from(self, repo_url: str, token: Union[bool, str, None] = None):
684692
if hub_url in repo_url or (
685693
"http" not in repo_url and len(repo_url.split("/")) <= 2
686694
):
687-
repo_type, namespace, repo_id = repo_type_and_id_from_hf_id(
695+
repo_type, namespace, repo_name = repo_type_and_id_from_hf_id(
688696
repo_url, hub_url=hub_url
689697
)
698+
repo_id = f"{namespace}/{repo_name}" if namespace is not None else repo_name
690699

691700
if repo_type is not None:
692701
self._repo_type = repo_type
@@ -701,10 +710,32 @@ def clone_from(self, repo_url: str, token: Union[bool, str, None] = None):
701710
scheme = urlparse(repo_url).scheme
702711
repo_url = repo_url.replace(f"{scheme}://", f"{scheme}://user:{token}@")
703712

704-
if namespace is not None:
705-
repo_url += f"{namespace}/"
706713
repo_url += repo_id
707714

715+
# To be removed: check if repo exists. If not, create it first.
716+
try:
717+
HfApi().repo_info(f"{repo_id}", repo_type=self._repo_type, token=token)
718+
except RepositoryNotFoundError:
719+
if self._repo_type == "space":
720+
raise ValueError(
721+
"Creating a Space through passing Space link to clone_from is"
722+
" not allowed. Make sure the Space exists on Hugging Face Hub."
723+
)
724+
else:
725+
warnings.warn(
726+
"Creating a repository through 'clone_from' is deprecated and"
727+
" will be removed in v0.12. Please create the repository first"
728+
" using `create_repo(..., exists_ok=True)`.",
729+
FutureWarning,
730+
)
731+
self.client.create_repo(
732+
repo_id=repo_id,
733+
token=token,
734+
repo_type=self._repo_type,
735+
exist_ok=True,
736+
private=self._private,
737+
)
738+
708739
# For error messages, it's cleaner to show the repo url without the token.
709740
clean_repo_url = re.sub(r"(https?)://.*@", r"\1://", repo_url)
710741
try:

tests/test_repository.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import uuid
2323
from io import BytesIO
2424

25-
import pytest
26-
2725
import requests
2826
from huggingface_hub._login import _currently_setup_credential_helpers
2927
from huggingface_hub.hf_api import HfApi
@@ -118,17 +116,16 @@ def test_init_clone_from(self):
118116
repo_id=f"{USER}/{self.REPO_NAME}-temp", repo_type="space"
119117
)
120118

119+
@expect_deprecation("clone_from")
121120
def test_clone_from_missing_repo(self):
122121
"""
123-
If the repo does not exist, it is not created automatically. This behavior
124-
was possible before but has been deprecated and removed.
122+
If the repo does not exist, it is created automatically.
123+
124+
This behavior is deprecated and will be removed in v0.12.
125125
"""
126-
with pytest.raises(EnvironmentError, match=".*remote: Repository not found.*"):
127-
Repository(
128-
WORKING_REPO_DIR,
129-
clone_from=f"{USER}/{uuid.uuid4()}",
130-
use_auth_token=self._token,
131-
)
126+
Repository(
127+
WORKING_REPO_DIR, clone_from=f"{USER}/{uuid.uuid4()}", token=self._token
128+
)
132129

133130
def test_clone_from_model(self):
134131
temp_repo_url = self._api.create_repo(
@@ -484,6 +481,7 @@ def test_clone_with_repo_name_and_user_namespace(self):
484481
self.assertTrue("model.bin" in files)
485482

486483
@retry_endpoint
484+
@expect_deprecation("clone_from")
487485
def test_clone_with_repo_name_and_no_namespace(self):
488486
with self.assertRaises(EnvironmentError):
489487
Repository(
@@ -1745,6 +1743,7 @@ def test_clone_with_repo_name_and_user_namespace(self):
17451743
self.assertTrue("test.py" in files)
17461744

17471745
@retry_endpoint
1746+
@expect_deprecation("clone_from")
17481747
def test_clone_with_repo_name_and_no_namespace(self):
17491748
self.assertRaises(
17501749
OSError,

0 commit comments

Comments
 (0)