Skip to content

Commit dd34efc

Browse files
committed
Remove support for callable annotations
This approach was unsafe. See PR #259 for more details
1 parent 4531c35 commit dd34efc

File tree

2 files changed

+0
-46
lines changed

2 files changed

+0
-46
lines changed

marshmallow_dataclass/__init__.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,6 @@ def _field_for_annotated_type(
757757
)
758758
# Support `partial(mf.List, mf.String)`
759759
or (isinstance(arg, partial) and _is_marshmallow_field(arg.func))
760-
# Support `lambda *args, **kwargs: mf.List(mf.String, *args, **kwargs)`
761-
or (_is_callable_marshmallow_field(arg))
762760
]
763761
if marshmallow_annotations:
764762
if len(marshmallow_annotations) > 1:
@@ -1087,18 +1085,6 @@ def _is_marshmallow_field(obj) -> bool:
10871085
) or isinstance(obj, marshmallow.fields.Field)
10881086

10891087

1090-
def _is_callable_marshmallow_field(obj) -> bool:
1091-
"""Checks if the object is a callable and if the callable returns a marshmallow field"""
1092-
if callable(obj):
1093-
try:
1094-
potential_field = obj()
1095-
return _is_marshmallow_field(potential_field)
1096-
except Exception:
1097-
return False
1098-
1099-
return False
1100-
1101-
11021088
def NewType(
11031089
name: str,
11041090
typ: Type[_U],

tests/test_annotated.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,3 @@ class AnnotatedValue:
6666

6767
with self.assertRaises(marshmallow.exceptions.ValidationError):
6868
schema.load({"emails": "notavalidemail"})
69-
70-
def test_annotated_callable_field(self) -> None:
71-
"""
72-
NewType allowed us to specify a lambda or partial because there was no type inspection.
73-
But with Annotated we do type inspection. While we can't reliably do type inspection on a callable,
74-
i.e.: lambda, we can call it and check if it returns a Field.
75-
"""
76-
77-
@dataclass
78-
class AnnotatedValue:
79-
emails: Annotated[
80-
List[str],
81-
lambda *args, **kwargs: marshmallow.fields.List(
82-
marshmallow.fields.Email, *args, **kwargs
83-
),
84-
] = dataclasses.field(default_factory=lambda: ["default@email.com"])
85-
86-
schema = AnnotatedValue.Schema() # type: ignore[attr-defined]
87-
88-
self.assertEqual(
89-
schema.load({}),
90-
AnnotatedValue(emails=["default@email.com"]),
91-
)
92-
self.assertEqual(
93-
schema.load({"emails": ["test@test.com"]}),
94-
AnnotatedValue(
95-
emails=["test@test.com"],
96-
),
97-
)
98-
99-
with self.assertRaises(marshmallow.exceptions.ValidationError):
100-
schema.load({"emails": "notavalidemail"})

0 commit comments

Comments
 (0)