Skip to content

fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240

Open
shhKnight30 wants to merge 2 commits intohuggingface:mainfrom
shhKnight30:fix-type-checking-issue
Open

fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240
shhKnight30 wants to merge 2 commits intohuggingface:mainfrom
shhKnight30:fix-type-checking-issue

Conversation

@shhKnight30
Copy link
Copy Markdown

What does this PR do?

Type checking for PreTrainedConfig subclasses broke in v5.4.0 and this fixes it.

The culprit is wrap_init_to_accept_kwargs — it swaps out the dataclass-generated
__init__ with a (**kwargs: Any) wrapper at runtime. That's fine for execution,
but mypy and pyright never run your code. They just see the wrapper signature and
have no idea what arguments your config actually accepts, so they flag everything.

The fix is a single decorator: @dataclass_transform(kw_only_default=True) on
PreTrainedConfig. It's a static-only hint (PEP 681)
that tells type checkers to derive __init__ signatures from the declared fields
instead. No runtime behavior changes, no new dependencies — typing_extensions is
already in the tree. Every subclass picks this up automatically.

Pydantic, attrs, and SQLModel all use the same approach.


Reproduction

# test_config.py
from transformers import LlamaConfig
config = LlamaConfig(vocab_size=32000)

v5.4.0:
error: Unexpected keyword argument "vocab_size" for "LlamaConfig" [call-arg]

After this PR:
Success: no issues found in 1 source file


Why not a runtime fix?

I went down that road first — tried preserving __signature__, __wrapped__,
and __annotations__ on the wrapper. None of it worked because static analyzers
don't evaluate runtime assignments. @dataclass_transform exists precisely for
this situation and is the right tool here.


Changes

src/transformers/configuration_utils.py

  • Added @dataclass_transform(kw_only_default=True) to PreTrainedConfig
  • Removed the now-unused from inspect import signature import and original_signature variable

Anything that could break?

Nothing at runtime. The decorator is a no-op outside of type checkers.
mypy ≥ 1.2 and all current pyright versions support PEP 681.

Worth noting: vllm and axolotl have both pinned transformers<5.4.0 because
of this issue, so there's real downstream pressure to get it resolved.


Before submitting

  • Read the contributor guide
  • Linked to the relevant issue — v5.4.0 breaks PretrainedConfig type checking #45071, where @Rocketknight1 indicated openness to a fix
  • No docs update needed — this is an internal change with no user-facing API surface
  • No runtime tests added — mypy itself is the test. Happy to wire up a CI check if that's preferred

Fixes #45071

cc @Rocketknight1 @ArthurZucker @Cyrilvallez

shhKnight30 and others added 2 commits April 4, 2026 15:52
…ingface#45071)

Add @dataclass_transform (PEP 681) so type checkers can synthesize
__init__ signatures from dataclass fields. No runtime behavior change.
Same pattern used by pydantic and attrs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v5.4.0 breaks PretrainedConfig type checking

1 participant