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_extract_generic():
assert extract_generic(List[int]) == (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 _build_value_for_collection(collection: Type, data: Any, config: Config) -> Any:
if is_instance(data, Mapping):
return data.__class__(
(key, _build_value(type_=extract_generic(collection)[1], data=value, config=config))
for key, value in data.items()
)
return data.__class__(_build_value(type_=extract_generic(collection)[0], data=item, config=config) for item in data)
def _build_value_for_collection(collection: Type, data: Any, config: Config) -> Any:
if is_instance(data, Mapping):
return data.__class__(
(key, _build_value(type_=extract_generic(collection)[1], data=value, config=config))
for key, value in data.items()
)
return data.__class__(_build_value(type_=extract_generic(collection)[0], data=item, config=config) for item in data)