Skip to content

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

Merged
zucchini-nlp merged 4 commits intohuggingface:mainfrom
shhKnight30:fix-type-checking-issue
Apr 10, 2026
Merged

fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240
zucchini-nlp merged 4 commits intohuggingface:mainfrom
shhKnight30:fix-type-checking-issue

Conversation

@shhKnight30
Copy link
Copy Markdown
Contributor

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

…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.
Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @shhKnight30 , thanks for the PR!

We are currently working on enable type checking with ty with @tarekziade . We'll be slowly adding more files to run the checker on, and configuration_utils isn't yet type-checked

That said, I tried running mypy and ty on the file from #45071 and I can confirm that ty complains about the same thing. So I'm happy to merge the PR

Ideally since we started, we'd make base config file completely ty-friendly, so I will take it noted

@zucchini-nlp
Copy link
Copy Markdown
Member

@bot /style

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

Style fix is beginning .... View the workflow run here.

@zucchini-nlp
Copy link
Copy Markdown
Member

Style bot went silent 😢 Can you run make style @shhKnight30 ?

@shhKnight30
Copy link
Copy Markdown
Contributor Author

ohk..... running make style and pushing the fixes.

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: clap, deit, depth_anything, dpt

@shhKnight30
Copy link
Copy Markdown
Contributor Author

ran make style..... fixed the remaining lint issues .......pushed the updates.

@zucchini-nlp zucchini-nlp force-pushed the fix-type-checking-issue branch from ba03200 to 67cff5c Compare April 10, 2026 10:18
@zucchini-nlp
Copy link
Copy Markdown
Member

Rebase was bad and had many unrelated diffs. Reverted back and rebased again, merging to main :)

@zucchini-nlp zucchini-nlp enabled auto-merge April 10, 2026 10:19
@shhKnight30
Copy link
Copy Markdown
Contributor Author

Thank u :)) @zucchini-nlp

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@github-actions
Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45240&sha=e3a1d8

@zucchini-nlp zucchini-nlp added this pull request to the merge queue Apr 10, 2026
Merged via the queue into huggingface:main with commit ce83f16 Apr 10, 2026
28 checks passed
sirzechs66 pushed a commit to sirzechs66/transformers that referenced this pull request Apr 18, 2026
…ingface#45071) (huggingface#45240)

* fix: restore mypy type checking for PreTrainedConfig subclasses (huggingface#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.

* style

---------

Co-authored-by: raushan <raushan@huggingface.co>
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

3 participants