Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from typing import Dict, Any, TypeVar, Type, Union, Callable, List, Collection, Optional, Set, Mapping, Tuple
class DaciteError(Exception):
pass
class WrongTypeError(DaciteError):
def __init__(self, field: Field, value: Any) -> None:
super().__init__(f'wrong type for field "{field.name}" - should be "{_get_type_name(field.type)}" '
f'instead of "{_get_type_name(type(value))}"')
self.field = field
self.value = value
class MissingValueError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'missing value for field {field.name}')
self.field = field
class UnionMatchError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'can not match the data to any type of "{field.name}" union: {_get_type_name(field.type)}')
self.field = field
class InvalidConfigurationError(DaciteError):
def __init__(self, parameter: str, available_choices: Set[str], value: str) -> None:
super().__init__(f'invalid value in "{parameter}" configuration: "{value}". '
f'Choices are: {", ".join(available_choices)}.')
self.parameter = parameter
from dataclasses import fields, MISSING, is_dataclass, Field, dataclass, field as dc_field
from typing import Dict, Any, TypeVar, Type, Union, Callable, List, Collection, Optional, Set, Mapping, Tuple
class DaciteError(Exception):
pass
class WrongTypeError(DaciteError):
def __init__(self, field: Field, value: Any) -> None:
super().__init__(f'wrong type for field "{field.name}" - should be "{_get_type_name(field.type)}" '
f'instead of "{_get_type_name(type(value))}"')
self.field = field
self.value = value
class MissingValueError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'missing value for field {field.name}')
self.field = field
class UnionMatchError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'can not match the data to any type of "{field.name}" union: {_get_type_name(field.type)}')
return _inner_from_dict_for_dataclass(
data_class=t,
data=data,
outer_config=outer_config,
field=field,
)
elif _is_data_class_collection(t) and _is_instance(t, data):
return _inner_from_dict_for_collection(
collection=t,
data=data,
outer_config=outer_config,
field=field,
)
elif _is_instance(t, data):
return data
except DaciteError:
pass
raise UnionMatchError(field)
self.value = value
class MissingValueError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'missing value for field {field.name}')
self.field = field
class UnionMatchError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'can not match the data to any type of "{field.name}" union: {_get_type_name(field.type)}')
self.field = field
class InvalidConfigurationError(DaciteError):
def __init__(self, parameter: str, available_choices: Set[str], value: str) -> None:
super().__init__(f'invalid value in "{parameter}" configuration: "{value}". '
f'Choices are: {", ".join(available_choices)}.')
self.parameter = parameter
self.available_choices = available_choices
self.value = value
@dataclass
class Config:
remap: Dict[str, str] = dc_field(default_factory=dict)
prefixed: Dict[str, str] = dc_field(default_factory=dict)
cast: List[str] = dc_field(default_factory=list)
transform: Dict[str, Callable[[Any], Any]] = dc_field(default_factory=dict)
flattened: List[str] = dc_field(default_factory=list)
class WrongTypeError(DaciteError):
def __init__(self, field: Field, value: Any) -> None:
super().__init__(f'wrong type for field "{field.name}" - should be "{_get_type_name(field.type)}" '
f'instead of "{_get_type_name(type(value))}"')
self.field = field
self.value = value
class MissingValueError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'missing value for field {field.name}')
self.field = field
class UnionMatchError(DaciteError):
def __init__(self, field: Field) -> None:
super().__init__(f'can not match the data to any type of "{field.name}" union: {_get_type_name(field.type)}')
self.field = field
class InvalidConfigurationError(DaciteError):
def __init__(self, parameter: str, available_choices: Set[str], value: str) -> None:
super().__init__(f'invalid value in "{parameter}" configuration: "{value}". '
f'Choices are: {", ".join(available_choices)}.')
self.parameter = parameter
self.available_choices = available_choices
self.value = value
@dataclass
class Config: