fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240
Open
shhKnight30 wants to merge 2 commits intohuggingface:mainfrom
Open
fix: restore mypy type checking for PreTrainedConfig subclasses (#45071)#45240shhKnight30 wants to merge 2 commits intohuggingface:mainfrom
shhKnight30 wants to merge 2 commits intohuggingface:mainfrom
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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