File tree Expand file tree Collapse file tree 1 file changed +15
-2
lines changed
packages/testing/src/execution_testing/base_types Expand file tree Collapse file tree 1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -35,9 +35,22 @@ def copy(self: Self, **kwargs: Any) -> Self:
3535 This method preserves the actual field values (including those set via
3636 default_factory) while maintaining the model_fields_set to track which
3737 fields were explicitly set.
38+
39+ The implementation uses exclude_unset=True as a safe baseline, then
40+ explicitly adds back fields that were set via default_factory but not
41+ explicitly set by the user. This avoids conflicts with models that have
42+ mutually exclusive fields (e.g., Transaction with secret_key vs signature).
3843 """
39- # Get all current field values, including those set via default_factory
40- dump_dict = self .model_dump ()
44+ # Start with explicitly set fields (safe baseline)
45+ dump_dict = self .model_dump (exclude_unset = True )
46+
47+ # For fields with default_factory, include them if they're not in model_fields_set
48+ # This handles cases like Environment.gas_limit where the factory captures
49+ # dynamic configuration that should be preserved in copies
50+ for field_name , field_info in self .__class__ .model_fields .items ():
51+ if field_name not in self .model_fields_set and field_info .default_factory :
52+ dump_dict [field_name ] = getattr (self , field_name )
53+
4154 # Merge with the updates
4255 dump_dict .update (kwargs )
4356 # Create the new instance
You can’t perform that action at this time.
0 commit comments