How to use the icontract.invariant 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 / tests / test_invariant.py View on Github external
def test_disabled(self) -> None:
        @icontract.invariant(lambda self: self.x > 0, enabled=False)
        class SomeClass:
            def __init__(self) -> None:
                self.x = -1

        inst = SomeClass()
        self.assertEqual(-1, inst.x)
github Parquery / icontract / tests / test_inheritance_invariant.py View on Github external
def test_inherited_invariant_on_setter(self) -> None:
        @icontract.invariant(lambda self: not self.toggled)
        class SomeBase(icontract.DBC):
            def __init__(self) -> None:
                self.toggled = False

        class SomeClass(SomeBase):
            @property
            def some_prop(self) -> int:
                return 0

            @some_prop.setter
            def some_prop(self, value: int) -> None:
                self.toggled = True

            def __repr__(self) -> str:
                return self.__class__.__name__
github Parquery / icontract / tests / test_invariant.py View on Github external
def test_as_function(self) -> None:
        @icontract.invariant(
            lambda self: self.x > 0, error=lambda self: ValueError("x must be positive, but got: {}".format(self.x)))
        class A:
            def __init__(self) -> None:
                self.x = 0

            def __repr__(self) -> str:
                return self.__class__.__name__

        value_error = None  # type: Optional[ValueError]
        try:
            _ = A()
        except ValueError as err:
            value_error = err

        self.assertIsNotNone(value_error)
        self.assertIsInstance(value_error, ValueError)
github Parquery / icontract / tests / test_invariant.py View on Github external
def test_inv_broken_before_private_method(self) -> None:
        @icontract.invariant(lambda self: self.x > 0)
        class SomeClass:
            def __init__(self) -> None:
                self.x = 100

            # A private method can not expect the invariant to hold.
            def __some_private_method(self) -> None:
                pass

            def some_method(self) -> None:
                self.x = -1
                self.__some_private_method()
                self.x = 10

        inst = SomeClass()
        inst.some_method()
        self.assertEqual(10, inst.x)
github Parquery / icontract / tests / test_inheritance_postcondition.py View on Github external
def test_count_checks_in_slot_wrappers(self) -> None:
        class Increment:
            count = 0

            def __call__(self) -> bool:
                Increment.count += 1
                return True

        inc = Increment()

        @icontract.invariant(lambda self: inc())
        class SomeClass:
            pass

        inst = SomeClass()
        self.assertEqual(1, Increment.count)  # Invariant needs to be checked once after the initialization.

        _ = str(inst)
        self.assertEqual(3, Increment.count)  # Invariant needs to be checked before and after __str__.
github Parquery / icontract / benchmarks / import_cost / classes_100_with_10_invariants.py View on Github external
@icontract.invariant(lambda self: self.x > 5)
@icontract.invariant(lambda self: self.x > 6)
@icontract.invariant(lambda self: self.x > 7)
@icontract.invariant(lambda self: self.x > 8)
@icontract.invariant(lambda self: self.x > 9)
class SomeClass3:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0)
@icontract.invariant(lambda self: self.x > 1)
@icontract.invariant(lambda self: self.x > 2)
@icontract.invariant(lambda self: self.x > 3)
@icontract.invariant(lambda self: self.x > 4)
@icontract.invariant(lambda self: self.x > 5)
@icontract.invariant(lambda self: self.x > 6)
@icontract.invariant(lambda self: self.x > 7)
@icontract.invariant(lambda self: self.x > 8)
@icontract.invariant(lambda self: self.x > 9)
class SomeClass4:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0)
github Parquery / icontract / benchmarks / import_cost / classes_100_with_5_invariants.py View on Github external
@icontract.invariant(lambda self: self.x > 0)
@icontract.invariant(lambda self: self.x > 1)
@icontract.invariant(lambda self: self.x > 2)
@icontract.invariant(lambda self: self.x > 3)
@icontract.invariant(lambda self: self.x > 4)
class SomeClass13:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0)
@icontract.invariant(lambda self: self.x > 1)
@icontract.invariant(lambda self: self.x > 2)
@icontract.invariant(lambda self: self.x > 3)
@icontract.invariant(lambda self: self.x > 4)
class SomeClass14:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0)
@icontract.invariant(lambda self: self.x > 1)
@icontract.invariant(lambda self: self.x > 2)
@icontract.invariant(lambda self: self.x > 3)
github Parquery / icontract / benchmarks / import_cost / classes_100_with_5_disabled_invariants.py View on Github external
@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
class SomeClass59:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass

@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
class SomeClass60:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass

@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
class SomeClass61:
    def __init__(self) -> None:
github Parquery / icontract / benchmarks / import_cost / classes_100_with_10_disabled_invariants.py View on Github external
def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
@icontract.invariant(lambda self: self.x > 5, enabled=False)
@icontract.invariant(lambda self: self.x > 6, enabled=False)
@icontract.invariant(lambda self: self.x > 7, enabled=False)
@icontract.invariant(lambda self: self.x > 8, enabled=False)
@icontract.invariant(lambda self: self.x > 9, enabled=False)
class SomeClass34:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
@icontract.invariant(lambda self: self.x > 5, enabled=False)
@icontract.invariant(lambda self: self.x > 6, enabled=False)
github Parquery / icontract / benchmarks / import_cost / classes_100_with_10_disabled_invariants.py View on Github external
#!/usr/bin/env python3
import icontract


@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
@icontract.invariant(lambda self: self.x > 5, enabled=False)
@icontract.invariant(lambda self: self.x > 6, enabled=False)
@icontract.invariant(lambda self: self.x > 7, enabled=False)
@icontract.invariant(lambda self: self.x > 8, enabled=False)
@icontract.invariant(lambda self: self.x > 9, enabled=False)
class SomeClass0:
    def __init__(self) -> None:
        self.x = 100

    def some_func(self) -> None:
        pass


@icontract.invariant(lambda self: self.x > 0, enabled=False)
@icontract.invariant(lambda self: self.x > 1, enabled=False)
@icontract.invariant(lambda self: self.x > 2, enabled=False)
@icontract.invariant(lambda self: self.x > 3, enabled=False)
@icontract.invariant(lambda self: self.x > 4, enabled=False)
@icontract.invariant(lambda self: self.x > 5, enabled=False)
@icontract.invariant(lambda self: self.x > 6, enabled=False)
@icontract.invariant(lambda self: self.x > 7, enabled=False)