From b3d184bf2ff7aeb24f60c0efc05515aaab58120b Mon Sep 17 00:00:00 2001 From: narugo1992 Date: Wed, 22 Apr 2026 01:25:00 +0800 Subject: [PATCH] Make deep compat module import-portable across hf matrix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `huggingface_hub.errors` landed around v0.22. For v0.20.3 — which is still in the PR #18 CI matrix — those exceptions only live under `huggingface_hub.utils`. Switch to the utils path, which re-exports the same symbols on every version we target. `CommitOperationCopy` was added in v0.21, so v0.20.3 doesn't ship it either. Guard the top-level import with a try/except and skip the one copy-op test when the installed client is too old. No test content changed; this is a pure import-compat patch driven by the matrix run on commit 397b737. --- .../api/test_huggingface_hub_deep.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/kohakuhub/api/test_huggingface_hub_deep.py b/test/kohakuhub/api/test_huggingface_hub_deep.py index c1f64da..598e5aa 100644 --- a/test/kohakuhub/api/test_huggingface_hub_deep.py +++ b/test/kohakuhub/api/test_huggingface_hub_deep.py @@ -26,7 +26,6 @@ import httpx import pytest from huggingface_hub import ( CommitOperationAdd, - CommitOperationCopy, CommitOperationDelete, HfApi, HfFileSystem, @@ -34,12 +33,23 @@ from huggingface_hub import ( hf_hub_url, snapshot_download, ) -from huggingface_hub.errors import ( + +# `huggingface_hub.errors` landed around v0.22; v0.20.3 (still in the CI matrix) +# keeps these exceptions under `huggingface_hub.utils`. The utils path is the +# version-portable import that works against every client version we target. +from huggingface_hub.utils import ( EntryNotFoundError, HfHubHTTPError, RepositoryNotFoundError, + RevisionNotFoundError, ) +# `CommitOperationCopy` was added in v0.21.0 — older matrix pins do not ship it. +try: + from huggingface_hub import CommitOperationCopy # type: ignore[attr-defined] +except ImportError: # v0.20.3 and earlier + CommitOperationCopy = None # type: ignore[assignment] + # --------------------------------------------------------------------------- # Helpers @@ -589,6 +599,11 @@ async def test_create_commit_supports_copy_operation( ``api/commit/routers/operations.py:813``). Make sure the server-side code path is reachable via the huggingface_hub client shape. """ + if CommitOperationCopy is None: + pytest.skip( + "CommitOperationCopy was added in huggingface_hub 0.21; the " + "installed client is older." + ) api = _api(live_server_url, hf_api_token) repo_id = "owner/hf-deep-copy-op" await _run(api.create_repo, repo_id) @@ -709,8 +724,6 @@ async def test_repo_info_revision_not_found_raises_named_error( """transformers / diffusers catch ``RevisionNotFoundError`` specifically to provide a better error message. The backend must surface ``X-Error-Code: RevisionNotFound`` so huggingface_hub maps it.""" - from huggingface_hub.errors import RevisionNotFoundError - api = _api(live_server_url, hf_api_token) with pytest.raises(RevisionNotFoundError): await _run(api.repo_info, "owner/demo-model", revision="no-such-revision")