-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from kodemore/fix-union-support
Add union and newtype support for python > 3.10
- Loading branch information
Showing
6 changed files
with
90 additions
and
4 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
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 |
---|---|---|
|
@@ -19,3 +19,72 @@ class Child(Parent): | |
|
||
assert isinstance(after[0], Parent) | ||
assert isinstance(after[1], Child) | ||
|
||
|
||
def test_union_simple_type_or_dataclass() -> None: | ||
# given | ||
@dataclass | ||
class EmailAddress: | ||
address: str | ||
label: str | ||
|
||
value = "[email protected]" | ||
complex_value = {"address": "[email protected]", "label": ""} | ||
|
||
# when | ||
result = decode(value, Union[str, EmailAddress]) | ||
|
||
# then | ||
assert result == "[email protected]" | ||
|
||
# when | ||
result = decode(complex_value, Union[str, EmailAddress]) | ||
|
||
# then | ||
assert isinstance(result, EmailAddress) | ||
|
||
|
||
def test_new_union_style_decode() -> None: | ||
# given | ||
@dataclass | ||
class EmailAddress: | ||
address: str | ||
label: str | ||
|
||
value = "[email protected]" | ||
complex_value = {"address": "[email protected]", "label": ""} | ||
|
||
# when | ||
result = decode(value, str | EmailAddress) | ||
|
||
# then | ||
assert result == "[email protected]" | ||
|
||
# when | ||
result = decode(complex_value, str | EmailAddress) | ||
|
||
# then | ||
assert isinstance(result, EmailAddress) | ||
|
||
|
||
def test_new_union_style_encode() -> None: | ||
# given | ||
@dataclass | ||
class EmailAddress: | ||
address: str | ||
label: str | ||
|
||
value = "[email protected]" | ||
complex_value = {"address": "[email protected]", "label": ""} | ||
|
||
# when | ||
result = encode(value, str | EmailAddress) | ||
|
||
# then | ||
assert result == "[email protected]" | ||
|
||
# when | ||
result = encode(EmailAddress(address="[email protected]", label=""), str | EmailAddress) | ||
|
||
# then | ||
assert result == {"address": "[email protected]", "label": ""} |