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

Prepare for 0.14 #1381

Merged
merged 18 commits into from
Mar 23, 2023
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
2 changes: 0 additions & 2 deletions src/huggingface_hub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ programmatic way of creating a repo, deleting it (`⚠️ caution`), pushing a
single file to a repo or listing models from the Hub, you'll find helpers in
`hf_api.py`. Some example functionality available with the `HfApi` class:

* `set_access_token()`
* `unset_access_token()`
* `whoami()`
* `create_repo()`
* `list_repo_files()`
Expand Down
6 changes: 1 addition & 5 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from typing import TYPE_CHECKING


__version__ = "0.13.0.dev0"
__version__ = "0.14.0.dev0"

# Alphabetical order of definitions is ensured in tests
# WARNING: any comment added in this dictionary definition will be lost when
Expand Down Expand Up @@ -155,10 +155,8 @@
"repo_type_and_id_from_hf_id",
"request_space_hardware",
"restart_space",
"set_access_token",
"space_info",
"unlike",
"unset_access_token",
"update_repo_visibility",
"upload_file",
"upload_folder",
Expand Down Expand Up @@ -414,10 +412,8 @@ def __dir__():
repo_type_and_id_from_hf_id, # noqa: F401
request_space_hardware, # noqa: F401
restart_space, # noqa: F401
set_access_token, # noqa: F401
space_info, # noqa: F401
unlike, # noqa: F401
unset_access_token, # noqa: F401
update_repo_visibility, # noqa: F401
upload_file, # noqa: F401
upload_folder, # noqa: F401
Expand Down
9 changes: 0 additions & 9 deletions src/huggingface_hub/_commit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
validate_hf_hub_args,
)
from .utils import tqdm as hf_tqdm
from .utils._deprecation import _deprecate_method
from .utils._typing import Literal


Expand Down Expand Up @@ -130,14 +129,6 @@ def __post_init__(self) -> None:
else:
self.upload_info = UploadInfo.from_fileobj(self.path_or_fileobj)

@_deprecate_method(version="0.14", message="Operation is validated at initialization.")
def validate(self) -> None:
pass

@_deprecate_method(version="0.14", message="Use `upload_info` property instead.")
def _upload_info(self) -> UploadInfo:
return self.upload_info

@contextmanager
def as_file(self, with_tqdm: bool = False) -> Iterator[BinaryIO]:
"""
Expand Down
8 changes: 1 addition & 7 deletions src/huggingface_hub/_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os
import subprocess
from getpass import getpass
from typing import List, Optional
from typing import Optional

from .commands._cli_utils import ANSI
from .commands.delete_cache import _ask_for_confirmation_no_tui
Expand All @@ -30,7 +30,6 @@
set_git_credential,
unset_git_credential,
)
from .utils._deprecation import _deprecate_method


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -295,8 +294,3 @@ def _set_store_as_git_credential_helper_globally() -> None:
run_subprocess("git config --global credential.helper store")
except subprocess.CalledProcessError as exc:
raise EnvironmentError(exc.stderr)


@_deprecate_method(version="0.14", message="Please use `list_credential_helpers` instead.")
def _currently_setup_credential_helpers(directory: Optional[str] = None) -> List[str]:
return list_credential_helpers(directory)
3 changes: 0 additions & 3 deletions src/huggingface_hub/commands/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
logout,
notebook_login,
)
from .._login import (
_currently_setup_credential_helpers as currently_setup_credential_helpers, # noqa: F401 # for backward compatibility
)
from ..utils import HfFolder
from ._cli_utils import ANSI

Expand Down
40 changes: 0 additions & 40 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,15 @@
HfFolder,
HfHubHTTPError,
build_hf_headers,
erase_from_credential_store,
filter_repo_objects,
hf_raise_for_status,
logging,
parse_datetime,
read_from_credential_store,
validate_hf_hub_args,
write_to_credential_store,
)
from .utils._deprecation import (
_deprecate_arguments,
_deprecate_list_output,
_deprecate_method,
)
from .utils._pagination import paginate
from .utils._typing import Literal, TypedDict
Expand Down Expand Up @@ -857,39 +853,6 @@ def _is_valid_token(self, token: str) -> bool:
except HTTPError:
return False

@staticmethod
@_deprecate_method(
version="0.14",
message=(
"`HfApi.set_access_token` is deprecated as it is very ambiguous. Use"
" `login` or `set_git_credential` instead."
),
)
def set_access_token(access_token: str):
"""
Saves the passed access token so git can correctly authenticate the
user.

Args:
access_token (`str`):
The access token to save.
"""
write_to_credential_store(USERNAME_PLACEHOLDER, access_token)

@staticmethod
@_deprecate_method(
version="0.14",
message=(
"`HfApi.unset_access_token` is deprecated as it is very ambiguous. Use"
" `login` or `unset_git_credential` instead."
),
)
def unset_access_token():
"""
Resets the user's access token.
"""
erase_from_credential_store(USERNAME_PLACEHOLDER)

def get_model_tags(self) -> ModelTags:
"Gets all valid model tags as a nested namespace object"
path = f"{self.endpoint}/api/models-tags-by-type"
Expand Down Expand Up @@ -4304,9 +4267,6 @@ def _parse_revision_from_pr_url(pr_url: str) -> str:

api = HfApi()

set_access_token = api.set_access_token
unset_access_token = api.unset_access_token

whoami = api.whoami

list_models = api.list_models
Expand Down
9 changes: 1 addition & 8 deletions src/huggingface_hub/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@
hf_raise_for_status,
)
from ._fixes import SoftTemporaryDirectory, yaml_dump
from ._git_credential import (
erase_from_credential_store,
list_credential_helpers,
read_from_credential_store,
set_git_credential,
unset_git_credential,
write_to_credential_store,
)
from ._git_credential import list_credential_helpers, set_git_credential, unset_git_credential
from ._headers import build_hf_headers, get_token_to_send
from ._hf_folder import HfFolder
from ._http import http_backoff
Expand Down
81 changes: 1 addition & 80 deletions src/huggingface_hub/utils/_git_credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
# limitations under the License.
"""Contains utilities to manage Git credentials."""
import subprocess
from typing import List, Optional, Tuple, Union
from typing import List, Optional

from ..constants import ENDPOINT
from ._deprecation import _deprecate_method
from ._subprocess import run_interactive_subprocess, run_subprocess


Expand Down Expand Up @@ -95,81 +94,3 @@ def unset_git_credential(username: str = "hf_user", folder: Optional[str] = None

stdin.write(standard_input)
stdin.flush()


@_deprecate_method(
version="0.14",
message=(
"Please use `huggingface_hub.set_git_credential` instead as it allows"
" the user to chose which git-credential tool to use."
),
)
def write_to_credential_store(username: str, password: str) -> None:
with run_interactive_subprocess("git credential-store store") as (stdin, _):
input_username = f"username={username.lower()}"
input_password = f"password={password}"
stdin.write(f"url={ENDPOINT}\n{input_username}\n{input_password}\n\n")
stdin.flush()


@_deprecate_method(
version="0.14",
message="Please open an issue on https://github.com/huggingface/huggingface_hub if this a useful feature for you.",
)
def read_from_credential_store(
username: Optional[str] = None,
) -> Union[Tuple[str, str], Tuple[None, None]]:
"""
Reads the credential store relative to huggingface.co.

Args:
username (`str`, *optional*):
A username to filter to search. If not specified, the first entry under
`huggingface.co` endpoint is returned.

Returns:
`Tuple[str, str]` or `Tuple[None, None]`: either a username/password pair or
None/None if credential has not been found. The returned username is always
lowercase.
"""
with run_interactive_subprocess("git credential-store get") as (stdin, stdout):
standard_input = f"url={ENDPOINT}\n"
if username is not None:
standard_input += f"username={username.lower()}\n"
standard_input += "\n"

stdin.write(standard_input)
stdin.flush()
output = stdout.read()

if len(output) == 0:
return None, None

username, password = [line for line in output.split("\n") if len(line) != 0]
return username.split("=")[1], password.split("=")[1]


@_deprecate_method(
version="0.14",
message=(
"Please use `huggingface_hub.unset_git_credential` instead as it allows"
" the user to chose which git-credential tool to use."
),
)
def erase_from_credential_store(username: Optional[str] = None) -> None:
"""
Erases the credential store relative to huggingface.co.

Args:
username (`str`, *optional*):
A username to filter to search. If not specified, all entries under
`huggingface.co` endpoint is erased.
"""
with run_interactive_subprocess("git credential-store erase") as (stdin, _):
standard_input = f"url={ENDPOINT}\n"
if username is not None:
standard_input += f"username={username.lower()}\n"
standard_input += "\n"

stdin.write(standard_input)
stdin.flush()
21 changes: 3 additions & 18 deletions tests/test_cache_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
from huggingface_hub.utils import SoftTemporaryDirectory, logging
from huggingface_hub.utils._errors import EntryNotFoundError

from .testing_constants import ENDPOINT_STAGING, TOKEN, USER
from .testing_constants import ENDPOINT_STAGING, TOKEN
from .testing_utils import (
expect_deprecation,
repo_name,
with_production_testing,
xfail_on_windows,
Expand Down Expand Up @@ -281,25 +280,11 @@ def test_file_downloaded_in_cache_several_revisions(self):
class ReferenceUpdates(unittest.TestCase):
_api = HfApi(endpoint=ENDPOINT_STAGING, token=TOKEN)

@classmethod
@expect_deprecation("set_access_token")
def setUpClass(cls):
"""
Share this valid token in all tests below.
"""
cls._token = TOKEN
cls._api.set_access_token(TOKEN)

def test_update_reference(self):
repo_id = f"{USER}/{repo_name()}"
self._api.create_repo(repo_id, exist_ok=True)
repo_id = self._api.create_repo(repo_name(), exist_ok=True).repo_id

try:
self._api.upload_file(
path_or_fileobj=BytesIO(b"Some string"),
path_in_repo="file.txt",
repo_id=repo_id,
)
self._api.upload_file(path_or_fileobj=BytesIO(b"Some string"), path_in_repo="file.txt", repo_id=repo_id)

with SoftTemporaryDirectory() as cache:
hf_hub_download(repo_id, "file.txt", cache_dir=cache)
Expand Down
Loading