def test_checking_before_args_available(): """When checking arguments before a method call, the checks for some arguments may be ignored by passing NotYetAvailable instances for them. The name of NotYetAvailable arguments should match the name of the argument it replaces.""" class ModelObject: @arg_checks(y=IsInstance(int), title=IsInstance(str)) def do_something(self, x, y, title='a title', style=None): pass model_object = ModelObject() with expected(NoException): ArgumentCheckedCallable(model_object.do_something).checkargs('x', NotYetAvailable('y'), title='a title') with expected(IsInstance): ArgumentCheckedCallable(model_object.do_something).checkargs('x', NotYetAvailable('y'), title=123) with expected(IncorrectArgumentError): ArgumentCheckedCallable(model_object.do_something, explanation='an explanation').checkargs('x', NotYetAvailable('x'), title='a valid title')
def test_argument_checks_with_deprecated_methods(): """When used with @deprecated, argument checks still work.""" @deprecated('this test class is deprecated', '1.2') class ADeprecatedClass(object): @arg_checks(y=IsInstance(int), title=IsInstance(six.string_types)) def __init__(self, x, y, title='a title', style=None): pass with expected(IsInstance): ADeprecatedClass(1, 'y') with expected(IsInstance): ArgumentCheckedCallable(ADeprecatedClass).checkargs('x', 'y') with expected(IncorrectArgumentError): ArgumentCheckedCallable(ADeprecatedClass, explanation='an explanation').checkargs( 'x', NotYetAvailable('x'), title='a valid title') class ADeprecatedClass(object): @deprecated('this instance method is deprecated', '1.3') @arg_checks(y=IsInstance(int), title=IsInstance(six.string_types)) def instance_method(self, x, y, title='a title', style=None): pass @deprecated('this class method is deprecated', '2.3') @arg_checks(y=IsInstance(int), title=IsInstance(six.string_types)) @classmethod def class_method(cls, x, y, title='a title', style=None): pass with expected(IsInstance): ADeprecatedClass().instance_method('x', 'y') with expected(IsInstance): ADeprecatedClass.class_method('x', 'y')
def correct_unbound_method_called(self): self.expected_exception = NoException self.args = (NotYetAvailable('self'), 1, 2) self.call_args = (self.ModelObject(), 1, 2) self.kwargs = {} self.callable = self.ModelObject.do_something