def import_loop(cls, instance_or_dict, field_converter=None, trusted_data=None,
                mapping=None, partial=False, strict=False, init_values=False,
                apply_defaults=False, convert=True, validate=False, new=False,
                oo=False, recursive=False, app_data=None, context=None):
    """
    The import loop is designed to take untrusted data and convert it into the
    native types, as described in ``cls``.  It does this by calling
    ``field_converter`` on every field.

    Errors are aggregated and returned by throwing a ``ModelConversionError``.

    :param cls:
        The class for the model.
    :param instance_or_dict:
        A dict of data to be converted into types according to ``cls``.
    :param field_converter:
        This function is applied to every field found in ``instance_or_dict``.
    :param trusted_data:
        A ``dict``-like structure that may contain already validated data.
    :param partial:
        Allow partial data to validate; useful for PATCH requests.
        Essentially drops the ``required=True`` arguments from field
        definitions. Default: False
    :param strict:
        Complain about unrecognized keys. Default: False
    :param apply_defaults:
        Whether to set fields to their default values when not present in input data.
    :param app_data:
        An arbitrary container for application-specific data that needs to
        be available during the conversion.
    :param context:
        A ``Context`` object that encapsulates configuration options and ``app_data``.
        The context object is created upon the initial invocation of ``import_loop``
        and is then propagated through the entire process.
    """
    if instance_or_dict is None:
        got_data = False
    else:
        got_data = True

    if got_data and not isinstance(instance_or_dict, (cls, dict)):
        raise ConversionError('Model conversion requires a model or dict')

    context = Context._make(context)
    try:
        context.initialized
    except:
        if type(field_converter) is types.FunctionType:
            field_converter = BasicConverter(field_converter)
        context._setdefaults({
            'initialized': True,
            'field_converter': field_converter,
            'trusted_data': trusted_data or {},
            'mapping': mapping or {},
            'partial': partial,
            'strict': strict,
            'init_values': init_values,
            'apply_defaults': apply_defaults,
            'convert': convert,
            'validate': validate,
            'new': new,
            'oo': oo,
            'recursive': recursive,
            'app_data': app_data if app_data is not None else {}
        })

    instance_or_dict = context.field_converter.pre(cls, instance_or_dict, context)

    _field_converter = context.field_converter
    _model_mapping = context.mapping.get('model_mapping')

    data = dict(context.trusted_data) if context.trusted_data else {}
    errors = {}

    if got_data:
        # Determine all acceptable field input names
        all_fields = cls._valid_input_keys
        if context.mapping:
            mapped_keys = (set(itertools.chain(*(
                          listify(input_keys) for target_key, input_keys in context.mapping.items()
                          if target_key != 'model_mapping'))))
            all_fields = all_fields | mapped_keys
        if context.strict:
            # Check for rogues if strict is set
            rogue_fields = all_fields - set(instance_or_dict)
            if rogue_fields:
                for field in rogue_fields:
                    errors[field] = 'Rogue field'

    for field_name, field in cls._field_list:

        value = Undefined
        serialized_field_name = field.serialized_name or field_name

        if got_data:
            for key in field.get_input_keys(context.mapping):
                if key and key in instance_or_dict:
                    value = instance_or_dict[key]
                    break

        if value is Undefined:
            if field_name in data:
                continue
            if context.apply_defaults:
                value = field.default
        if value is Undefined and context.init_values:
            value = None

        if got_data:
            if field.is_compound:
                if context.trusted_data and context.recursive:
                    td = context.trusted_data.get(field_name)
                else:
                    td = {}
                if _model_mapping:
                    submap = _model_mapping.get(field_name)
                else:
                    submap = {}
                field_context = context._branch(trusted_data=td, mapping=submap)
            else:
                field_context = context
            try:
                value = _field_converter(field, value, field_context)
            except (FieldError, CompoundError) as exc:
                errors[serialized_field_name] = exc
                if isinstance(exc, DataError):
                    data[field_name] = exc.partial_data
                continue

        if not context.oo and value is Undefined:
            continue

        data[field_name] = value

    if errors:
        partial_data = dict(((key, value) for key, value in data.items() if value is not Undefined))
        raise DataError(errors, partial_data)

    data = context.field_converter.post(cls, data, context)

    return data
dataset/ETHPy150Open schematics/schematics/schematics/transforms.py/import_loop/swapped operands of `-`