@@ -166,13 +166,27 @@ def __init__(self, **kwargs: Any) -> None:
166166 # Call the original __init__ with standard fields
167167 original_init (self , ** standard_kwargs )
168168
169- # Add any additional kwargs as attributes
169+ # Pass any additional kwargs to `__post_init__` and let the object
170+ # decide whether to set the attr or use for different purposes (e.g. BC checks)
171+ additional_kwargs = {}
170172 for name , value in kwargs .items ():
171173 if name not in dataclass_fields :
172- self .__setattr__ (name , value )
174+ additional_kwargs [name ] = value
175+
176+ self .__post_init__ (** additional_kwargs )
173177
174178 cls .__init__ = __init__ # type: ignore[method-assign]
175179
180+ # Define a default __post_init__ if not defined
181+ if not hasattr (cls , "__post_init__" ):
182+
183+ def __post_init__ (self , ** kwargs : Any ) -> None :
184+ """Default __post_init__ to accept additional kwargs."""
185+ for name , value in kwargs .items ():
186+ setattr (self , name , value )
187+
188+ cls .__post_init__ = __post_init__ # type: ignore[method-assign]
189+
176190 # (optional) Override __repr__ to include additional kwargs
177191 original_repr = cls .__repr__
178192
0 commit comments