diff options
| author | Jason Madden <jamadden@gmail.com> | 2020-02-08 07:57:35 -0600 |
|---|---|---|
| committer | Jason Madden <jamadden@gmail.com> | 2020-02-08 07:57:35 -0600 |
| commit | f6d2e9445d47dd104c146157065169be8df4d409 (patch) | |
| tree | 0e8f22680524d135f8af30c720c3057f6397c07f /docs | |
| parent | d6343eeaa7f67838db9de82974898b80938a7aef (diff) | |
| download | zope-interface-issue171.tar.gz | |
Make verifyObject/Class collect and raise all errors instead of only the first.issue171
Fixes #171.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/verify.rst | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/docs/verify.rst b/docs/verify.rst index 6f48218..a921b8b 100644 --- a/docs/verify.rst +++ b/docs/verify.rst @@ -23,7 +23,7 @@ Attributes of the object, be they defined by its class or added by its .. doctest:: >>> from zope.interface import Interface, Attribute, implementer - >>> from zope.interface.exceptions import BrokenImplementation + >>> from zope.interface import Invalid >>> class IFoo(Interface): ... x = Attribute("The X attribute") ... y = Attribute("The Y attribute") @@ -47,7 +47,7 @@ exception. (We'll define a helper to make this easier to show.) ... foo = Foo() ... try: ... return verifyObject(IFoo, foo) - ... except BrokenImplementation as e: + ... except Invalid as e: ... print(e) >>> @implementer(IFoo) @@ -62,6 +62,18 @@ exception. (We'll define a helper to make this easier to show.) >>> verify_foo() The object <Foo...> has failed to implement interface <InterfaceClass ...IFoo>: The IFoo.x attribute was not provided. +If both attributes are missing, an exception is raised reporting +both errors. + +.. doctest:: + + >>> @implementer(IFoo) + ... class Foo(object): + ... pass + >>> verify_foo() + The object <Foo ...> has failed to implement interface <InterfaceClass ...IFoo>: + The IFoo.x attribute was not provided + The IFoo.y attribute was not provided If an attribute is implemented as a property that raises an ``AttributeError`` when trying to get its value, the attribute is considered missing: @@ -124,18 +136,7 @@ that takes one argument. If we don't provide it, we get an error. >>> verify_foo() The object <Foo...> has failed to implement interface <InterfaceClass builtins.IFoo>: The IFoo.simple(arg1) attribute was not provided. -Once they exist, they are checked for compatible signatures. This is a -different type of exception, so we need an updated helper. - -.. doctest:: - - >>> from zope.interface.exceptions import BrokenMethodImplementation - >>> def verify_foo(): - ... foo = Foo() - ... try: - ... return verifyObject(IFoo, foo) - ... except BrokenMethodImplementation as e: - ... print(e) +Once they exist, they are checked to be callable, and for compatible signatures. Not being callable is an error. @@ -143,7 +144,7 @@ Not being callable is an error. >>> Foo.simple = 42 >>> verify_foo() - The object <Foo...> violates its contract in IFoo.simple(arg1): implementation is not a method. + The object <Foo...> violates the contract of IFoo.simple(arg1) because implementation is not a method. Taking too few arguments is an error. @@ -151,7 +152,7 @@ Taking too few arguments is an error. >>> Foo.simple = lambda: "I take no arguments" >>> verify_foo() - The object <Foo...> violates its contract in IFoo.simple(arg1): implementation doesn't allow enough arguments. + The object <Foo...> violates the contract of IFoo.simple(arg1) because implementation doesn't allow enough arguments. Requiring too many arguments is an error. (Recall that the ``self`` argument is implicit.) @@ -160,7 +161,7 @@ argument is implicit.) >>> Foo.simple = lambda self, a, b: "I require two arguments" >>> verify_foo() - The object <Foo...> violates its contract in IFoo.simple(arg1): implementation requires too many arguments. + The object <Foo...> violates the contract of IFoo.simple(arg1) because implementation requires too many arguments. Variable arguments can be used to implement the required number, as can arguments with defaults. @@ -185,7 +186,7 @@ variable keyword arguments, the implementation must also accept them. ... class Foo(object): ... def needs_kwargs(self, a=1, b=2): pass >>> verify_foo() - The object <Foo...> violates its contract in IFoo.needs_kwargs(**kwargs): implementation doesn't support keyword arguments. + The object <Foo...> violates the contract of IFoo.needs_kwargs(**kwargs) because implementation doesn't support keyword arguments. >>> class IFoo(Interface): ... def needs_varargs(*args): pass @@ -193,15 +194,32 @@ variable keyword arguments, the implementation must also accept them. ... class Foo(object): ... def needs_varargs(self, **kwargs): pass >>> verify_foo() - The object <Foo...> violates its contract in IFoo.needs_varargs(*args): implementation doesn't support variable arguments. + The object <Foo...> violates the contract of IFoo.needs_varargs(*args) because implementation doesn't support variable arguments. + + +Of course, missing attributes are also found and reported. + +.. doctest:: + + >>> class IFoo(Interface): + ... x = Attribute('The X attribute') + ... def method(arg1): "Takes one positional argument" + >>> @implementer(IFoo) + ... class Foo(object): + ... def method(self): "I don't have enough arguments" + >>> verify_foo() + The object <Foo...> has failed to implement interface <InterfaceClass ...IFoo>: + The IFoo.x attribute was not provided + violates the contract of IFoo.method(arg1) because implementation doesn't allow enough arguments Verifying Classes ================= The function `verifyClass` is used to check that a class implements an interface properly, meaning that its instances properly provide the -interface. Most of the same things that `verifyObject` checks can be -checked for classes. +interface. Many of the same things that `verifyObject` checks can be +checked for classes, but certain conditions, such as the presence of +attributes, cannot be verified. .. autofunction:: verifyClass @@ -211,8 +229,8 @@ checked for classes. >>> def verify_foo_class(): ... try: ... return verifyClass(IFoo, Foo) - ... except BrokenMethodImplementation as e: + ... except Invalid as e: ... print(e) >>> verify_foo_class() - The object <class 'Foo'> violates its contract in IFoo.needs_varargs(*args): implementation doesn't support variable arguments. + The object <class 'Foo'> violates the contract of IFoo.method(arg1) because implementation doesn't allow enough arguments. |
