Ejemplo n.º 1
0
def dataclass_model(
    origin: Type,
    model: Model,
    *,
    fields_only: bool = False,
    additional_properties: Optional[bool] = None,
    coercion: Optional["Coerce"] = None,
    fall_back_on_default: Optional[bool] = None,
    exclude_unset: Optional[bool] = None,
) -> Tuple[Conversion, Conversion]:
    warnings.warn(
        "dataclass_model is deprecated, use set_object_fields instead",
        DeprecationWarning,
    )
    if isinstance(model, type):
        check_model(origin, model)

    model_type = DataclassModel(origin, model, fields_only)
    conversion = Conversion(
        identity,
        additional_properties=additional_properties,
        coerce=coercion,
        fall_back_on_default=fall_back_on_default,
        exclude_unset=exclude_unset,
    )
    d_conv = replace(conversion, source=model_type, target=origin)
    s_conv = replace(conversion, source=origin, target=model_type)
    return d_conv, s_conv
Ejemplo n.º 2
0
def handle_dataclass_model(
        conversion: ResolvedConversion) -> ResolvedConversion:
    conv: Conversion = conversion
    if isinstance(conv.source, DataclassModel):
        conv = replace(conv, source=conv.source.dataclass)
    if isinstance(conv.target, DataclassModel):
        conv = replace(conv, target=conv.target.dataclass)
    return ResolvedConversion(conv)
Ejemplo n.º 3
0
def resolve_conversion(
    conversion: Union[Converter, property, Conversion], namespace: Dict[str, Any] = None
) -> ResolvedConversion:
    if not isinstance(conversion, Conversion):
        conversion = Conversion(conversion)
    if is_method(conversion.converter):
        if conversion.source is None:
            conversion = replace(conversion, source=method_class(conversion.converter))
        conversion = replace(conversion, converter=method_wrapper(conversion.converter))
    assert not isinstance(conversion.converter, property)
    source, target = converter_types(
        conversion.converter, conversion.source, conversion.target, namespace
    )
    return ResolvedConversion(replace(conversion, source=source, target=target))
Ejemplo n.º 4
0
def test_replace():
    obj = WithFieldsSet()
    assert fields_set(obj) == set()
    obj2 = std_replace(obj)
    assert fields_set(obj2) == {"a"}
    obj3 = replace(obj)
    assert fields_set(obj3) == set()
Ejemplo n.º 5
0
def handle_identity_conversion(conversion: ResolvedConversion,
                               tp: AnyType) -> ResolvedConversion:
    if (is_identity(conversion) and conversion.source == conversion.target
            and is_type_var(conversion.source)):
        return ResolvedConversion(replace(conversion, source=tp, target=tp))
    else:
        return conversion
Ejemplo n.º 6
0
def _override_alias(field: ObjectField, aliaser: Aliaser) -> ObjectField:
    if field.override_alias:
        return replace(
            field,
            metadata={**field.metadata, ALIAS_METADATA: aliaser(field.alias)},
            default=MISSING_DEFAULT,
        )
    else:
        return field
Ejemplo n.º 7
0
    def unsupported(self, tp: AnyType) -> Result:
        from apischema import settings

        origin = get_origin_or_type(tp)
        if isinstance(origin, type):
            fields = settings.default_object_fields(origin)
            if fields is not None:
                if get_args(tp):
                    sub = dict(zip(get_parameters(origin), get_args(tp)))
                    fields = [
                        replace(f, type=substitute_type_vars(f.type, sub))
                        for f in fields
                    ]
                return self._object(origin, fields)
        return super().unsupported(tp)