Adding a bunch of flake8 extensions and working through the errors
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
import typing
|
||||
from datetime import datetime
|
||||
from enum import EnumMeta
|
||||
import typing
|
||||
from typing import Dict, List, Type
|
||||
from typing import Any, Dict, Type
|
||||
|
||||
from dateutil import parser
|
||||
|
||||
|
||||
def from_json(cls, data):
|
||||
def from_json(template_type: Any, data: Any) -> Any:
|
||||
"""
|
||||
Converts data from a JSON parse into Python data structures.
|
||||
Converts data from a JSON parse into an instantiation of the Python object
|
||||
specified by template_type.
|
||||
|
||||
Arguments:
|
||||
|
||||
cls: the template class to deserialize into
|
||||
template_type: the template type to deserialize into
|
||||
data: the data to deserialize to the class
|
||||
"""
|
||||
# Approach for deserialization here:
|
||||
@@ -20,50 +21,49 @@ def from_json(cls, data):
|
||||
|
||||
# If it's a forward reference, evaluate it to figure out the actual
|
||||
# type. This allows for types that have to be put into a string.
|
||||
if isinstance(cls, typing.ForwardRef):
|
||||
cls = cls._evaluate(globals(), locals())
|
||||
if isinstance(template_type, typing.ForwardRef): # type: ignore
|
||||
template_type = template_type._evaluate(globals(), locals())
|
||||
|
||||
annotations: Dict[str, Type] = getattr(cls, '__annotations__', {})
|
||||
annotations: Dict[str,
|
||||
Type] = getattr(template_type, '__annotations__', {})
|
||||
|
||||
# Handle primitive of objects
|
||||
instance: Any = None
|
||||
if data is None:
|
||||
instance = None
|
||||
# Handle generics. List[*], Dict[*, *] in particular.
|
||||
elif type(cls) == typing._GenericAlias:
|
||||
elif type(template_type) == typing._GenericAlias: # type: ignore
|
||||
# Having to use this because things changed in Python 3.7.
|
||||
class_name = cls._name
|
||||
class_name = template_type._name
|
||||
|
||||
# This is not very elegant since it doesn't allow things which sublass
|
||||
# from List or Dict. For my purposes, this doesn't matter.
|
||||
if class_name == 'List':
|
||||
list_type = cls.__args__[0]
|
||||
instance: List[list_type] = list()
|
||||
for value in data:
|
||||
instance.append(from_json(list_type, value))
|
||||
inner_type = template_type.__args__[0]
|
||||
instance = [from_json(inner_type, value) for value in data]
|
||||
|
||||
elif class_name == 'Dict':
|
||||
key_type, val_type = cls.__args__
|
||||
instance: Dict[key_type, val_type] = dict()
|
||||
for key, value in data.items():
|
||||
key = from_json(key_type, key)
|
||||
value = from_json(val_type, value)
|
||||
instance[key] = value
|
||||
key_type, val_type = template_type.__args__
|
||||
instance = {
|
||||
from_json(key_type, key): from_json(val_type, value)
|
||||
for key, value in data.items()
|
||||
}
|
||||
else:
|
||||
raise Exception(
|
||||
f'Trying to deserialize an unsupported type: {cls._name}')
|
||||
|
||||
elif cls == str or issubclass(cls, str):
|
||||
'Trying to deserialize an unsupported type: {}'.format(
|
||||
template_type._name))
|
||||
elif template_type == str or issubclass(template_type, str):
|
||||
instance = data
|
||||
elif cls == int or issubclass(cls, int):
|
||||
elif template_type == int or issubclass(template_type, int):
|
||||
instance = int(data)
|
||||
elif cls == bool or issubclass(cls, bool):
|
||||
elif template_type == bool or issubclass(template_type, bool):
|
||||
instance = bool(data)
|
||||
elif type(cls) == EnumMeta:
|
||||
elif type(template_type) == EnumMeta:
|
||||
if type(data) == dict:
|
||||
instance = cls(data.get('_value_'))
|
||||
instance = template_type(data.get('_value_'))
|
||||
else:
|
||||
instance = cls(data)
|
||||
elif cls == datetime:
|
||||
instance = template_type(data)
|
||||
elif template_type == datetime:
|
||||
if type(data) == int:
|
||||
instance = datetime.fromtimestamp(data / 1000)
|
||||
else:
|
||||
@@ -72,7 +72,7 @@ def from_json(cls, data):
|
||||
# Handle everything else by first instantiating the class, then adding
|
||||
# all of the sub-elements, recursively calling from_json on them.
|
||||
else:
|
||||
instance: cls = cls()
|
||||
instance = template_type()
|
||||
for field, field_type in annotations.items():
|
||||
value = data.get(field)
|
||||
setattr(instance, field, from_json(field_type, value))
|
||||
|
Reference in New Issue
Block a user