Skip to content

Commit

Permalink
Remove support for annotated partials
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanderlee committed Jun 26, 2024
1 parent dd34efc commit 7ac088d
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 34 deletions.
2 changes: 0 additions & 2 deletions marshmallow_dataclass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,6 @@ def _field_for_annotated_type(
is_generic_type(arg)
and _is_marshmallow_field(typing_extensions.get_origin(arg))
)
# Support `partial(mf.List, mf.String)`
or (isinstance(arg, partial) and _is_marshmallow_field(arg.func))
]
if marshmallow_annotations:
if len(marshmallow_annotations) > 1:
Expand Down
33 changes: 1 addition & 32 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import dataclasses
import functools
import sys
import unittest
from typing import List, Optional
from typing import Optional

import marshmallow
import marshmallow.fields
Expand Down Expand Up @@ -37,32 +35,3 @@ class AnnotatedValue:

with self.assertRaises(marshmallow.exceptions.ValidationError):
schema.load({"value": "notavalidemail"})

def test_annotated_partial_field(self) -> None:
"""
NewType allowed us to specify a lambda or partial because there was no type inspection.
But with Annotated we do type inspection. Partial still allows us to to type inspection.
"""

@dataclass
class AnnotatedValue:
emails: Annotated[
List[str],
functools.partial(marshmallow.fields.List, marshmallow.fields.Email),
] = dataclasses.field(default_factory=lambda: ["default@email.com"])

schema = AnnotatedValue.Schema() # type: ignore[attr-defined]

self.assertEqual(
schema.load({}),
AnnotatedValue(emails=["default@email.com"]),
)
self.assertEqual(
schema.load({"emails": ["test@test.com"]}),
AnnotatedValue(
emails=["test@test.com"],
),
)

with self.assertRaises(marshmallow.exceptions.ValidationError):
schema.load({"emails": "notavalidemail"})

0 comments on commit 7ac088d

Please sign in to comment.