-
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 #29 from kodemore/fix-union-force-mode
Add force mode to unions and hotfix union encoding
- Loading branch information
Showing
8 changed files
with
115 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
from chili.typing import get_non_optional_fields | ||
|
||
|
||
def test_get_non_optional_fields_from_data_class() -> None: | ||
# given | ||
@dataclass | ||
class Person: | ||
name: str | ||
age: int | ||
street_name: Optional[str] | ||
street_number: Optional[int] | ||
|
||
# when | ||
fields = get_non_optional_fields(Person) | ||
|
||
# then | ||
assert fields == ["name", "age"] | ||
|
||
|
||
def test_get_non_optional_fields_from_class() -> None: | ||
class Person: | ||
name: str | ||
age: int | ||
street_name: Optional[str] | ||
street_number: Optional[int] | ||
|
||
# when | ||
fields = get_non_optional_fields(Person) | ||
|
||
# then | ||
assert fields == ["name", "age"] |
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,5 +1,5 @@ | ||
from dataclasses import dataclass | ||
from typing import List, Union | ||
from typing import List, Optional, Union | ||
|
||
from chili import decode, encode | ||
|
||
|
@@ -88,3 +88,48 @@ class EmailAddress: | |
|
||
# then | ||
assert result == {"address": "[email protected]", "label": ""} | ||
|
||
|
||
def test_can_decode_nested_union() -> None: | ||
# given | ||
@dataclass | ||
class HomeAddress: | ||
home_street: str | ||
number: Optional[int] = 0 | ||
|
||
@dataclass | ||
class OfficeAddress: | ||
office_street: str | ||
number: Optional[int] = 0 | ||
|
||
@dataclass | ||
class Address: | ||
street: str | ||
number: Optional[int] = 0 | ||
|
||
@dataclass | ||
class Person: | ||
name: str | ||
address: HomeAddress | OfficeAddress | Address | ||
|
||
data_office = { | ||
"name": "Bob", | ||
"address": {"office_street": "street"}, | ||
} | ||
|
||
data_home = { | ||
"name": "Bob", | ||
"address": {"home_street": "home"}, | ||
} | ||
|
||
# when | ||
decoded = decode(data_office, Person) | ||
|
||
# then | ||
assert isinstance(decoded.address, OfficeAddress) | ||
|
||
# when | ||
decoded = decode(data_home, Person) | ||
|
||
# then | ||
assert isinstance(decoded.address, HomeAddress) |