Repository: huggingface/huggingface_hub
Pull request #1638 (2023-09-05 08:08:51)

Fix bug #1634 (drop finishing spaces and EOL)

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) Report
Patch coverage: **`100.00%`** and project coverage change: **`-0.35%`** :warning:
> Comparison is base [(`94c427a`)](https://app.codecov.io/gh/huggingface/huggingface_hub/commit/94c427aa236ec43dfd1ef83e225e4ee8242b04e0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) 82.13% compared to head [(`3f67344`)](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) 81.78%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1638      +/-   ##
==========================================
- Coverage   82.13%   81.78%   -0.35%     
==========================================
  Files          60       60              
  Lines        6746     6749       +3     
==========================================
- Hits         5541     5520      -21     
- Misses       1205     1229      +24     
```


| [Files Changed](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface) | Coverage Δ | |
|---|---|---|
| [src/huggingface\_hub/utils/\_hf\_folder.py](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface#diff-c3JjL2h1Z2dpbmdmYWNlX2h1Yi91dGlscy9faGZfZm9sZGVyLnB5) | `95.45% <100.00%> (+0.33%)` | :arrow_up: |

... and [6 files with indirect coverage changes](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/huggingface/huggingface_hub/pull/1638?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=huggingface).


Patch:
diff --git a/src/huggingface_hub/utils/_hf_folder.py b/src/huggingface_hub/utils/_hf_folder.py
index 5c9f07c9..77daced7 100644
--- a/src/huggingface_hub/utils/_hf_folder.py
+++ b/src/huggingface_hub/utils/_hf_folder.py
@@ -65,11 +65,14 @@ class HfFolder:
         # 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
 


Test patch:
diff --git a/tests/test_utils_hf_folder.py b/tests/test_utils_hf_folder.py
index 7a1bcf19..ce0448ec 100644
--- a/tests/test_utils_hf_folder.py
+++ b/tests/test_utils_hf_folder.py
@@ -83,3 +83,12 @@ class HfFolderTest(unittest.TestCase):
             # 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()