How to use the icontract.errors.ViolationError function in icontract

To help you get started, we’ve selected a few icontract 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 Parquery / icontract / icontract / _checkers.py View on Github external
msg_parts = []
                if contract.location is not None:
                    msg_parts.append("{}:\n".format(contract.location))

                msg_parts.append(
                    ("The argument(s) of the precondition error have not been set: {}. "
                     "Does the original function define them? Did you supply them in the call?").format(missing_args))

                raise TypeError(''.join(msg_parts))

            raise contract.error(**error_kwargs)

        else:
            msg = icontract._represent.generate_message(contract=contract, condition_kwargs=condition_kwargs)
            if contract.error is None:
                raise ViolationError(msg)
            elif isinstance(contract.error, type):
                raise contract.error(msg)
github Parquery / icontract / icontract / _checkers.py View on Github external
if 'result' in resolved_kwargs:
                    raise TypeError("Unexpected argument 'result' in a function decorated with postconditions.")

                if 'OLD' in resolved_kwargs:
                    raise TypeError("Unexpected argument 'OLD' in a function decorated with postconditions.")

            # Assert the preconditions in groups. This is necessary to implement "require else" logic when a class
            # weakens the preconditions of its base class.
            violation_err = None  # type: Optional[ViolationError]
            for group in preconditions:
                violation_err = None
                try:
                    for contract in group:
                        _assert_precondition(contract=contract, resolved_kwargs=resolved_kwargs)
                    break
                except ViolationError as err:
                    violation_err = err

            if violation_err is not None:
                raise violation_err  # pylint: disable=raising-bad-type

            # Capture the snapshots
            if postconditions and snapshots:
                old_as_mapping = dict()  # type: MutableMapping[str, Any]
                for snap in snapshots:
                    # This assert is just a last defense.
                    # Conflicting snapshot names should have been caught before, either during the decoration or
                    # in the meta-class.
                    assert snap.name not in old_as_mapping, "Snapshots with the conflicting name: {}"
                    old_as_mapping[snap.name] = _capture_snapshot(a_snapshot=snap, resolved_kwargs=resolved_kwargs)

                resolved_kwargs['OLD'] = _Old(mapping=old_as_mapping)
github Parquery / icontract / icontract / _checkers.py View on Github external
if contract.error is not None and (inspect.ismethod(contract.error) or inspect.isfunction(contract.error)):
            assert contract.error_arg_set is not None, "Expected error_arg_set non-None if contract.error a function."
            assert contract.error_args is not None, "Expected error_args non-None if contract.error a function."

            if 'self' in contract.error_arg_set:
                raise contract.error(self=instance)
            else:
                raise contract.error()
        else:
            if 'self' in contract.condition_arg_set:
                msg = icontract._represent.generate_message(contract=contract, condition_kwargs={"self": instance})
            else:
                msg = icontract._represent.generate_message(contract=contract, condition_kwargs=dict())

            if contract.error is None:
                raise ViolationError(msg)
            elif isinstance(contract.error, type):
                raise contract.error(msg)
            else:
                raise NotImplementedError("Unhandled contract.error: {}".format(contract.error))
github Parquery / icontract / icontract / __init__.py View on Github external
invariant = icontract._decorators.invariant

import icontract._globals
aRepr = icontract._globals.aRepr
SLOW = icontract._globals.SLOW

import icontract._metaclass
DBCMeta = icontract._metaclass.DBCMeta
DBC = icontract._metaclass.DBC

import icontract._types
_Contract = icontract._types.Contract
_Snapshot = icontract._types.Snapshot

import icontract.errors
ViolationError = icontract.errors.ViolationError
github Parquery / icontract / icontract / _checkers.py View on Github external
msg_parts = []
                if contract.location is not None:
                    msg_parts.append("{}:\n".format(contract.location))

                msg_parts.append(
                    ("The argument(s) of the postcondition error have not been set: {}. "
                     "Does the original function define them? Did you supply them in the call?").format(missing_args))

                raise TypeError(''.join(msg_parts))

            raise contract.error(**error_kwargs)

        else:
            msg = icontract._represent.generate_message(contract=contract, condition_kwargs=condition_kwargs)
            if contract.error is None:
                raise ViolationError(msg)
            elif isinstance(contract.error, type):
                raise contract.error(msg)