diff --git a/docs/contracts/index.md b/docs/contracts/index.md index 6171e3dab..95ade1563 100644 --- a/docs/contracts/index.md +++ b/docs/contracts/index.md @@ -48,11 +48,9 @@ AssertionError: divide argument '2' did not match type annotation for parameter ## API -The `python_ta.contracts` module offers three functions for enabling contract checking. +The `python_ta.contracts` module offers two functions for enabling contract checking. The first, `check_all_contracts`, enables contract checking for all functions and classes defined within a module or set of modules. -The second, `check_contracts`, is a decorator allowing more fine-grained control over which -functions/classes have contract checking enabled. The third, `check_invariants`, takes in an object and checks -that the representation invariants of the object are satisfied. +The second, `check_contracts`, is a decorator allowing more fine-grained control over which functions/classes have contract checking enabled. ```{eval-rst} .. autofunction:: python_ta.contracts.check_all_contracts @@ -62,6 +60,8 @@ that the representation invariants of the object are satisfied. .. autofunction:: python_ta.contracts.check_contracts(func_or_class) ``` +You can pass an object into the function `validate_invariants` to manually check the representation invariants of the object. + ```{eval-rst} .. autofunction:: python_ta.contracts.check_invariants(object) ``` diff --git a/python_ta/contracts/__init__.py b/python_ta/contracts/__init__.py index f06b26124..a686f5644 100644 --- a/python_ta/contracts/__init__.py +++ b/python_ta/contracts/__init__.py @@ -610,7 +610,7 @@ def _set_invariants(klass: type) -> None: setattr(klass, "__representation_invariants__", rep_invariants) -def check_invariants(obj: object) -> None: +def validate_invariants(obj: object) -> None: """Check that the representation invariants of obj are satisfied.""" klass = obj.__class__ klass_mod = _get_module(klass) diff --git a/tests/test_check_invariants.py b/tests/test_validate_invariants.py similarity index 85% rename from tests/test_check_invariants.py rename to tests/test_validate_invariants.py index 50a450186..279ffe9d8 100644 --- a/tests/test_check_invariants.py +++ b/tests/test_validate_invariants.py @@ -6,7 +6,7 @@ import pytest -from python_ta.contracts import check_contracts, check_invariants +from python_ta.contracts import check_contracts, validate_invariants @check_contracts @@ -34,9 +34,9 @@ def test_no_errors() -> None: person = Person("Jim", 50, ["Pam", "Dwight"]) try: - check_invariants(person) - except Exception: - pytest.fail("check_invariants has incorrectly raised an error") + validate_invariants(person) + except AssertionError: + pytest.fail("check_invariants has incorrectly raised an AssertionError") def test_raise_error() -> None: @@ -45,4 +45,4 @@ def test_raise_error() -> None: person.friends.pop() with pytest.raises(AssertionError): - check_invariants(person) + validate_invariants(person)