Skip to content

Commit 6969b5f

Browse files
committed
Explicit true value for environment variables (#1081)
* explicit true value for environ variables * flake8 * fix test
1 parent 6e6333f commit 6969b5f

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

docs/source/how-to-cache.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ or to run Python as an administrator.
126126

127127
When symlinks are not supported, a warning message is displayed to the user to alert
128128
them they are using a degraded version of the cache-system. This warning can be disabled
129-
by setting the `DISABLE_SYMLINKS_WARNING` environment variable to true.
129+
by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable to true.
130130

131131
## Scan your cache
132132

src/huggingface_hub/constants.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,21 @@ def _is_true_or_auto(value: Optional[str]) -> bool:
8686

8787
# Here, `True` will disable progress bars globally without possibility of enabling it
8888
# programatically. `False` will enable them without possibility of disabling them.
89-
# If environement variable is not set (None), then the user is free to enable/disable
89+
# If environment variable is not set (None), then the user is free to enable/disable
9090
# them programmatically.
9191
# TL;DR: env variable has priority over code
9292
HF_HUB_DISABLE_PROGRESS_BARS: Optional[bool] = os.environ.get(
9393
"HF_HUB_DISABLE_PROGRESS_BARS"
9494
)
9595
if HF_HUB_DISABLE_PROGRESS_BARS is not None:
9696
HF_HUB_DISABLE_PROGRESS_BARS = _is_true(HF_HUB_DISABLE_PROGRESS_BARS)
97+
98+
# Disable warning on machines that do not support symlinks (e.g. Windows non-developer)
99+
HF_HUB_DISABLE_SYMLINKS_WARNING: bool = _is_true(
100+
os.environ.get("HF_HUB_DISABLE_SYMLINKS_WARNING")
101+
)
102+
103+
# Disable sending the cached token by default is all HTTP requests to the Hub
104+
HF_HUB_DISABLE_IMPLICIT_TOKEN: bool = _is_true(
105+
os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN")
106+
)

src/huggingface_hub/file_download.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from . import __version__ # noqa: F401 # for backward compatibility
2424
from .constants import (
2525
DEFAULT_REVISION,
26+
HF_HUB_DISABLE_SYMLINKS_WARNING,
2627
HUGGINGFACE_CO_URL_TEMPLATE,
2728
HUGGINGFACE_HEADER_X_LINKED_ETAG,
2829
HUGGINGFACE_HEADER_X_REPO_COMMIT,
@@ -100,15 +101,15 @@ def are_symlinks_supported(cache_dir: Union[str, Path, None] = None) -> bool:
100101
# Likely running on Windows
101102
_are_symlinks_supported_in_dir[cache_dir] = False
102103

103-
if not os.environ.get("DISABLE_SYMLINKS_WARNING"):
104+
if not HF_HUB_DISABLE_SYMLINKS_WARNING:
104105
message = (
105106
"`huggingface_hub` cache-system uses symlinks by default to"
106107
" efficiently store duplicated files but your machine does not"
107108
f" support them in {cache_dir}. Caching files will still work"
108109
" but in a degraded version that might require more space on"
109110
" your disk. This warning can be disabled by setting the"
110-
" `DISABLE_SYMLINKS_WARNING` environment variable. For more"
111-
" details, see"
111+
" `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For"
112+
" more details, see"
112113
" https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations."
113114
)
114115
if os.name == "nt":

src/huggingface_hub/utils/_headers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Contains utilities to handle headers to send in calls to Huggingface Hub."""
16-
import os
1716
from typing import Dict, Optional, Union
1817

18+
from ..constants import HF_HUB_DISABLE_IMPLICIT_TOKEN
1919
from ._hf_folder import HfFolder
2020
from ._runtime import (
2121
get_fastai_version,
@@ -153,7 +153,7 @@ def _get_token_to_send(use_auth_token: Optional[Union[bool, str]]) -> Optional[s
153153
return cached_token
154154

155155
# Case implicit use of the token is forbidden by env variable
156-
if os.environ.get("HF_HUB_DISABLE_IMPLICIT_TOKEN"):
156+
if HF_HUB_DISABLE_IMPLICIT_TOKEN:
157157
return None
158158

159159
# Otherwise: we use the cached token as the user has not explicitly forbidden it

tests/test_utils_headers.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,21 @@ def test_write_action_use_auth_token_false(self) -> None:
5959
with self.assertRaises(ValueError):
6060
build_hf_headers(use_auth_token=False, is_write_action=True)
6161

62-
@patch.dict("os.environ", {"HF_HUB_DISABLE_IMPLICIT_TOKEN": "1"})
6362
def test_implicit_use_disabled(self, mock_HfFolder: Mock) -> None:
64-
mock_HfFolder().get_token.return_value = FAKE_TOKEN
65-
self.assertEqual(build_hf_headers(), NO_AUTH_HEADER) # token is not sent
63+
with patch( # not as decorator to avoid friction with @handle_injection
64+
"huggingface_hub.utils._headers.HF_HUB_DISABLE_IMPLICIT_TOKEN", True
65+
):
66+
mock_HfFolder().get_token.return_value = FAKE_TOKEN
67+
self.assertEqual(build_hf_headers(), NO_AUTH_HEADER) # token is not sent
6668

67-
@patch.dict("os.environ", {"HF_HUB_DISABLE_IMPLICIT_TOKEN": "1"})
6869
def test_implicit_use_disabled_but_explicit_use(self, mock_HfFolder: Mock) -> None:
69-
mock_HfFolder().get_token.return_value = FAKE_TOKEN
70+
with patch( # not as decorator to avoid friction with @handle_injection
71+
"huggingface_hub.utils._headers.HF_HUB_DISABLE_IMPLICIT_TOKEN", True
72+
):
73+
mock_HfFolder().get_token.return_value = FAKE_TOKEN
7074

71-
# This is not an implicit use so we still send it
72-
self.assertEqual(build_hf_headers(use_auth_token=True), FAKE_TOKEN_HEADER)
75+
# This is not an implicit use so we still send it
76+
self.assertEqual(build_hf_headers(use_auth_token=True), FAKE_TOKEN_HEADER)
7377

7478

7579
class TestUserAgentHeadersUtil(unittest.TestCase):

0 commit comments

Comments
 (0)