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

Align HfFolder.get_token with huggingface_hub.get_token #1966

Merged
merged 2 commits into from
Jan 11, 2024
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
27 changes: 4 additions & 23 deletions src/huggingface_hub/utils/_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Contain helper class to retrieve/store token from/to local cache."""
import os
import warnings
from pathlib import Path
from typing import Optional

from .. import constants
from ._token import get_token


class HfFolder:
Expand Down Expand Up @@ -50,12 +50,8 @@ def get_token(cls) -> Optional[str]:
"""
Get token or None if not existent.

Note that a token can be also provided using the `HF_TOKEN` environment variable.

Token is saved in the huggingface home folder. You can configure it by setting
the `HF_HOME` environment variable. Previous location was `~/.huggingface/token`.
If token is found in old location but not in new location, it is copied there first.
For more details, see https://github.com/huggingface/huggingface_hub/issues/1232.
This method is deprecated in favor of [`huggingface_hub.get_token`] but is kept for backward compatibility.
Its behavior is the same as [`huggingface_hub.get_token`].

Returns:
`str` or `None`: The token, `None` if it doesn't exist.
Expand All @@ -66,22 +62,7 @@ def get_token(cls) -> Optional[str]:
except Exception: # if not possible (e.g. PermissionError), do not raise
pass

# 1. Is it set by environment variable ?
token: Optional[str] = os.environ.get("HF_TOKEN")
if token is None: # Ensure backward compatibility but doesn't have priority
token = os.environ.get("HUGGING_FACE_HUB_TOKEN")
if token is not None:
token = token.replace("\r", "").replace("\n", "").strip()
if token != "":
return token

# 2. Is it set in token path ?
try:
token = cls.path_token.read_text()
token = token.replace("\r", "").replace("\n", "").strip()
return token
except FileNotFoundError:
return None
return get_token()

# TODO: deprecate when adapted in transformers/datasets/gradio
# @_deprecate_method(version="1.0", message="Use `huggingface_hub.logout` instead.")
Expand Down
44 changes: 1 addition & 43 deletions tests/test_utils_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
"""Contain tests for `HfFolder` utility."""
import os
import unittest
from pathlib import Path
from unittest.mock import patch
from uuid import uuid4

from huggingface_hub.utils import HfFolder, SoftTemporaryDirectory
from huggingface_hub.utils import HfFolder


def _generate_token() -> str:
Expand All @@ -44,46 +42,6 @@ def test_token_workflow(self):
with unittest.mock.patch.dict(os.environ, {"HF_TOKEN": token}):
self.assertEqual(HfFolder.get_token(), token)

def test_token_in_old_path(self):
token = _generate_token()
token2 = _generate_token()
with SoftTemporaryDirectory() as tmpdir:
path_token = Path(tmpdir) / "new_token_path"
old_path_token = Path(tmpdir) / "old_path_token"

# Use dummy paths
new_patcher = patch.object(HfFolder, "path_token", path_token)
old_patcher = patch.object(HfFolder, "_old_path_token", old_path_token)
new_patcher.start()
old_patcher.start()

# Reads from old path -> works but warn
old_path_token.write_text(token)
with self.assertWarns(UserWarning):
self.assertEqual(HfFolder.get_token(), token)
# Old path still exists
self.assertEqual(old_path_token.read_text(), token)
# New path is created
self.assertEqual(path_token.read_text(), token)

# Delete -> works, doesn't warn, delete both paths
HfFolder.delete_token()
self.assertFalse(old_path_token.exists())
self.assertFalse(path_token.exists())

# Write -> only to new path
HfFolder.save_token(token)
self.assertFalse(old_path_token.exists())
self.assertEqual(path_token.read_text(), token)

# Read -> new path has priority. No warning message.
old_path_token.write_text(token2)
self.assertEqual(HfFolder.get_token(), token)

# Un-patch
new_patcher.stop()
old_patcher.stop()

def test_token_strip(self):
"""
Test the workflow when the token is mistakenly finishing with new-line or space character.
Expand Down
Loading