-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Annotated support and therefore set minimum python version as 3.9
- Loading branch information
1 parent
d6396c1
commit 658e18f
Showing
9 changed files
with
89 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"-W", | ||
"ignore", | ||
"-vv", | ||
"tests" | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
from typing import Annotated | ||
|
||
import marshmallow.fields | ||
from . import NewType | ||
|
||
Url = NewType("Url", str, field=marshmallow.fields.Url) | ||
Email = NewType("Email", str, field=marshmallow.fields.Email) | ||
Url = Annotated[str, marshmallow.fields.Url] | ||
Email = Annotated[str, marshmallow.fields.Email] | ||
|
||
# Aliases | ||
URL = Url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import unittest | ||
from typing import Annotated, Optional | ||
|
||
import marshmallow | ||
import marshmallow.fields | ||
|
||
from marshmallow_dataclass import dataclass | ||
|
||
|
||
class TestAnnotatedField(unittest.TestCase): | ||
def test_annotated_field(self): | ||
@dataclass | ||
class AnnotatedValue: | ||
value: Annotated[str, marshmallow.fields.Email] | ||
default_string: Annotated[ | ||
Optional[str], marshmallow.fields.String(missing="Default String") | ||
] = None | ||
|
||
schema = AnnotatedValue.Schema() | ||
|
||
self.assertEqual( | ||
schema.load({"value": "test@test.com"}), | ||
AnnotatedValue(value="test@test.com", default_string="Default String"), | ||
) | ||
self.assertEqual( | ||
schema.load({"value": "test@test.com", "default_string": "override"}), | ||
AnnotatedValue(value="test@test.com", default_string="override"), | ||
) | ||
|
||
with self.assertRaises(marshmallow.exceptions.ValidationError): | ||
schema.load({"value": "notavalidemail"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters