def margherita(cls, radius): return cls(radius, ['mozzarella', 'tomatoes']) def area(self): return self.circle_area(self.radius) @staticmethod def circle_area(r): """ Precondition: r > 0 """ return r ** 2 * math.pi # Decorating everything in this file check_all_contracts(__name__, decorate_main = False) @pytest.fixture def person(): return Person("David", 31, ["Sushi"]) def test_change_age_invalid_over(person) -> None: """ Change the age to larger than 100. Expect an exception. """ with pytest.raises(AssertionError) as excinfo: person.change_age(200) msg = str(excinfo.value) assert 'age < 150' in msg
def setup_module() -> None: """Pytest hook for setting up the module""" check_all_contracts(__name__, decorate_main=False)
Preconditions: - age > 0 - len(name) > 0 - is_valid_name(name) - len(id) == 8 """ self.name = name self.age = age self.id = id if __name__ == '__main__': # "Magic function" to activate all contracts and representation invariants in the given file. # Can also import decorators "check_contracts" for functions and "add_class_invariants" for classes. import python_ta.contracts as contracts contracts.check_all_contracts() # This call works properly. # my_sum([100, 200, 300]) # This call will raise an error: the type contract is violated by the parameter. # my_sum('hello') # This call will raise an error: the first precondition is not satisfied. # my_sum([]) # This call will raise an error: the second precondition is not satisfied. # my_sum([1, 2, 3]) # This call will raise an error: the return type is violated. # my_sum2([100, 200, 300])
def run(): from python_ta.contracts import check_all_contracts check_all_contracts(__name__, decorate_main=False) divide(10, "x")