Skip to content

Commit 6b073af

Browse files
author
python-desert
authored
Don't copy dataclass methods to the schema.
2 parents 1a44f59 + 0c9d39e commit 6b073af

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

changelog.d/79.change.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Schemas no longer copy non-field dataclass attributes. Thanks to @sveinse for report and test.

src/desert/_make.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ def class_schema(
118118
else:
119119
raise desert.exceptions.NotAnAttrsClassOrDataclass(clazz)
120120

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+
}
124125

126+
# Update the schema members to contain marshmallow fields instead of dataclass fields.
125127
hints = t.get_type_hints(clazz)
126128
for field in fields:
127129
if field.init:

tests/test_make.py

+14
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,17 @@ def _(self):
557557

558558
schema = desert.schema(C)
559559
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

0 commit comments

Comments
 (0)