Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@cached
def get_naked_class(cls: type) -> type:
# Python3.5: typing classes have __extra__
# Python3.6: typing classes have __extra__
# Python3.7: typing classes have __origin__
# Return the non-generic class (e.g. dict) of a generic type (e.g. Dict).
attr = '__origin__'
if sys.version_info[1] in (5, 6):
attr = '__extra__'
return getattr(cls, attr, cls)
@cached
def _get_signature(cls):
return inspect.signature(cls.__init__).parameters
@cached
def get_serializer(
cls: type,
fork_inst: Optional[type] = StateHolder) -> callable:
"""
Return the serializer function that would be used for the given ``cls``.
:param cls: the type for which a serializer is to be returned.
:param fork_inst: if given, it uses this fork of ``JsonSerializable``.
:return: a serializer function.
"""
serializer = _get_lizer(cls, fork_inst._serializers,
fork_inst._classes_serializers, fork_inst)
return serializer
@cached
def get_deserializer(
cls: type,
fork_inst: Optional[type] = StateHolder) -> callable:
"""
Return the deserializer function that would be used for the given ``cls``.
:param cls: the type for which a deserializer is to be returned.
:param fork_inst: if given, it uses this fork of ``JsonSerializable``.
:return: a deserializer function.
"""
deserializer = _get_lizer(cls, fork_inst._deserializers,
fork_inst._classes_deserializers, fork_inst)
return deserializer
@cached
def _get_attributes_from_class(
cls: type,
strip_privates: bool,
strip_properties: bool,
strip_class_variables: bool,
strip_attr: tuple,
strict: bool) -> Dict[str, Optional[type]]:
# Get the attributes that are known in the class.
attributes_and_types = _get_attributes_and_types(cls, strict)
return _filter_attributes(cls, attributes_and_types, strip_privates,
strip_properties, strip_class_variables,
strip_attr)
@cached
def get_validator(
cls: type,
fork_inst: type = StateHolder) -> callable:
"""
Return the validator function that would be used for the given ``cls``.
:param cls: the type for which a deserializer is to be returned.
:param fork_inst: if given, it uses this fork of ``JsonSerializable``.
:return: a validator function.
"""
return _get_lizer(cls, fork_inst._validators,
fork_inst._classes_validators, fork_inst)
@cached
def get_union_params(un: type) -> list:
# Python3.5: Unions have __union_params__
# Python3.7: Unions have __args__
return getattr(un, '__union_params__', getattr(un, '__args__', None))
@cached
def get_type_hints(func: callable):
# Python3.5: get_type_hints raises on classes without explicit constructor
try:
result = typing.get_type_hints(func)
except AttributeError:
result = {}
return result
@cached
def _get_attributes_and_types(cls: type,
strict: bool) -> Dict[str, Optional[type]]:
if '__slots__' in cls.__dict__:
attributes = {attr: None for attr in cls.__slots__}
elif hasattr(cls, '__annotations__'):
# Fixme: remove code below if tests succeed on all platforms.
# attributes = cls.__annotations__
attributes = get_type_hints(cls)
elif strict:
hints = get_type_hints(cls.__init__)
attributes = {k: hints[k] for k in hints if k != 'self'}
else:
attributes = {}
# Add properties and class variables.
props, class_vars = _get_class_props(cls)
@cached
def tuple_with_ellipsis(tup: type) -> bool:
# Python3.5: Tuples have __tuple_use_ellipsis__
# Python3.7: Tuples have __args__
use_el = getattr(tup, '__tuple_use_ellipsis__', None)
if use_el is None:
use_el = tup.__args__[-1] is ...
return use_el