How to use the jsons.exceptions.UnfulfilledArgumentError function in jsons

To help you get started, we’ve selected a few jsons examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_load_namedtuple(self):
        dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
                                tzinfo=datetime.timezone.utc)
        T = NamedTuple('T', [('x', str), ('y', datetime.datetime)])
        t = T('test', dat)
        loaded = jsons.load(['test', '2018-07-08T21:34:00Z'], T)
        self.assertEqual(t, loaded)

        T._field_defaults = dict(y=dat)
        loaded2 = jsons.load(['test'], T)
        self.assertEqual(t, loaded)

        with self.assertRaises(UnfulfilledArgumentError):
            jsons.load([], T)
        try:
            jsons.load([], T)
        except UnfulfilledArgumentError as err:
            self.assertEqual([], err.source)
            self.assertEqual(T, err.target)
            self.assertEqual('x', err.argument)
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_exception_unfulfilled_arg(self):
        class C:
            def __init__(self, x, y):
                self.x = x
                self.y = y

        with self.assertRaises(UnfulfilledArgumentError):
            jsons.load({"x": 1}, C)

        try:
            jsons.load({"x": 1}, C)
        except UnfulfilledArgumentError as err:
            self.assertDictEqual({"x": 1}, err.source)
            self.assertEqual(C, err.target)
            self.assertEqual('y', err.argument)
github ramonhagenaars / jsons / jsons / deserializers / default_object.py View on Github external
elif sig_key in attr_getters:
        # There exists an attr_getter for this argument.
        attr_getter = attr_getters.pop(sig_key)
        result = sig_key, attr_getter()
    elif sig.default != inspect.Parameter.empty:
        # There is a default value for this argument.
        result = sig_key, sig.default
    elif sig.kind in (inspect.Parameter.VAR_POSITIONAL,
                      inspect.Parameter.VAR_KEYWORD):
        # This argument is either *args or **kwargs.
        result = None, None
    elif can_match_with_none(cls):
        # It is fine that there is no value.
        result = sig_key, None
    else:
        raise UnfulfilledArgumentError(
            'No value found for "{}"'.format(sig_key), sig_key, obj, orig_cls)
    return result
github ramonhagenaars / jsons / jsons / deserializers.py View on Github external
arg_cls = None
            if sig.annotation != inspect.Parameter.empty:
                arg_cls = sig.annotation
            value = _main_impl.load(obj[sig_key], arg_cls, **kwargs)
            constructor_args_in_obj[sig_key] = value
        elif sig_key in attr_getters:
            # There exists an attr_getter for this argument.
            attr_getter = attr_getters.pop(sig_key)
            constructor_args_in_obj[sig_key] = attr_getter()
        elif sig.default != inspect.Parameter.empty:
            # There is a default value for this argument.
            constructor_args_in_obj[sig_key] = sig.default
        elif sig.kind not in (inspect.Parameter.VAR_POSITIONAL,
                              inspect.Parameter.VAR_KEYWORD):
            # This argument is no *args or **kwargs and has no value.
            raise UnfulfilledArgumentError(
                'No value found for "{}"'.format(sig_key),
                sig_key, obj, cls)

    return constructor_args_in_obj, attr_getters
github ramonhagenaars / jsons / jsons / deserializers / default_tuple.py View on Github external
else:
            field = cls._field_defaults.get(field_name, None)

        # _field_types has been deprecated in favor of __annotations__ in Python 3.8
        if hasattr(cls, '__annotations__'):
            field_types = getattr(cls, '__annotations__', {})
        else:
            field_types = getattr(cls, '_field_types', {})

        if field is None:
            hint = field_types.get(field_name)
            if NoneType not in (get_union_params(hint) or []):
                # The value 'None' is not permitted here.
                msg = ('No value present in {} for argument "{}"'
                       .format(obj, field_name))
                raise UnfulfilledArgumentError(msg, field_name, obj, cls)
        cls_ = field_types.get(field_name) if field_types else None
        loaded_field = load(field, cls_, **kwargs)
        args.append(loaded_field)
    inst = cls(*args)
    return inst
github ramonhagenaars / jsons / jsons / deserializers.py View on Github external
that list.
    :param obj: the tuple that needs deserializing.
    :param cls: the NamedTuple.
    :param kwargs: any keyword arguments.
    :return: a deserialized named tuple (i.e. an instance of a class).
    """
    args = []
    for index, field_name in enumerate(cls._fields):
        if index < len(obj):
            field = obj[index]
        else:
            field = cls._field_defaults.get(field_name, None)
        if not field:
            msg = ('No value present in {} for argument "{}"'
                   .format(obj, field_name))
            raise UnfulfilledArgumentError(msg, field_name, obj, cls)
        cls_ = cls._field_types.get(field_name, None)
        loaded_field = _main_impl.load(field, cls_, **kwargs)
        args.append(loaded_field)
    inst = cls(*args)
    return inst