Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Collapse preconditions and postconditions from the bases with the the function's own ones
preconditions = _collapse_preconditions(
base_preconditions=base_preconditions,
bases_have_func=bases_have_func,
preconditions=preconditions,
func=func)
snapshots = _collapse_snapshots(base_snapshots=base_snapshots, snapshots=snapshots)
postconditions = _collapse_postconditions(
base_postconditions=base_postconditions, postconditions=postconditions)
if preconditions or postconditions:
if contract_checker is None:
contract_checker = icontract._checkers.decorate_with_checker(func=func)
# Replace the function with the function decorated with contract checks
if inspect.isfunction(value):
namespace[key] = contract_checker
elif isinstance(value, staticmethod):
namespace[key] = staticmethod(contract_checker)
elif isinstance(value, classmethod):
namespace[key] = classmethod(contract_checker)
else:
raise NotImplementedError("Unexpected value for a function: {}".format(value))
# Override the preconditions and postconditions
contract_checker.__preconditions__ = preconditions
contract_checker.__postcondition_snapshots__ = snapshots
:param func: function to be wrapped
:return: contract checker around ``func`` if no contract checker on the decorator stack, or ``func`` otherwise
"""
if not self.enabled:
return func
# Find a contract checker
contract_checker = icontract._checkers.find_checker(func=func)
if contract_checker is not None:
# Do not add an additional wrapper since the function has been already wrapped with a contract checker
result = func
else:
# Wrap the function with a contract checker
contract_checker = icontract._checkers.decorate_with_checker(func=func)
result = contract_checker
# Add the precondition to the list of preconditions stored at the checker
assert hasattr(contract_checker, "__preconditions__")
preconditions = getattr(contract_checker, "__preconditions__")
assert isinstance(preconditions, list)
assert len(preconditions) <= 1, \
("At most a single group of preconditions expected when wrapping with a contract checker. "
"The preconditions are merged only in the DBC metaclass. "
"The current number of precondition groups: {}").format(len(preconditions))
if len(preconditions) == 0:
# Create the first group if there is no group so far, i.e. this is the first decorator.
preconditions.append([])
preconditions[0].append(self._contract)
:param func: function to be wrapped
:return: contract checker around ``func`` if no contract checker on the decorator stack, or ``func`` otherwise
"""
if not self.enabled:
return func
# Find a contract checker
contract_checker = icontract._checkers.find_checker(func=func)
if contract_checker is not None:
# Do not add an additional wrapper since the function has been already wrapped with a contract checker
result = func
else:
# Wrap the function with a contract checker
contract_checker = icontract._checkers.decorate_with_checker(func=func)
result = contract_checker
# Add the postcondition to the list of postconditions stored at the checker
assert hasattr(contract_checker, "__postconditions__")
assert isinstance(getattr(contract_checker, "__postconditions__"), list)
getattr(contract_checker, "__postconditions__").append(self._contract)
return result
postconditions = contract_checker.__postconditions__
preconditions = _collapse_preconditions(
base_preconditions=base_preconditions,
bases_have_func=bases_have_func,
preconditions=preconditions,
func=func)
snapshots = _collapse_snapshots(base_snapshots=base_snapshots, snapshots=snapshots)
postconditions = _collapse_postconditions(
base_postconditions=base_postconditions, postconditions=postconditions)
if preconditions or postconditions:
if contract_checker is None:
contract_checker = icontract._checkers.decorate_with_checker(func=func)
# Replace the function with the function decorated with contract checks
if func == value.fget:
fget = contract_checker
elif func == value.fset:
fset = contract_checker
elif func == value.fdel:
fdel = contract_checker
else:
raise NotImplementedError("Unhandled case: func neither fget, fset nor fdel")
# Override the preconditions and postconditions
contract_checker.__preconditions__ = preconditions
contract_checker.__postcondition_snapshots__ = snapshots
contract_checker.__postconditions__ = postconditions