From c0cefd88058cb18f5dd263f15b2051fc48f14f1d Mon Sep 17 00:00:00 2001 From: Nurlan Mammadli Date: Thu, 19 Nov 2026 09:55:27 -0500 Subject: [PATCH] fix: keep keyword-only kwargs in select_compatible_kwargs select_compatible_kwargs filtered on inspect.getfullargspec(...).args, which lists only positional-or-keyword parameters and does not follow functools.wraps. In huggingface_hub, hf_hub_download takes revision, local_files_only, token and the rest as keyword-only, so download_file_from_hf silently dropped all of them. Filter on inspect.signature(...).parameters and keep both positional-or-keyword and keyword-only names. Co-Authored-By: Claude Opus 5.5 --- tests/unit/utilities/test_hf_utils.py | 49 ++++++++++++++++++++++++++ transformer_lens/utilities/hf_utils.py | 9 ++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tests/unit/utilities/test_hf_utils.py b/tests/unit/utilities/test_hf_utils.py index d52a1a7e71..8eef08fb86 100644 --- a/tests/unit/utilities/test_hf_utils.py +++ b/tests/unit/utilities/test_hf_utils.py @@ -2,6 +2,7 @@ from __future__ import annotations +from functools import wraps from types import SimpleNamespace from typing import Any, List @@ -149,6 +150,32 @@ def test_idempotent(self) -> None: assert before is after +class TestSelectCompatibleKwargs: + def test_keeps_keyword_only_parameters(self) -> None: + def target(repo_id: str, *, revision: str, local_files_only: bool) -> None: + pass + + kwargs = {"repo_id": "any/repo", "revision": "abc", "local_files_only": True, "bogus": 1} + + assert hf_utils.select_compatible_kwargs(kwargs, target) == { + "repo_id": "any/repo", + "revision": "abc", + "local_files_only": True, + } + + def test_follows_wrapped_function_parameters(self) -> None: + def target(revision: str) -> str: + return revision + + @wraps(target) + def wrapped(*args: Any, **kwargs: Any) -> str: + return target(*args, **kwargs) + + assert hf_utils.select_compatible_kwargs({"revision": "abc", "bogus": 1}, wrapped) == { + "revision": "abc" + } + + class TestDownloadFileFromHf: """End-to-end coverage: download_file_from_hf must actually use the retry helper. @@ -180,6 +207,28 @@ def fake_hub_download(**kwargs: Any) -> str: assert state["calls"] == 2 assert len(_no_sleep) == 1 + def test_forwards_keyword_only_download_parameters( + self, monkeypatch: pytest.MonkeyPatch, tmp_path: Any + ) -> None: + fake_file = tmp_path / "data.json" + fake_file.write_text('{"ok": true}') + calls = [] + + def fake_hub_download( + *, revision: str | None = None, local_files_only: bool = False, **kwargs: Any + ) -> str: + calls.append((revision, local_files_only)) + return str(fake_file) + + monkeypatch.setattr(hf_utils, "hf_hub_download", fake_hub_download) + + result = hf_utils.download_file_from_hf( + "any/repo", "data.json", revision="abc", local_files_only=True + ) + + assert result == {"ok": True} + assert calls == [("abc", True)] + class TestRemotePostInitCompat: """4.x-era remote configs define argless __post_init__, which >=5.x calls diff --git a/transformer_lens/utilities/hf_utils.py b/transformer_lens/utilities/hf_utils.py index 0bc87f7cd0..a435c1147a 100644 --- a/transformer_lens/utilities/hf_utils.py +++ b/transformer_lens/utilities/hf_utils.py @@ -157,7 +157,14 @@ def get_rotary_pct_from_config(config: Any) -> float: def select_compatible_kwargs(kwargs_dict: Dict[str, Any], callable: Callable) -> Dict[str, Any]: """Return a dict with the elements kwargs_dict that are parameters of callable""" - return {k: v for k, v in kwargs_dict.items() if k in inspect.getfullargspec(callable).args} + parameters = inspect.signature(callable).parameters + return { + k: v + for k, v in kwargs_dict.items() + if k in parameters + and parameters[k].kind + in (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY) + } def download_file_from_hf(