[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2026-05-09 00:02:33 +00:00
commit 200822cf93
4 changed files with 71 additions and 77 deletions

View file

@ -59,9 +59,7 @@ def has_def(src: str, name: str, kind: str = "any") -> bool:
rf"^\s*(?:async\s+)?def\s+{re.escape(name)}\b", src, re.MULTILINE
):
return True
if kind == "any" and re.search(
rf"^\s*{re.escape(name)}\s*[:=]", src, re.MULTILINE
):
if kind == "any" and re.search(rf"^\s*{re.escape(name)}\s*[:=]", src, re.MULTILINE):
return True
return False

View file

@ -259,9 +259,7 @@ def test_peft_lora_model_create_and_replace(tag: str):
src = fetch_text("huggingface/peft", tag, "src/peft/tuners/lora/model.py")
if src is None:
pytest.skip(f"{tag}: src/peft/tuners/lora/model.py missing")
assert has_def(src, "LoraModel", "class"), (
f"{tag}: class LoraModel missing"
)
assert has_def(src, "LoraModel", "class"), f"{tag}: class LoraModel missing"
assert has_def(src, "_create_and_replace", "func"), (
f"{tag}: LoraModel._create_and_replace missing; "
f"unsloth/models/loader.py:1535-1601 monkey-patch breaks (unsloth#4807)"
@ -284,11 +282,12 @@ def test_peft_transformers_weight_conversion_module(tag: str):
]
hit = first_match("huggingface/peft", tag, candidates)
if hit is None:
pytest.skip(
f"{tag}: transformers_weight_conversion not present (legacy peft)"
)
pytest.skip(f"{tag}: transformers_weight_conversion not present (legacy peft)")
_, src = hit
assert has_def(src, "build_peft_weight_mapping", "func") or "build_peft_weight_mapping" in src, (
assert (
has_def(src, "build_peft_weight_mapping", "func")
or "build_peft_weight_mapping" in src
), (
f"{tag}: build_peft_weight_mapping missing in transformers_weight_conversion; "
f"unsloth/import_fixes.py:1375-1456 wrap breaks (unsloth#5167)"
)
@ -307,11 +306,14 @@ def test_peft_integrations_dequantize_module_weight(tag: str):
"src/peft/utils/integrations/__init__.py",
]
hit = first_match("huggingface/peft", tag, candidates)
assert hit is not None, (
f"{tag}: src/peft/utils/integrations[.py|/__init__.py] both missing"
)
assert (
hit is not None
), f"{tag}: src/peft/utils/integrations[.py|/__init__.py] both missing"
_, src = hit
assert has_def(src, "dequantize_module_weight", "func") or "dequantize_module_weight" in src, (
assert (
has_def(src, "dequantize_module_weight", "func")
or "dequantize_module_weight" in src
), (
f"{tag}: peft.utils.integrations.dequantize_module_weight missing; "
f"unsloth-zoo vllm_utils.py:2701, unsloth/_utils.py:1550, "
f"saving_utils.py:270 ImportError"
@ -382,9 +384,9 @@ def test_peft_peft_model_from_pretrained_signature(tag: str):
assert src is not None, f"{tag}: src/peft/peft_model.py missing"
# We expect `def from_pretrained` in PeftModel class. Just check
# the method name exists; full kwarg list is too brittle.
assert has_def(src, "from_pretrained", "func"), (
f"{tag}: PeftModel.from_pretrained missing in peft_model.py"
)
assert has_def(
src, "from_pretrained", "func"
), f"{tag}: PeftModel.from_pretrained missing in peft_model.py"
# -------------------------------------------------------------------------
@ -409,6 +411,6 @@ def test_peft_version_parseable(tag: str):
)
and re.search(r"^\s*__version__\s*=\s*version\s*\(", src, re.MULTILINE)
)
assert has_literal or has_subimport or has_metadata, (
f"{tag}: peft.__version__ not exported via any known mechanism"
)
assert (
has_literal or has_subimport or has_metadata
), f"{tag}: peft.__version__ not exported via any known mechanism"

View file

@ -68,9 +68,9 @@ def test_trainer_class_importable_path(tag: str):
or src/transformers/trainer/__init__.py."""
candidates = ["src/transformers/trainer.py", "src/transformers/trainer/__init__.py"]
hit = first_match("huggingface/transformers", tag, candidates)
assert hit is not None, (
f"{tag}: src/transformers/trainer[.py|/__init__.py] both missing"
)
assert (
hit is not None
), f"{tag}: src/transformers/trainer[.py|/__init__.py] both missing"
_, src = hit
assert has_def(src, "Trainer", "class"), f"{tag}: class Trainer missing"
@ -84,9 +84,7 @@ def test_trainer_compute_loss_num_items_in_batch_param(tag: str):
assert hit is not None
_, src = hit
# Find the compute_loss signature - it's a class method, indented.
m = re.search(
r"^\s*def compute_loss\(([^)]*)\)", src, re.MULTILINE | re.DOTALL
)
m = re.search(r"^\s*def compute_loss\(([^)]*)\)", src, re.MULTILINE | re.DOTALL)
if m is None:
pytest.fail(f"{tag}: Trainer.compute_loss not found in source")
assert "num_items_in_batch" in m.group(1), (
@ -130,9 +128,9 @@ def test_trainer_get_batch_samples_returns_num_items(tag: str):
_, src = hit
if not has_def(src, "get_batch_samples", "func"):
pytest.skip(f"{tag}: get_batch_samples not yet on Trainer")
assert "num_items_in_batch" in src, (
f"{tag}: Trainer.get_batch_samples / num_items_in_batch contract missing"
)
assert (
"num_items_in_batch" in src
), f"{tag}: Trainer.get_batch_samples / num_items_in_batch contract missing"
@pytest.mark.parametrize("tag", TRANSFORMERS_TAGS)
@ -167,7 +165,9 @@ def test_modeling_utils_exposes_checkpoint(tag: str):
"""unsloth-zoo#549: transformers 5.2+ uses `transformers.modeling_utils.checkpoint`
(alias for torch.utils.checkpoint.checkpoint). Patch must replace
the transformers reference, not just torch's."""
src = fetch_text("huggingface/transformers", tag, "src/transformers/modeling_utils.py")
src = fetch_text(
"huggingface/transformers", tag, "src/transformers/modeling_utils.py"
)
if src is None:
pytest.skip(f"{tag}: modeling_utils.py missing")
# Either a direct import or local rebinding.
@ -190,13 +190,13 @@ def test_modeling_utils_exposes_checkpoint(tag: str):
def test_pushtohubmixin_create_repo_status(tag: str):
"""unsloth-zoo#393: transformers 5.x removed PushToHubMixin._create_repo.
On 4.x present, on 5.x absent. Snapshot which side."""
src = fetch_text("huggingface/transformers", tag, "src/transformers/modeling_utils.py")
src = fetch_text(
"huggingface/transformers", tag, "src/transformers/modeling_utils.py"
)
if src is None:
pytest.skip(f"{tag}: modeling_utils.py missing")
# Just record the presence; either is OK as long as we know.
has_create = bool(
re.search(r"def _create_repo\b", src) or "_create_repo" in src
)
has_create = bool(re.search(r"def _create_repo\b", src) or "_create_repo" in src)
# Informational only — both branches are tracked.
_ = has_create
@ -213,9 +213,9 @@ def test_integrations_bitsandbytes_module_present(tag: str):
)
if src is None:
pytest.skip(f"{tag}: integrations/bitsandbytes.py missing (legacy layout)")
assert "Linear4bit" in src or "linear" in src.lower(), (
f"{tag}: integrations/bitsandbytes.py has no Linear4bit reference"
)
assert (
"Linear4bit" in src or "linear" in src.lower()
), f"{tag}: integrations/bitsandbytes.py has no Linear4bit reference"
@pytest.mark.parametrize("tag", TRANSFORMERS_TAGS)
@ -224,7 +224,9 @@ def test_quantizers_should_convert_module_signature(tag: str):
quantizers_utils.should_convert_module(full_name, patterns).
Snapshot whether function exists and its substring-match form."""
src = fetch_text(
"huggingface/transformers", tag, "src/transformers/quantizers/quantizers_utils.py"
"huggingface/transformers",
tag,
"src/transformers/quantizers/quantizers_utils.py",
)
if src is None:
pytest.skip(f"{tag}: quantizers/quantizers_utils.py missing")
@ -233,7 +235,7 @@ def test_quantizers_should_convert_module_signature(tag: str):
# The bug we want to catch: substring matching uses `.{key}.` in
# `.{full_name}.` form. Patch only fires when this substring is
# in source AND mismatch behaviour exists.
has_dot_form = ".{key}." in src or "f'.{key}.'" in src or "f\".{key}.\"" in src
has_dot_form = ".{key}." in src or "f'.{key}.'" in src or 'f".{key}."' in src
# Informational only.
_ = has_dot_form
@ -258,9 +260,9 @@ def test_fp8linear_init_param_names(tag: str):
pytest.skip(f"{tag}: FP8Linear not yet defined")
has_bias_kw = re.search(r"def __init__\([^)]*\bbias\b", src) is not None
has_has_bias_kw = re.search(r"def __init__\([^)]*\bhas_bias\b", src) is not None
assert has_bias_kw or has_has_bias_kw, (
f"{tag}: FP8Linear.__init__ has neither `bias` nor `has_bias` param"
)
assert (
has_bias_kw or has_has_bias_kw
), f"{tag}: FP8Linear.__init__ has neither `bias` nor `has_bias` param"
# =========================================================================
@ -277,10 +279,7 @@ def test_processing_utils_unpack_importable(tag: str):
)
if src is None:
pytest.skip(f"{tag}: processing_utils.py missing")
has_unpack = bool(
re.search(r"^Unpack\b\s*=", src, re.MULTILINE)
or "Unpack" in src
)
has_unpack = bool(re.search(r"^Unpack\b\s*=", src, re.MULTILINE) or "Unpack" in src)
assert has_unpack, (
f"{tag}: transformers.processing_utils.Unpack missing; "
f"unsloth-zoo#583/584 import guard breaks"
@ -301,9 +300,9 @@ def test_gemma3_attention_forward_present(tag: str):
)
if src is None:
pytest.skip(f"{tag}: modeling_gemma3.py missing")
assert has_def(src, "Gemma3Attention", "class"), (
f"{tag}: class Gemma3Attention missing"
)
assert has_def(
src, "Gemma3Attention", "class"
), f"{tag}: class Gemma3Attention missing"
@pytest.mark.parametrize("tag", TRANSFORMERS_TAGS)
@ -315,9 +314,7 @@ def test_gpt_oss_model_forward_present(tag: str):
)
if src is None:
pytest.skip(f"{tag}: modeling_gpt_oss.py missing (legacy)")
assert has_def(src, "GptOssModel", "class"), (
f"{tag}: class GptOssModel missing"
)
assert has_def(src, "GptOssModel", "class"), f"{tag}: class GptOssModel missing"
# =========================================================================
@ -391,9 +388,9 @@ def test_apply_chat_template_signature_present(tag: str):
)
if src is None:
pytest.skip(f"{tag}: tokenization_utils_base.py missing")
assert has_def(src, "apply_chat_template", "func"), (
f"{tag}: apply_chat_template missing in tokenization_utils_base.py"
)
assert has_def(
src, "apply_chat_template", "func"
), f"{tag}: apply_chat_template missing in tokenization_utils_base.py"
# =========================================================================
@ -413,9 +410,9 @@ def test_modeling_attn_mask_utils_symbols(tag: str):
)
if src is None:
pytest.skip(f"{tag}: modeling_attn_mask_utils.py missing")
assert has_def(src, "AttentionMaskConverter", "class"), (
f"{tag}: AttentionMaskConverter missing"
)
assert has_def(
src, "AttentionMaskConverter", "class"
), f"{tag}: AttentionMaskConverter missing"
# _prepare_4d_attention_mask_for_sdpa is a function we hard-import.
assert (
has_def(src, "_prepare_4d_attention_mask_for_sdpa", "func")
@ -430,9 +427,9 @@ def test_cache_utils_classes(tag: str):
pytest.skip(f"{tag}: cache_utils.py missing")
needed = ("Cache", "DynamicCache")
for cls in needed:
assert has_def(src, cls, "class"), (
f"{tag}: transformers.cache_utils.{cls} missing"
)
assert has_def(
src, cls, "class"
), f"{tag}: transformers.cache_utils.{cls} missing"
@pytest.mark.parametrize("tag", TRANSFORMERS_TAGS)

View file

@ -79,7 +79,7 @@ TRL_TAGS = [
"v0.28.0",
"v0.29.0",
"v0.29.1",
"v1.0.0", # anchor
"v1.0.0", # anchor
"v1.1.0",
"v1.2.0",
"v1.3.0",
@ -346,9 +346,9 @@ def test_trl_sft_trainer_module_internals(tag: str):
f"{tag}: trl/trainer/sft_trainer.py missing; "
f"unsloth/tokenizer_utils.py:1538 wildcard import fails"
)
assert has_def(src, "SFTTrainer", "class"), (
f"{tag}: class SFTTrainer missing in sft_trainer.py"
)
assert has_def(
src, "SFTTrainer", "class"
), f"{tag}: class SFTTrainer missing in sft_trainer.py"
# neftune_post_forward_hook: optional (TRL removed it in some
# versions); soft-imported in tokenizer_utils.py:1542. Don't fail.
if "neftune_post_forward_hook" not in src:
@ -368,9 +368,9 @@ def test_trl_dpo_trainer_module_exists(tag: str):
f"{tag}: trl/trainer/dpo_trainer.py missing; "
f"unsloth-zoo/temporary_patches/misc.py:1376 import fails"
)
assert has_def(src, "DPOTrainer", "class"), (
f"{tag}: class DPOTrainer missing in dpo_trainer.py"
)
assert has_def(
src, "DPOTrainer", "class"
), f"{tag}: class DPOTrainer missing in dpo_trainer.py"
# -------------------------------------------------------------------------
@ -414,6 +414,7 @@ def test_trl_models_utils_disable_gradient_checkpointing(tag: str):
# Strip leading 'v' and parse.
try:
from packaging.version import Version
require = Version(tag.lstrip("v")) >= Version("1.0.0")
except Exception:
require = False
@ -466,9 +467,7 @@ def test_trl_import_utils_available_pattern(tag: str):
@pytest.mark.parametrize("tag", TRL_TAGS)
def test_trl_openenv_utils_generators(tag: str):
src = fetch_text(
"huggingface/trl", tag, "trl/experimental/openenv/utils.py"
)
src = fetch_text("huggingface/trl", tag, "trl/experimental/openenv/utils.py")
if src is None:
pytest.skip(f"{tag}: openenv.utils not present (gated optional)")
legacy = "generate_rollout_completions" in src
@ -592,9 +591,7 @@ def test_trl_sft_trainer_class(tag: str):
which is also fine."""
src = fetch_text("huggingface/trl", tag, "trl/trainer/sft_trainer.py")
assert src is not None
assert has_def(src, "SFTTrainer", "class"), (
f"{tag}: class SFTTrainer missing"
)
assert has_def(src, "SFTTrainer", "class"), f"{tag}: class SFTTrainer missing"
# -------------------------------------------------------------------------
@ -619,9 +616,9 @@ def test_trl_dpo_trainer_methods(tag: str):
src = fetch_text("huggingface/trl", tag, "trl/trainer/dpo_trainer.py")
assert src is not None
# The DPO class itself must always exist.
assert has_def(src, "DPOTrainer", "class"), (
f"{tag}: class DPOTrainer missing in dpo_trainer.py"
)
assert has_def(
src, "DPOTrainer", "class"
), f"{tag}: class DPOTrainer missing in dpo_trainer.py"
# Informational only -- pass either way:
for method in (
"concatenated_inputs",