-
Notifications
You must be signed in to change notification settings - Fork 76
Add Annotated support #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
402dea5
Add Annotated support and therefore set minimum python version as 3.9
mvanderlee 48b7e17
Add support back for Python3.8
mvanderlee 41e56e8
Fix Python 3.8 and add tox config for cross version testing
mvanderlee 0d43c41
Fix style issues. Rebase did not trigger pre-commit.
mvanderlee 113c478
Remove enum from pre-commit
mvanderlee 7eb9281
re-add but deprecate the newtype documentation
mvanderlee fae55c3
Move Annotated and Union handling to their own functions
mvanderlee 7fc3fb7
Remove tox from requirements
mvanderlee c19b563
Add coverage report and remove virtualenv-pyenv
mvanderlee dbd909f
Add warning when multiple Field annotations have bene detected
mvanderlee 0a43d81
Remove tox and document Annotated for python 3.8
mvanderlee 45e2aff
fix: line-endings
dairiki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,14 @@ | ||
import sys | ||
|
||
import marshmallow.fields | ||
from . import NewType | ||
|
||
Url = NewType("Url", str, field=marshmallow.fields.Url) | ||
Email = NewType("Email", str, field=marshmallow.fields.Email) | ||
if sys.version_info >= (3, 9): | ||
from typing import Annotated | ||
else: | ||
from typing_extensions import Annotated | ||
|
||
Url = Annotated[str, marshmallow.fields.Url] | ||
Email = Annotated[str, marshmallow.fields.Email] | ||
|
||
# Aliases | ||
URL = Url |
This file contains hidden or 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 hidden or 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,4 +1,4 @@ | ||
from setuptools import setup, find_packages | ||
from setuptools import find_packages, setup | ||
|
||
VERSION = "9.0.0" | ||
|
||
|
This file contains hidden or 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,37 @@ | ||
import sys | ||
import unittest | ||
from typing import Optional | ||
|
||
import marshmallow | ||
import marshmallow.fields | ||
|
||
from marshmallow_dataclass import dataclass | ||
|
||
if sys.version_info >= (3, 9): | ||
from typing import Annotated | ||
else: | ||
from typing_extensions import Annotated | ||
|
||
|
||
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(load_default="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"}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.