fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240
Conversation
…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.
zucchini-nlp
left a comment
There was a problem hiding this comment.
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
|
@bot /style |
|
Style fix is beginning .... View the workflow run here. |
|
Style bot went silent 😢 Can you run |
|
ohk..... running make style and pushing the fixes. |
|
[For maintainers] Suggested jobs to run (before merge) run-slow: clap, deit, depth_anything, dpt |
|
ran make style..... fixed the remaining lint issues .......pushed the updates. |
ba03200 to
67cff5c
Compare
|
Rebase was bad and had many unrelated diffs. Reverted back and rebased again, merging to |
|
Thank u :)) @zucchini-nlp |
|
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. |
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45240&sha=e3a1d8 |
…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>
What does this PR do?
Type checking for
PreTrainedConfigsubclasses 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)onPreTrainedConfig. It's a static-only hint (PEP 681)that tells type checkers to derive
__init__signatures from the declared fieldsinstead. No runtime behavior changes, no new dependencies —
typing_extensionsisalready in the tree. Every subclass picks this up automatically.
Pydantic, attrs, and SQLModel all use the same approach.
Reproduction
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 analyzersdon't evaluate runtime assignments.
@dataclass_transformexists precisely forthis situation and is the right tool here.
Changes
src/transformers/configuration_utils.py@dataclass_transform(kw_only_default=True)toPreTrainedConfigfrom inspect import signatureimport andoriginal_signaturevariableAnything 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:
vllmandaxolotlhave both pinnedtransformers<5.4.0becauseof this issue, so there's real downstream pressure to get it resolved.
Before submitting
PretrainedConfigtype checking #45071, where @Rocketknight1 indicated openness to a fixFixes #45071
cc @Rocketknight1 @ArthurZucker @Cyrilvallez