Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_is_instance_with_new_type_and_matching_value_type():
assert is_instance("test", NewType("MyStr", str))
def test_is_instance_with_built_in_type_and_not_matching_value_type():
assert not is_instance("test", int)
def test_is_instance_with_built_in_type_and_matching_value_type():
assert is_instance(1, int)
def test_is_instance_with_generic_collection_and_not_matching_value_type():
assert not is_instance({1}, List[int])
if extra_fields:
raise UnexpectedDataError(keys=extra_fields)
for field in data_class_fields:
field = copy.copy(field)
field.type = data_class_hints[field.name]
try:
try:
field_data = data[field.name]
transformed_value = transform_value(
type_hooks=config.type_hooks, cast=config.cast, target_type=field.type, value=field_data
)
value = _build_value(type_=field.type, data=transformed_value, config=config)
except DaciteFieldError as error:
error.update_path(field.name)
raise
if config.check_types and not is_instance(value, field.type):
raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
except KeyError:
try:
value = get_default_value_for_field(field)
except DefaultValueNotFoundError:
if not field.init:
continue
raise MissingValueError(field.name)
if field.init:
init_values[field.name] = value
else:
post_init_values[field.name] = value
return create_instance(data_class=data_class, init_values=init_values, post_init_values=post_init_values)
def _build_value(type_: Type, data: Any, config: Config) -> Any:
if is_union(type_):
return _build_value_for_union(union=type_, data=data, config=config)
elif is_generic_collection(type_) and is_instance(data, extract_origin_collection(type_)):
return _build_value_for_collection(collection=type_, data=data, config=config)
elif is_dataclass(type_) and is_instance(data, Data):
return from_dict(data_class=type_, data=data, config=config)
return data