1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
=====================================
Verifying interface implementations
=====================================
The ``zope.interface.verify`` module provides functions that test whether a
given interface is implemented by a class or provided by an object.
.. currentmodule:: zope.interface.verify
Verifying objects
=================
.. autofunction:: verifyObject
.. autoexception:: zope.interface.Invalid
Let's demonstrate. We'll begin by defining a simple interface hierarchy
requiring two attributes, and a helper method that will instantiate and verify
that an object provides this interface.
.. doctest::
>>> from zope.interface import Interface, Attribute, implementer
>>> from zope.interface import Invalid
>>> from zope.interface.verify import verifyObject
>>> oname, __name__ = __name__, 'base' # Pretend we're in a module, not a doctest
>>> class IBase(Interface):
... x = Attribute("The X attribute")
>>> __name__ = 'module' # Pretend to be a different module.
>>> class IFoo(IBase):
... y = Attribute("The Y attribute")
>>> __name__ = oname; del oname
>>> class Foo(object):
... pass
>>> def verify_foo(**kwargs):
... foo = Foo()
... try:
... return verifyObject(IFoo, foo, **kwargs)
... except Invalid as e:
... print(e)
If we try to verify an instance of this ``Foo`` class, three errors
will be reported. The declarations (does the object provide ``IFoo``)
are checked, as are the attributes specified in the interface being
validated (and its ancestors). Notice that the interface being
verified is shown, as is the interface where the attribute was
defined.
.. doctest::
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo:
Does not declaratively implement the interface
The base.IBase.x attribute was not provided
The module.IFoo.y attribute was not provided
If we add the two missing attributes, we still have the error about not
declaring the correct interface.
.. doctest::
>>> Foo.x = Foo.y = 42
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: Does not declaratively implement the interface.
If we want to only check the structure of the object, without examining
its declarations, we can use the ``tentative`` argument.
.. doctest::
>>> verify_foo(tentative=True)
True
Of course, we can always mark a particular instance as providing the
desired interface.
.. doctest::
>>> from zope.interface import alsoProvides
>>> foo = Foo()
>>> alsoProvides(foo, IFoo)
>>> verifyObject(IFoo, foo)
True
If all instances will provide the interface, we can
mark a class as implementing it. But we have to remove the interface from the
instance first so a consistent interface resolution order can be achieved.
(Calling ``gc.collect()`` is also necessary because we use weakrefs.)
.. doctest::
>>> from zope.interface import classImplements
>>> from zope.interface import noLongerProvides
>>> import gc
>>> noLongerProvides(foo, IFoo)
>>> _ = gc.collect()
>>> classImplements(Foo, IFoo)
>>> verify_foo()
True
Testing for attributes
----------------------
Attributes of the object, be they defined by its class or added by its
``__init__`` method, will be recognized:
.. doctest::
>>> @implementer(IFoo)
... class Foo(object):
... x = 1
... def __init__(self):
... self.y = 2
>>> verifyObject(IFoo, Foo())
True
If either attribute is missing, verification will fail by raising an
exception.
.. doctest::
>>> @implementer(IFoo)
... class Foo(object):
... x = 1
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The module.IFoo.y attribute was not provided.
>>> @implementer(IFoo)
... class Foo(object):
... def __init__(self):
... self.y = 2
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The base.IBase.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 ...IFoo:
The base.IBase.x attribute was not provided
The module.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:
.. doctest::
>>> oname, __name__ = __name__, 'module'
>>> class IFoo(Interface):
... x = Attribute('The X attribute')
>>> __name__ = oname; del oname
>>> @implementer(IFoo)
... class Foo(object):
... @property
... def x(self):
... raise AttributeError
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The module.IFoo.x attribute was not provided.
Any other exception raised by a property will propagate to the caller of
``verifyObject``:
.. doctest::
>>> @implementer(IFoo)
... class Foo(object):
... @property
... def x(self):
... raise Exception
>>> verify_foo()
Traceback (most recent call last):
Exception
Of course, broken properties that are not required by the interface don't do
any harm:
.. doctest::
>>> @implementer(IFoo)
... class Foo(object):
... x = 1
... @property
... def y(self):
... raise Exception
>>> verify_foo()
True
Testing For Methods
-------------------
Methods are also validated to exist. We'll start by defining a method
that takes one argument. If we don't provide it, we get an error.
.. doctest::
>>> oname, __name__ = __name__, 'module'
>>> class IFoo(Interface):
... def simple(arg1): "Takes one positional argument"
>>> __name__ = oname; del oname
>>> @implementer(IFoo)
... class Foo(object):
... pass
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The module.IFoo.simple(arg1) attribute was not provided.
Once they exist, they are checked to be callable, and for compatible signatures.
Not being callable is an error.
.. doctest::
>>> Foo.simple = 42
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The contract of module.IFoo.simple(arg1) is violated because '42' is not a method.
Taking too few arguments is an error. (Recall that the ``self``
argument is implicit.)
.. doctest::
>>> Foo.simple = lambda self: "I take no arguments"
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The contract of module.IFoo.simple(arg1) is violated because '<lambda>()' doesn't allow enough arguments.
Requiring too many arguments is an error.
.. doctest::
>>> Foo.simple = lambda self, a, b: "I require two arguments"
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The contract of module.IFoo.simple(arg1) is violated because '<lambda>(a, b)' requires too many arguments.
Variable arguments can be used to implement the required number, as
can arguments with defaults.
.. doctest::
>>> Foo.simple = lambda self, *args: "Varargs work."
>>> verify_foo()
True
>>> Foo.simple = lambda self, a=1, b=2: "Default args work."
>>> verify_foo()
True
If our interface defines a method that uses variable positional or
variable keyword arguments, the implementation must also accept them.
.. doctest::
>>> oname, __name__ = __name__, 'module'
>>> class IFoo(Interface):
... def needs_kwargs(**kwargs): pass
>>> __name__ = oname; del oname
>>> @implementer(IFoo)
... class Foo(object):
... def needs_kwargs(self, a=1, b=2): pass
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The contract of module.IFoo.needs_kwargs(**kwargs) is violated because 'Foo.needs_kwargs(a=1, b=2)' doesn't support keyword arguments.
>>> oname, __name__ = __name__, 'module'
>>> class IFoo(Interface):
... def needs_varargs(*args): pass
>>> __name__ = oname; del oname
>>> @implementer(IFoo)
... class Foo(object):
... def needs_varargs(self, **kwargs): pass
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo: The contract of module.IFoo.needs_varargs(*args) is violated because 'Foo.needs_varargs(**kwargs)' doesn't support variable arguments.
Of course, missing attributes are also found and reported, and the
source interface of the missing attribute is included. Similarly, when
the failing method is from a parent class, that is also reported.
.. doctest::
>>> oname, __name__ = __name__, 'base'
>>> class IBase(Interface):
... def method(arg1): "Takes one positional argument"
>>> __name__ = 'module'
>>> class IFoo(IBase):
... x = Attribute('The X attribute')
>>> __name__ = oname; del oname
>>> class Base(object):
... def method(self): "I don't have enough arguments"
>>> @implementer(IFoo)
... class Foo(Base):
... pass
>>> verify_foo()
The object <...Foo...> has failed to implement interface ...IFoo:
The contract of base.IBase.method(arg1) is violated because 'Base.method()' doesn't allow enough arguments
The module.IFoo.x attribute was not provided
Verifying Classes
=================
The function `verifyClass` is used to check that a class implements
an interface properly, meaning that its instances properly provide the
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
.. doctest::
>>> from zope.interface.verify import verifyClass
>>> def verify_foo_class():
... try:
... return verifyClass(IFoo, Foo)
... except Invalid as e:
... print(e)
>>> verify_foo_class()
The object <class 'Foo'> has failed to implement interface ...IFoo: The contract of base.IBase.method(arg1) is violated because 'Base.method(self)' doesn't allow enough arguments.
|