Skip to content

Commit 2143229

Browse files
Fix minor things in ci (#3558)
* type hints * skip unix-specific tests on windows instead of expecting failure * typo --------- Co-authored-by: Celina Hanouti <[email protected]>
1 parent 9a1c4af commit 2143229

File tree

8 files changed

+25
-34
lines changed

8 files changed

+25
-34
lines changed

src/huggingface_hub/hf_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7609,7 +7609,7 @@ def create_inference_endpoint(
76097609
"type": type,
76107610
}
76117611
if scaling_metric:
7612-
payload["compute"]["scaling"]["measure"] = {scaling_metric: scaling_threshold}
7612+
payload["compute"]["scaling"]["measure"] = {scaling_metric: scaling_threshold} # type: ignore
76137613
if env:
76147614
payload["model"]["env"] = env
76157615
if secrets:

src/huggingface_hub/hf_file_system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def _open(
341341
revision: Optional[str] = None,
342342
block_size: Optional[int] = None,
343343
**kwargs,
344-
) -> "HfFileSystemFile":
344+
) -> Union["HfFileSystemFile", "HfFileSystemStreamFile"]:
345345
block_size = block_size if block_size is not None else self.block_size
346346
if block_size is not None:
347347
kwargs["block_size"] = block_size

src/huggingface_hub/inference/_mcp/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def format_result(result: "mcp_types.CallToolResult") -> str:
5757
elif item.type == "resource":
5858
resource = item.resource
5959

60-
if hasattr(resource, "text"):
60+
if hasattr(resource, "text") and isinstance(resource.text, str):
6161
formatted_parts.append(resource.text)
6262

63-
elif hasattr(resource, "blob"):
63+
elif hasattr(resource, "blob") and isinstance(resource.blob, str):
6464
formatted_parts.append(
6565
f"[Binary Content ({resource.uri}): {resource.mimeType}, {_get_base64_size(resource.blob)} bytes]\n"
6666
f"The task is complete and the content accessible to the User"

src/huggingface_hub/serialization/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def split_state_dict_into_shards_factory(
102102
continue
103103

104104
# If a `tensor` shares the same underlying storage as another tensor, we put `tensor` in the same `block`
105-
storage_id = get_storage_id(tensor)
105+
storage_id = get_storage_id(tensor) # type: ignore[invalid-argument-type]
106106
if storage_id is not None:
107107
if storage_id in storage_id_to_tensors:
108108
# We skip this tensor for now and will reassign to correct shard later
@@ -114,7 +114,7 @@ def split_state_dict_into_shards_factory(
114114
storage_id_to_tensors[storage_id] = [key]
115115

116116
# Compute tensor size
117-
tensor_size = get_storage_size(tensor)
117+
tensor_size = get_storage_size(tensor) # type: ignore[invalid-argument-type]
118118

119119
# If this tensor is bigger than the maximal size, we put it in its own shard
120120
if tensor_size > max_shard_size:

tests/test_cache_layout.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
from huggingface_hub.utils import SoftTemporaryDirectory, logging
99

1010
from .testing_constants import ENDPOINT_STAGING, TOKEN
11-
from .testing_utils import (
12-
repo_name,
13-
with_production_testing,
14-
xfail_on_windows,
15-
)
11+
from .testing_utils import repo_name, skip_on_windows, with_production_testing
1612

1713

1814
logger = logging.get_logger(__name__)
@@ -28,7 +24,7 @@ def get_file_contents(path):
2824

2925
@with_production_testing
3026
class CacheFileLayoutHfHubDownload(unittest.TestCase):
31-
@xfail_on_windows(reason="Symlinks are deactivated in Windows tests.")
27+
@skip_on_windows(reason="Symlinks are deactivated in Windows tests.")
3228
def test_file_downloaded_in_cache(self):
3329
for revision, expected_reference in (
3430
(None, "main"),
@@ -130,7 +126,7 @@ def test_file_download_happens_once(self):
130126

131127
self.assertEqual(creation_time_0, creation_time_1)
132128

133-
@xfail_on_windows(reason="Symlinks are deactivated in Windows tests.")
129+
@skip_on_windows(reason="Symlinks are deactivated in Windows tests.")
134130
def test_file_download_happens_once_intra_revision(self):
135131
# Tests that a file is only downloaded once if it's not updated, even across different revisions.
136132

@@ -145,7 +141,7 @@ def test_file_download_happens_once_intra_revision(self):
145141

146142
self.assertEqual(creation_time_0, creation_time_1)
147143

148-
@xfail_on_windows(reason="Symlinks are deactivated in Windows tests.")
144+
@skip_on_windows(reason="Symlinks are deactivated in Windows tests.")
149145
def test_multiple_refs_for_same_file(self):
150146
with SoftTemporaryDirectory() as cache:
151147
hf_hub_download(MODEL_IDENTIFIER, "file_0.txt", cache_dir=cache)
@@ -179,7 +175,7 @@ def test_multiple_refs_for_same_file(self):
179175

180176
@with_production_testing
181177
class CacheFileLayoutSnapshotDownload(unittest.TestCase):
182-
@xfail_on_windows(reason="Symlinks are deactivated in Windows tests.")
178+
@skip_on_windows(reason="Symlinks are deactivated in Windows tests.")
183179
def test_file_downloaded_in_cache(self):
184180
with SoftTemporaryDirectory() as cache:
185181
snapshot_download(MODEL_IDENTIFIER, cache_dir=cache)
@@ -210,7 +206,7 @@ def test_file_downloaded_in_cache(self):
210206

211207
self.assertTrue(all([os.path.isfile(link) for link in resolved_snapshot_links]))
212208

213-
@xfail_on_windows(reason="Symlinks are deactivated in Windows tests.")
209+
@skip_on_windows(reason="Symlinks are deactivated in Windows tests.")
214210
def test_file_downloaded_in_cache_several_revisions(self):
215211
with SoftTemporaryDirectory() as cache:
216212
snapshot_download(MODEL_IDENTIFIER, cache_dir=cache, revision="file-3")

tests/test_file_download.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
DUMMY_RENAMED_OLD_MODEL_ID,
5656
SAMPLE_DATASET_IDENTIFIER,
5757
repo_name,
58+
skip_on_windows,
5859
use_tmp_repo,
5960
with_production_testing,
60-
xfail_on_windows,
6161
)
6262

6363

@@ -224,7 +224,7 @@ def test_file_cached_and_read_only_access(self):
224224
# Set permission back for cleanup
225225
_recursive_chmod(tmpdir, 0o777)
226226

227-
@xfail_on_windows(reason="umask is UNIX-specific")
227+
@skip_on_windows(reason="umask is UNIX-specific")
228228
def test_hf_hub_download_custom_cache_permission(self):
229229
"""Checks `hf_hub_download` respect the cache dir permission.
230230
@@ -951,13 +951,13 @@ def test_hf_hub_url_on_awful_subfolder_and_filename(self):
951951
self.expected_resolve_url,
952952
)
953953

954-
@xfail_on_windows(reason="Windows paths cannot contain a '?'.")
954+
@skip_on_windows(reason="Windows paths cannot contain a '?'.")
955955
def test_hf_hub_download_on_awful_filepath(self):
956956
local_path = hf_hub_download(self.repo_url.repo_id, self.filepath, cache_dir=self.cache_dir)
957957
# Local path is not url-encoded
958958
self.assertTrue(local_path.endswith(self.filepath))
959959

960-
@xfail_on_windows(reason="Windows paths cannot contain a '?'.")
960+
@skip_on_windows(reason="Windows paths cannot contain a '?'.")
961961
def test_hf_hub_download_on_awful_subfolder_and_filename(self):
962962
local_path = hf_hub_download(
963963
self.repo_url.repo_id,
@@ -991,11 +991,11 @@ def setUpClass(cls):
991991
def tearDownClass(cls) -> None:
992992
cls.api.delete_repo(repo_id=cls.repo_id)
993993

994-
@xfail_on_windows(reason="Windows paths cannot contain '\\..\\'.", raises=ValueError)
994+
@skip_on_windows(reason="Windows paths cannot contain '\\..\\'.")
995995
def test_download_folder_file_in_cache_dir(self) -> None:
996996
hf_hub_download(self.repo_id, "folder/..\\..\\..\\file", cache_dir=self.cache_dir)
997997

998-
@xfail_on_windows(reason="Windows paths cannot contain '\\..\\'.", raises=ValueError)
998+
@skip_on_windows(reason="Windows paths cannot contain '\\..\\'.")
999999
def test_download_folder_file_to_local_dir(self) -> None:
10001000
with SoftTemporaryDirectory() as local_dir:
10011001
hf_hub_download(self.repo_id, "folder/..\\..\\..\\file", cache_dir=self.cache_dir, local_dir=local_dir)

tests/test_utils_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from huggingface_hub.utils import DeleteCacheStrategy, HFCacheInfo, _format_size, scan_cache_dir
1111
from huggingface_hub.utils._cache_manager import CacheNotFound, _try_delete_path
1212

13-
from .testing_utils import rmtree_with_retry, with_production_testing, xfail_on_windows
13+
from .testing_utils import rmtree_with_retry, skip_on_windows, with_production_testing
1414

1515

1616
# On production server to avoid recreating them all the time
@@ -372,7 +372,7 @@ def test_ref_to_missing_revision(self) -> None:
372372
+ f"({self.repo_path}).",
373373
)
374374

375-
@xfail_on_windows("Last modified/last accessed work a bit differently on Windows.")
375+
@skip_on_windows("Last modified/last accessed work a bit differently on Windows.")
376376
def test_scan_cache_last_modified_and_last_accessed(self) -> None:
377377
"""Scan the last_modified and last_accessed properties when scanning."""
378378
TIME_GAP = 0.1
@@ -697,7 +697,7 @@ def test_delete_path_on_missing_folder(self) -> None:
697697
assert len(captured.output) > 0
698698
assert any(f"Couldn't delete TYPE: file not found ({dir_path})" in log for log in captured.output)
699699

700-
@xfail_on_windows(reason="Permissions are handled differently on Windows.")
700+
@skip_on_windows(reason="Permissions are handled differently on Windows.")
701701
def test_delete_path_on_local_folder_with_wrong_permission(self) -> None:
702702
"""Try to delete a local folder that is protected."""
703703
dir_path = self.cache_dir / "something"

tests/testing_utils.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,22 +302,17 @@ def _inner_test_function(*args, **kwargs):
302302
return _inner_decorator
303303

304304

305-
def xfail_on_windows(reason: str, raises: Optional[type[Exception]] = None):
305+
def skip_on_windows(reason: str):
306306
"""
307-
Decorator to flag tests that we expect to fail on Windows.
308-
309-
Will not raise an error if the expected error happens while running on Windows machine.
310-
If error is expected but does not happen, the test fails as well.
307+
Decorator to flag tests that we want to skip on Windows.
311308
312309
Args:
313310
reason (`str`):
314-
Reason why it should fail.
315-
raises (`type[Exception]`):
316-
The error type we except to happen.
311+
Reason to skip it.
317312
"""
318313

319314
def _inner_decorator(test_function: Callable) -> Callable:
320-
return pytest.mark.xfail(os.name == "nt", reason=reason, raises=raises, strict=True, run=True)(test_function)
315+
return pytest.mark.skipif(os.name == "nt", reason=reason)(test_function)
321316

322317
return _inner_decorator
323318

0 commit comments

Comments
 (0)