-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14 from robabram/ra/dict-annot-type-fix
Correctly handle 'typing.Dict' annotations
- Loading branch information
Showing
3 changed files
with
71 additions
and
11 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,61 @@ | ||
# | ||
# This file is subject to the terms and conditions defined in the | ||
# file 'LICENSE', which is part of this source code package. | ||
# | ||
from typing import Dict, List | ||
|
||
from tests.base_test import BaseTestCase | ||
from python_easy_json import JSONObject | ||
|
||
|
||
class SimpleDictTestObject(JSONObject): | ||
|
||
test_prop: Dict = None | ||
|
||
|
||
class ListDictTestObject(JSONObject): | ||
|
||
test_prop: List[Dict] = None | ||
|
||
|
||
class TestListsDict(BaseTestCase): | ||
|
||
def test_simple_dict(self): | ||
""" Test an object with a typing.Dict type hint. """ | ||
|
||
data = { | ||
'test_prop': { | ||
'col1': 'abc', | ||
'col2': 'xyz' | ||
} | ||
} | ||
|
||
obj = SimpleDictTestObject(data, cast_types=True) | ||
|
||
self.assertIsInstance(obj, SimpleDictTestObject) | ||
# Ensure property type is 'dict'. | ||
self.assertIsInstance(obj.test_prop, JSONObject) | ||
|
||
def test_list_of_dicts(self): | ||
""" Test a list of dict objects """ | ||
data = { | ||
'test_prop': [ | ||
{ | ||
'col1': 'abc', | ||
'col2': 'xyz' | ||
}, | ||
{ | ||
'col1': 'ghi', | ||
'col2': 'rst' | ||
} | ||
] | ||
} | ||
|
||
obj = ListDictTestObject(data, cast_types=True) | ||
|
||
self.assertIsInstance(obj, ListDictTestObject) | ||
# Ensure property type is 'dict'. | ||
self.assertIsInstance(obj.test_prop, list) | ||
|
||
for v in obj.test_prop: | ||
self.assertIsInstance(v, JSONObject) |