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

Fix bug #1634 (drop finishing spaces and EOL) #1638

Merged
merged 4 commits into from
Sep 5, 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
5 changes: 4 additions & 1 deletion src/huggingface_hub/utils/_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ def get_token(cls) -> Optional[str]:
# 1. Is it set by environment variable ?
token: Optional[str] = os.environ.get("HUGGING_FACE_HUB_TOKEN")
if token is not None:
token = token.replace("\r", "").replace("\n", "").strip()
return token

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

Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils_hf_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ def test_token_in_old_path(self):
# 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.
"""
token = _generate_token()
HfFolder.save_token(" " + token + "\n")
self.assertEqual(HfFolder.get_token(), token)
HfFolder.delete_token()