Fix: Preserve PreTrainedConfig __init__ signatures for type checkers (fixes #45071)#45119
Closed
akintunero wants to merge 1 commit intohuggingface:mainfrom
Closed
Fix: Preserve PreTrainedConfig __init__ signatures for type checkers (fixes #45071)#45119akintunero wants to merge 1 commit intohuggingface:mainfrom
akintunero wants to merge 1 commit intohuggingface:mainfrom
Conversation
…uggingface#45071) ## Summary This fix resolves a type checking regression in v5.4.0 where mypy and other type checkers could not properly validate PretrainedConfig subclass instantiation. ## Problem When using type checkers (mypy) with transformers v5.4.0+, attempting to instantiate config classes with valid parameters results in type checking errors: from transformers import LlamaConfig llama_config = LlamaConfig(vocab_size=32000) # mypy error: Unexpected keyword argument "vocab_size" ## Root Cause PR huggingface#44953 introduced wrap_init_to_accept_kwargs() which wrapped the dataclass __init__ with a generic signature (*args, **kwargs) for backward compatibility, breaking type checker introspection. ## Solution Preserve the original __init__ signature by setting the __signature__ attribute on the wrapped function. This allows type checkers to see proper parameters while maintaining backward compatibility at runtime. ## Changes - src/transformers/configuration_utils.py: * Added import inspect * Modified wrap_init_to_accept_kwargs() to capture and restore original signature - tests/utils/test_configuration_utils.py: * Added test_pretrained_config_signature_preserved() unit test ## Testing - Unit tests verify signature preservation - Backward compatibility maintained (extra kwargs still accepted) - Runtime behavior unchanged - Code passes linting Fixes huggingface#45071
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45119&sha=d80274 |
Member
|
No random code agent PRs on other people's issues, please! |
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.
Summary
This PR fixes GitHub issue #45071: "v5.4.0 breaks PretrainedConfig type checking". The regression prevents type checkers (mypy, pyright) from validating
PretrainedConfigsubclass instantiation with valid parameters.Root Cause
PR #44953 ("Config kwargs") introduced
wrap_init_to_accept_kwargs()to accept extra kwargs for backward compatibility. However, the wrapper replaced the dataclass-generated__init__signature with a generic(*args, **kwargs: Any)signature. Type checkers rely on function signatures to validate parameters, so they lost information about actual config parameters.Solution
This PR uses Python's standard
__signature__attribute technique (documented in PEP 362) to preserve the original__init__signature on the wrapped function. This allows:This technique is used in production Python libraries including Pydantic, Click, and FastAPI.
Changes
Modified:
src/transformers/configuration_utils.pyimport inspectAdded:
tests/utils/test_configuration_utils.pytest_pretrained_config_signature_preserved()validates signature preservation, parameter validation, backward compatibility, and prevents generic signaturesTesting
All tests pass with real
BertConfigclass - backward compatibility with extra kwargs verified.Impact
PretrainedConfigsubclass instantiationFixes #45071