Skip to content

Fix: Preserve PreTrainedConfig __init__ signatures for type checkers (fixes #45071)#45119

Closed
akintunero wants to merge 1 commit intohuggingface:mainfrom
akintunero:fix/issue-45071-config-type-checking
Closed

Fix: Preserve PreTrainedConfig __init__ signatures for type checkers (fixes #45071)#45119
akintunero wants to merge 1 commit intohuggingface:mainfrom
akintunero:fix/issue-45071-config-type-checking

Conversation

@akintunero
Copy link
Copy Markdown
Contributor

Summary

This PR fixes GitHub issue #45071: "v5.4.0 breaks PretrainedConfig type checking". The regression prevents type checkers (mypy, pyright) from validating PretrainedConfig subclass 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:

  • Type checkers to see and validate the original parameters
  • Runtime to continue accepting extra kwargs for backward compatibility
  • Zero breaking changes to existing code

This technique is used in production Python libraries including Pydantic, Click, and FastAPI.

Changes

Modified: src/transformers/configuration_utils.py

  • Line 18: Added import inspect
  • Line 78: Capture original signature before wrapping
  • Lines 112-113: Restore signature on wrapped function

Added: tests/utils/test_configuration_utils.py

  • New test test_pretrained_config_signature_preserved() validates signature preservation, parameter validation, backward compatibility, and prevents generic signatures

Testing

All tests pass with real BertConfig class - backward compatibility with extra kwargs verified.

Impact

  • Type Checking: mypy, pyright, and other type checkers can now properly validate PretrainedConfig subclass instantiation
  • Runtime: No changes - extra kwargs still accepted for backward compatibility
  • Performance: Negligible - only affects type checker introspection
  • Breaking Changes: None - fully backward compatible

Fixes #45071

…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
@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=45119&sha=d80274

@Rocketknight1
Copy link
Copy Markdown
Member

No random code agent PRs on other people's issues, please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v5.4.0 breaks PretrainedConfig type checking

2 participants