Skip to content

Commit 388b77a

Browse files
author
desert
committed
Don't copy dataclass methods to the schema.
1 parent 7ca1e23 commit 388b77a

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/desert/_make.py

Lines changed: 5 additions & 3 deletions
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

Lines changed: 14 additions & 0 deletions
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)