Skip to content

Commit

Permalink
align HfFolder.get_token with hfh.get_token (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin committed Jan 22, 2024
1 parent 0ed5a26 commit fe687e3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 66 deletions.
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

0 comments on commit fe687e3

Please sign in to comment.