Skip to content

Pass kwargs to post init in dataclasses#3771

Merged
Wauplin merged 2 commits intohuggingface:mainfrom
zucchini-nlp:config-validation-changes
Feb 4, 2026
Merged

Pass kwargs to post init in dataclasses#3771
Wauplin merged 2 commits intohuggingface:mainfrom
zucchini-nlp:config-validation-changes

Conversation

@zucchini-nlp
Copy link
Copy Markdown
Member

As per title, needed for huggingface/transformers#41250. I verified that it is the easiest way to keep BC for remote code, because we can't force everyone to use dataclasses as configs

We don't want to set kwargs as attr directly, as some keys have to be popped (i.e. deprecated key) and some keys have to be transformed before setting (e.g. key attn_implememtation is set to private _attn_implememtation_internal)

While it can possibly be done in post init by manually deleting and post processing attr, imo it's cleaner to pass kwargs

# We could do smth like this right now, personally i find it ugly
def __post_init__(self):
    if hastattr(sefl, "deprecated_arg": del self.deprecated_arg
    if hastattr(sefl, "private_arg": self.move_to_private_property(private_arg)

# But after the suggested PR we don't need to do anything, and can keep existing code in transformers
def __post_init__(self, **kwargs):
        if (torch_dtype := kwargs.pop("torch_dtype", None)) is not None:
            self.dtype = self.dtype if self.dtype is not None else torch_dtype

@zucchini-nlp zucchini-nlp requested a review from Wauplin February 4, 2026 08:43
@bot-ci-comment
Copy link
Copy Markdown

bot-ci-comment bot commented Feb 4, 2026

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.

@codecov
Copy link
Copy Markdown

codecov bot commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.56%. Comparing base (1daa48b) to head (95102bd).
⚠️ Report is 64 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3771      +/-   ##
==========================================
+ Coverage   75.00%   76.56%   +1.55%     
==========================================
  Files         145      153       +8     
  Lines       13978    15086    +1108     
==========================================
+ Hits        10484    11550    +1066     
- Misses       3494     3536      +42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@Wauplin Wauplin left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @zucchini-nlp! Makes perfect sense yes. I've updated the PR to add back the default behavior of setting kwargs as attributes but it can now be overwritten. I'll merge the PR now (failing CI is unrelated) and we'll ship it very soon :)

@Wauplin Wauplin merged commit 1b03f0b into huggingface:main Feb 4, 2026
14 of 17 checks passed
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.

2 participants