Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated code from v0.9, v0.10 and v0.11 #1092

Merged
merged 9 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 13 additions & 144 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,6 @@
logger = logging.get_logger(__name__)


# TODO: remove after deprecation period is over (v0.10)
def _validate_repo_id_deprecation(repo_id, name, organization):
"""Returns (name, organization) from the input."""
if repo_id and not name and organization:
# this means the user had passed name as positional, now mapped to
# repo_id and is passing organization as well. This wouldn't be an
# issue if they pass everything as kwarg. So we switch the parameters
# here:
repo_id, name = name, repo_id

if not (repo_id or name):
raise ValueError(
"No name provided. Please pass `repo_id` with a valid repository name."
)

if repo_id and (name or organization):
raise ValueError(
"Only pass `repo_id` and leave deprecated `name` and `organization` to be"
" None."
)
elif name or organization:
warnings.warn(
"`name` and `organization` input arguments are deprecated and "
"will be removed in v0.10. Pass `repo_id` instead.",
FutureWarning,
)
else:
if "/" in repo_id:
organization, name = repo_id.split("/")
else:
organization, name = None, repo_id
return name, organization


def repo_type_and_id_from_hf_id(
hf_id: str, hub_url: Optional[str] = None
) -> Tuple[Optional[str], Optional[str], str]:
Expand All @@ -129,6 +95,10 @@ def repo_type_and_id_from_hf_id(
- <repo_id>
hub_url (`str`, *optional*):
The URL of the HuggingFace Hub, defaults to https://huggingface.co

Returns:
A tuple with three items: repo_type (`str` or `None`), namespace (`str` or
`None`) and repo_id (`str`).
"""
hub_url = re.sub(r"https?://", "", hub_url if hub_url is not None else ENDPOINT)
is_hf_url = hub_url in hf_id and "@" not in hf_id
Expand Down Expand Up @@ -1552,26 +1522,17 @@ def create_repo(
repo_id: str = None,
*,
token: Optional[str] = None,
organization: Optional[str] = None,
private: bool = False,
repo_type: Optional[str] = None,
exist_ok: Optional[bool] = False,
exist_ok: bool = False,
space_sdk: Optional[str] = None,
name: Optional[str] = None,
) -> str:
"""Create an empty repo on the HuggingFace Hub.

Args:
repo_id (`str`):
A namespace (user or an organization) and a repo name separated
by a `/`.

<Tip>

Version added: 0.5

</Tip>

token (`str`, *optional*):
An authentication token (See https://huggingface.co/settings/token)
private (`bool`, *optional*, defaults to `False`):
Expand All @@ -1589,42 +1550,10 @@ def create_repo(
Returns:
`str`: URL to the newly created repo.
"""
name, organization = _validate_repo_id_deprecation(repo_id, name, organization)
organization, name = repo_id.split("/") if "/" in repo_id else (None, repo_id)

path = f"{self.endpoint}/api/repos/create"

checked_name = repo_type_and_id_from_hf_id(name)

if (
repo_type is not None
and checked_name[0] is not None
and repo_type != checked_name[0]
):
raise ValueError(
f"""Passed `repo_type` and found `repo_type` are not the same ({repo_type},
{checked_name[0]}).
Please make sure you are expecting the right type of repository to
exist."""
)

if (
organization is not None
and checked_name[1] is not None
and organization != checked_name[1]
):
raise ValueError(
f"""Passed `organization` and `name` organization are not the same ({organization},
{checked_name[1]}).
Please either include the organization in only `name` or the
`organization` parameter, such as
`api.create_repo({checked_name[0]}, organization={organization})` or
`api.create_repo({checked_name[1]}/{checked_name[2]})`"""
)

repo_type = repo_type or checked_name[0]
organization = organization or checked_name[1]
name = checked_name[2]

if repo_type not in REPO_TYPES:
raise ValueError("Invalid repo type")

Expand All @@ -1648,23 +1577,20 @@ def create_repo(
)

if getattr(self, "_lfsmultipartthresh", None):
# Testing purposes only.
# See https://github.com/huggingface/huggingface_hub/pull/733/files#r820604472
json["lfsmultipartthresh"] = self._lfsmultipartthresh
headers = build_hf_headers(use_auth_token=token, is_write_action=True)
r = requests.post(path, headers=headers, json=json)

try:
hf_raise_for_status(r)
except HTTPError as err:
if not (exist_ok and err.response.status_code == 409):
try:
additional_info = r.json().get("error", None)
if additional_info:
new_err = f"{err.args[0]} - {additional_info}"
err.args = (new_err,) + err.args[1:]
except ValueError:
pass

raise err
if exist_ok and err.response.status_code == 409:
# Repo already exists and `exist_ok=True`
pass
else:
raise

d = r.json()
return d["url"]
Expand All @@ -1684,13 +1610,6 @@ def delete_repo(
repo_id (`str`):
A namespace (user or an organization) and a repo name separated
by a `/`.

<Tip>

Version added: 0.5

</Tip>

token (`str`, *optional*):
An authentication token (See https://huggingface.co/settings/token)
repo_type (`str`, *optional*):
Expand All @@ -1711,38 +1630,6 @@ def delete_repo(

path = f"{self.endpoint}/api/repos/delete"

checked_name = repo_type_and_id_from_hf_id(name)

if (
repo_type is not None
and checked_name[0] is not None
and repo_type != checked_name[0]
):
raise ValueError(
f"""Passed `repo_type` and found `repo_type` are not the same ({repo_type},
{checked_name[0]}).
Please make sure you are expecting the right type of repository to
exist."""
)

if (
organization is not None
and checked_name[1] is not None
and organization != checked_name[1]
):
raise ValueError(
"Passed `organization` and `name` organization are not the same"
f" ({organization}, {checked_name[1]})."
"\nPlease either include the organization in only `name` or the"
" `organization` parameter, such as "
f"`api.create_repo({checked_name[0]}, organization={organization})` "
f"or `api.create_repo({checked_name[1]}/{checked_name[2]})`"
)

repo_type = repo_type or checked_name[0]
organization = organization or checked_name[1]
name = checked_name[2]

if repo_type not in REPO_TYPES:
raise ValueError("Invalid repo type")

Expand Down Expand Up @@ -1771,13 +1658,6 @@ def update_repo_visibility(
repo_id (`str`, *optional*):
A namespace (user or an organization) and a repo name separated
by a `/`.

<Tip>

Version added: 0.5

</Tip>

private (`bool`, *optional*, defaults to `False`):
Whether the model repo should be private.
token (`str`, *optional*):
Expand Down Expand Up @@ -2091,7 +1971,6 @@ def upload_file(
token: Optional[str] = None,
repo_type: Optional[str] = None,
revision: Optional[str] = None,
identical_ok: Optional[bool] = None,
commit_message: Optional[str] = None,
commit_description: Optional[str] = None,
create_pr: Optional[bool] = None,
Expand Down Expand Up @@ -2122,9 +2001,6 @@ def upload_file(
revision (`str`, *optional*):
The git revision to commit from. Defaults to the head of the
`"main"` branch.
identical_ok (`bool`, *optional*, defaults to `True`):
Deprecated: will be removed in 0.11.0.
Changing this value has no effect.
commit_message (`str`, *optional*):
The summary / title / first line of the generated commit
commit_description (`str` *optional*)
Expand Down Expand Up @@ -2201,13 +2077,6 @@ def upload_file(
"https://huggingface.co/username/my-model/blob/refs%2Fpr%2F1/remote/file/path.h5"
```
"""
if identical_ok is not None:
warnings.warn(
"`identical_ok` has no effect and is deprecated. It will be removed in"
" 0.11.0.",
FutureWarning,
)

if repo_type not in REPO_TYPES:
raise ValueError(f"Invalid repo type, must be one of {REPO_TYPES}")

Expand Down
108 changes: 33 additions & 75 deletions src/huggingface_hub/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import tempfile
import threading
import time
import warnings
from contextlib import contextmanager
from pathlib import Path
from typing import Callable, Dict, Iterator, List, Optional, Tuple, Union
from urllib.parse import urlparse

from huggingface_hub.constants import REPO_TYPES_URL_PREFIXES, REPOCARD_NAME
from huggingface_hub.repocard import metadata_load, metadata_save
from requests.exceptions import HTTPError

from .hf_api import HfApi, repo_type_and_id_from_hf_id
from .lfs import LFS_MULTIPART_UPLOAD_COMMAND
Expand Down Expand Up @@ -677,49 +675,13 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
repo_url += REPO_TYPES_URL_PREFIXES[self._repo_type]

if token is not None:
whoami_info = self.client.whoami(token)
user = whoami_info["name"]
valid_organisations = [org["name"] for org in whoami_info["orgs"]]

if namespace is not None:
repo_id = f"{namespace}/{repo_id}"
repo_url += repo_id

# Add token in git url when provided
scheme = urlparse(repo_url).scheme
repo_url = repo_url.replace(f"{scheme}://", f"{scheme}://user:{token}@")
if namespace == user or namespace in valid_organisations:
try:
_ = HfApi().repo_info(
f"{repo_id}",
repo_type=self._repo_type,
use_auth_token=token,
)
except HTTPError:
if self._repo_type == "space":
raise ValueError(
"Creating a Space through passing Space link to"
" clone_from is not allowed. Make sure the Space exists"
" on Hugging Face Hub."
)
else:
warnings.warn(
"Creating a repository through 'clone_from' is"
" deprecated and will be removed in v0.11.",
FutureWarning,
)

self.client.create_repo(
repo_id=repo_id,
token=token,
repo_type=self._repo_type,
exist_ok=True,
private=self._private,
)

else:
if namespace is not None:
repo_url += f"{namespace}/"
repo_url += repo_id
if namespace is not None:
repo_url += f"{namespace}/"
repo_url += repo_id

# For error messages, it's cleaner to show the repo url without the token.
clean_repo_url = re.sub(r"(https?)://.*@", r"\1://", repo_url)
Expand All @@ -744,39 +706,7 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
)
else:
# Check if the folder is the root of a git repository
in_repository = is_git_repo(self.local_dir)

if in_repository:
if is_local_clone(self.local_dir, repo_url):
logger.warning(
f"{self.local_dir} is already a clone of {clean_repo_url}."
" Make sure you pull the latest changes with"
" `repo.git_pull()`."
)
else:
output = run_subprocess(
"git remote get-url origin".split(),
self.local_dir,
check=False,
)

error_msg = (
f"Tried to clone {clean_repo_url} in an unrelated git"
" repository.\nIf you believe this is an error, please add"
f" a remote with the following URL: {clean_repo_url}."
)
if output.returncode == 0:
clean_local_remote_url = re.sub(
r"https://.*@", "https://", output.stdout
)
error_msg += (
"\nLocal path has its origin defined as:"
f" {clean_local_remote_url}"
)

raise EnvironmentError(error_msg)

if not in_repository:
if not is_git_repo(self.local_dir):
raise EnvironmentError(
"Tried to clone a repository in a non-empty folder that isn't a"
" git repository. If you really want to do this, do it"
Expand All @@ -785,6 +715,34 @@ def clone_from(self, repo_url: str, use_auth_token: Union[bool, str, None] = Non
" existing files there afterwards."
)

if is_local_clone(self.local_dir, repo_url):
logger.warning(
f"{self.local_dir} is already a clone of {clean_repo_url}."
" Make sure you pull the latest changes with"
" `repo.git_pull()`."
)
else:
output = run_subprocess(
"git remote get-url origin".split(),
self.local_dir,
check=False,
)

error_msg = (
f"Tried to clone {clean_repo_url} in an unrelated git"
" repository.\nIf you believe this is an error, please add"
f" a remote with the following URL: {clean_repo_url}."
)
if output.returncode == 0:
clean_local_remote_url = re.sub(
r"https://.*@", "https://", output.stdout
)
error_msg += (
"\nLocal path has its origin defined as:"
f" {clean_local_remote_url}"
)
raise EnvironmentError(error_msg)

except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)

Expand Down
Loading