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_optional_with_optional():
assert is_optional(Optional[int])
def test_is_optional_with_optional_of_union():
assert is_optional(Optional[Union[int, float]])
def test_is_optional_with_non_optional():
assert not is_optional(int)
def _build_value_for_union(union: Type, data: Any, config: Config) -> Any:
types = extract_generic(union)
if is_optional(union) and len(types) == 2:
return _build_value(type_=types[0], data=data, config=config)
for inner_type in types:
try:
value = _build_value(type_=inner_type, data=data, config=config)
if is_instance(value, inner_type):
return value
except DaciteError:
pass
if not config.check_types:
return data
raise UnionMatchError(field_type=union, value=data)
def get_default_value_for_field(field: Field) -> Any:
if field.default != MISSING:
return field.default
elif field.default_factory != MISSING: # type: ignore
return field.default_factory() # type: ignore
elif is_optional(field.type):
return None
raise DefaultValueNotFoundError()