-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0efa3e
commit b4776a9
Showing
4 changed files
with
60 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from typing import List, Optional | ||
|
||
from implicitdict import ImplicitDict | ||
|
||
|
||
class MySpecialClass(str): | ||
@property | ||
def is_special(self) -> bool: | ||
return True | ||
|
||
|
||
class MyContainers(ImplicitDict): | ||
single_value: MySpecialClass | ||
value_list: List[MySpecialClass] | ||
optional_list: Optional[List[MySpecialClass]] | ||
optional_value_list: List[Optional[MySpecialClass]] | ||
list_of_lists: List[List[MySpecialClass]] | ||
|
||
|
||
def test_container_item_value_casting(): | ||
containers: MyContainers = ImplicitDict.parse( | ||
{ | ||
"single_value": "foo", | ||
"value_list": ["value1", "value2"], | ||
"optional_list": ["bar"], | ||
"optional_value_list": ["baz", None], | ||
"list_of_lists": [["list1v1", "list1v2"], ["list2v1"]] | ||
}, MyContainers) | ||
|
||
assert containers.single_value.is_special | ||
|
||
assert len(containers.value_list) == 2 | ||
for v in containers.value_list: | ||
assert v.is_special | ||
|
||
assert len(containers.optional_list) == 1 | ||
assert containers.optional_list[0].is_special | ||
|
||
assert len(containers.optional_value_list) == 2 | ||
for v in containers.optional_value_list: | ||
assert (v is None) or v.is_special | ||
|
||
assert len(containers.list_of_lists) == 2 | ||
assert len(containers.list_of_lists[0]) == 2 | ||
for v in containers.list_of_lists[0]: | ||
assert v.is_special | ||
assert len(containers.list_of_lists[1]) == 1 | ||
for v in containers.list_of_lists[1]: | ||
assert v.is_special |
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