File tree 3 files changed +20
-3
lines changed
3 files changed +20
-3
lines changed Original file line number Diff line number Diff line change
1
+ Schemas no longer copy non-field dataclass attributes. Thanks to @sveinse for report and test.
Original file line number Diff line number Diff line change @@ -118,10 +118,12 @@ def class_schema(
118
118
else :
119
119
raise desert .exceptions .NotAnAttrsClassOrDataclass (clazz )
120
120
121
- # Copy all public members of the dataclass to the schema
122
- attributes = {k : v for k , v in inspect .getmembers (clazz ) if not k .startswith ("_" )}
123
- # Update the schema members to contain marshmallow fields instead of dataclass fields
121
+ # Copy all public fields of the dataclass to the schema
122
+ attributes = {
123
+ field .name : field for field in fields if not field .name .startswith ("_" )
124
+ }
124
125
126
+ # Update the schema members to contain marshmallow fields instead of dataclass fields.
125
127
hints = t .get_type_hints (clazz )
126
128
for field in fields :
127
129
if field .init :
Original file line number Diff line number Diff line change @@ -557,3 +557,17 @@ def _(self):
557
557
558
558
schema = desert .schema (C )
559
559
assert schema .load ({"x" : 1 }) == C (x = 1 , y = 2 )
560
+
561
+
562
+ def test_methods_not_on_schema (module ):
563
+ """Dataclass methods are not copied to the schema."""
564
+
565
+ @module .dataclass
566
+ class A :
567
+ def dataclass_method (self ) -> None :
568
+ """This method should not exist on the schema."""
569
+
570
+ schema = desert .schema (A )
571
+ sentinel = object ()
572
+ method = getattr (schema , "dataclass_method" , sentinel )
573
+ assert method is sentinel
You can’t perform that action at this time.
0 commit comments