From 00294fa2cbcf83188306efaf99314a631eba9c05 Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Thu, 11 Jan 2024 12:06:33 +0100 Subject: [PATCH] align HfFolder.get_token with hfh.get_token --- src/huggingface_hub/utils/_hf_folder.py | 27 +++------------ tests/test_utils_hf_folder.py | 44 +------------------------ 2 files changed, 5 insertions(+), 66 deletions(-) diff --git a/src/huggingface_hub/utils/_hf_folder.py b/src/huggingface_hub/utils/_hf_folder.py index eb091ee954..92954c8e77 100644 --- a/src/huggingface_hub/utils/_hf_folder.py +++ b/src/huggingface_hub/utils/_hf_folder.py @@ -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: @@ -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. @@ -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.") diff --git a/tests/test_utils_hf_folder.py b/tests/test_utils_hf_folder.py index c101ab0dee..6c9b6abb1a 100644 --- a/tests/test_utils_hf_folder.py +++ b/tests/test_utils_hf_folder.py @@ -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: @@ -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.