Declaring and Checking The Interfaces of Objects
Declaring what interfaces an object implements or provides, and later being able to check those, is an important part of this package. Declaring interfaces, in particular, can be done both statically at object definition time and dynamically later on.
The functionality that allows declaring and checking interfaces is
provided directly in the zope.interface
module. It is described by
the interface zope.interface.interfaces.IInterfaceDeclaration
. We
will first look at that interface, and then we will look more
carefully at each object it documents, including providing examples.
- interface zope.interface.interfaces.IInterfaceDeclaration[source]
Declare and check the interfaces of objects.
The functions defined in this interface are used to declare the interfaces that objects provide and to query the interfaces that have been declared.
Interfaces can be declared for objects in two ways:
Interfaces are declared for instances of the object’s class
Interfaces are declared for the object directly.
The interfaces declared for an object are, therefore, the union of interfaces declared for the object directly and the interfaces declared for instances of the object’s class.
Note that we say that a class implements the interfaces provided by it’s instances. An instance can also provide interfaces directly. The interfaces provided by an object are the union of the interfaces provided directly and the interfaces implemented by the class.
This interface is implemented by
zope.interface
.- Interface
The base class used to create new interfaces
- taggedValue(key, value)
Attach a tagged value to an interface while defining the interface.
This is a way of executing
IElement.setTaggedValue()
from the definition of the interface. For example:class IFoo(Interface): taggedValue('key', 'value')
See also
- invariant(checker_function)
Attach an invariant checker function to an interface while defining it.
Invariants can later be validated against particular implementations by calling
IInterface.validateInvariants()
.For example:
def check_range(ob): if ob.max < ob.min: raise ValueError("max value is less than min value") class IRange(Interface): min = Attribute("The min value") max = Attribute("The max value") invariant(check_range)
See also
- interfacemethod(method)
A decorator that transforms a method specification into an implementation method.
This is used to override methods of
Interface
or provide new methods. Definitions using this decorator will not appear inIInterface.names()
. It is possible to have an implementation method and a method specification of the same name.For example:
class IRange(Interface): @interfacemethod def __adapt__(self, obj): if isinstance(obj, range): # Return the builtin ``range`` as-is return obj return super(type(IRange), self).__adapt__(obj)
You can use
super
to call the parent class functionality. Note that the zero-argument version (super().__adapt__
) works on Python 3.6 and above, but prior to that the two-argument version must be used, and the class must be explicitly passed as the first argument.New in version 5.1.0.
See also
- providedBy(ob)
Return the interfaces provided by an object.
This is the union of the interfaces directly provided by an object and interfaces implemented by it’s class.
The value returned is an
IDeclaration
.See also
- implementedBy(class_)
Return the interfaces implemented for a class’s instances.
The value returned is an
IDeclaration
.See also
- classImplements(class_, *interfaces)
Declare additional interfaces implemented for instances of a class.
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared.
Consider the following example:
class C(A, B): ... classImplements(C, I1, I2)
Instances of
C
provideI1
,I2
, and whatever interfaces instances ofA
andB
provide. This is equivalent to:@implementer(I1, I2) class C(A, B): pass
See also
See also
- classImplementsFirst(cls, interface)
- implementer(*interfaces)
Create a decorator for declaring interfaces implemented by a factory.
A callable is returned that makes an implements declaration on objects passed to it.
See also
- classImplementsOnly(class_, *interfaces)
Declare the only interfaces implemented by instances of a class.
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) replace any previous declarations.
Consider the following example:
class C(A, B): ... classImplements(C, IA, IB. IC) classImplementsOnly(C. I1, I2)
Instances of
C
provide onlyI1
,I2
, and regardless of whatever interfaces instances ofA
andB
implement.See also
- implementer_only(*interfaces)
Create a decorator for declaring the only interfaces implemented.
A callable is returned that makes an implements declaration on objects passed to it.
See also
- directlyProvidedBy(object)
Return the interfaces directly provided by the given object.
The value returned is an
IDeclaration
.See also
- directlyProvides(object, *interfaces)
Declare interfaces declared directly for an object.
The arguments after the object are one or more interfaces or interface specifications (
IDeclaration
objects).Caution
The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object. See
alsoProvides()
to add additional interfaces.Consider the following example:
class C(A, B): ... ob = C() directlyProvides(ob, I1, I2)
The object,
ob
providesI1
,I2
, and whatever interfaces instances have been declared for instances ofC
.To remove directly provided interfaces, use
directlyProvidedBy
and subtract the unwanted interfaces. For example:directlyProvides(ob, directlyProvidedBy(ob)-I2)
removes I2 from the interfaces directly provided by
ob
. The object,ob
no longer directly providesI2
, although it might still provideI2
if it’s class implementsI2
.To add directly provided interfaces, use
directlyProvidedBy
and include additional interfaces. For example:directlyProvides(ob, directlyProvidedBy(ob), I2)
adds I2 to the interfaces directly provided by ob.
See also
- alsoProvides(object, *interfaces)
Declare additional interfaces directly for an object.
For example:
alsoProvides(ob, I1)
is equivalent to:
directlyProvides(ob, directlyProvidedBy(ob), I1)
See also
- noLongerProvides(object, interface)
Remove an interface from the list of an object’s directly provided interfaces.
For example:
noLongerProvides(ob, I1)
is equivalent to:
directlyProvides(ob, directlyProvidedBy(ob) - I1)
with the exception that if
I1
is an interface that is provided byob
through the class’s implementation,ValueError
is raised.See also
- provider(*interfaces)
Declare interfaces provided directly by a class.
See also
- moduleProvides(*interfaces)
Declare interfaces provided by a module.
This function is used in a module definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The given interfaces (including the interfaces in the specifications) are used to create the module’s direct-object interface specification. An error will be raised if the module already has an interface specification. In other words, it is an error to call this function more than once in a module definition.
This function is provided for convenience. It provides a more convenient way to call
directlyProvides
for a module. For example:moduleImplements(I1)
is equivalent to:
directlyProvides(sys.modules[__name__], I1)
See also
- Declaration(*interfaces)
Create an interface specification.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).A new interface specification (
IDeclaration
) with the given interfaces is returned.See also
Declaring Interfaces
To declare an interface itself, extend the Interface
base class.
- interface zope.interface.Interface
- zope.interface.taggedValue(key, value)[source]
Attaches a tagged value to an interface at definition time.
- zope.interface.invariant(call)[source]
- zope.interface.interfacemethod(func)[source]
Convert a method specification to an actual method of the interface.
This is a decorator that functions like
staticmethod
et al.The primary use of this decorator is to allow interface definitions to define the
__adapt__
method, but other interface methods can be overridden this way too.
Declaring The Interfaces of Objects
implementer
- class zope.interface.implementer(*interfaces)[source]
Bases:
object
Declare the interfaces implemented by instances of a class.
This function is called as a class decorator.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared, unless the interface is already implemented.
Previous declarations include declarations for base classes unless implementsOnly was used.
This function is provided for convenience. It provides a more convenient way to call
classImplements
. For example:@implementer(I1) class C(object): pass
is equivalent to calling:
classImplements(C, I1)
after the class has been created.
See also
classImplements
The change history provided there applies to this function too.
implementer_only
- class zope.interface.implementer_only(*interfaces)[source]
Bases:
object
Declare the only interfaces implemented by instances of a class
This function is called as a class decorator.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).Previous declarations including declarations for base classes are overridden.
This function is provided for convenience. It provides a more convenient way to call
classImplementsOnly
. For example:@implementer_only(I1) class C(object): pass
is equivalent to calling:
classImplementsOnly(I1)
after the class has been created.
classImplementsOnly
- zope.interface.classImplementsOnly(cls, *interfaces)[source]
Declare the only interfaces implemented by instances of a class
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) replace any previous declarations, including inherited definitions. If you wish to preserve inherited declarations, you can pass
implementedBy(cls)
in interfaces. This can be used to alter the interface resolution order.
Consider the following example:
>>> from zope.interface import implementedBy
>>> from zope.interface import implementer
>>> from zope.interface import classImplementsOnly
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> @implementer(I3)
... class A(object):
... pass
>>> @implementer(I4)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsOnly(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2']
Instances of C
provide only I1
, I2
, and regardless of
whatever interfaces instances of A
and B
implement.
classImplements
- zope.interface.classImplements(cls, *interfaces)[source]
Declare additional interfaces implemented for instances of a class
The arguments after the class are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to any interfaces previously declared. An effort is made to keep a consistent C3 resolution order, but this cannot be guaranteed.
Changed in version 5.0.0: Each individual interface in interfaces may be added to either the beginning or end of the list of interfaces declared for cls, based on inheritance, in order to try to maintain a consistent resolution order. Previously, all interfaces were added to the end.
Changed in version 5.1.0: If cls is already declared to implement an interface (or derived interface) in interfaces through inheritance, the interface is ignored. Previously, it would redundantly be made direct base of cls, which often produced inconsistent interface resolution orders. Now, the order will be consistent, but may change. Also, if the
__bases__
of the cls are later changed, the cls will no longer be considered to implement such an interface (changing the__bases__
of cls has never been supported).
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import classImplements
>>> from zope.interface.ro import is_consistent
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(IA)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplements(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'IA', 'IB']
Instances of C
provide I1
and I2
, plus whatever
instances of A
and B
provide.
>>> classImplements(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'I5', 'IA', 'IB']
Instances of C
now also provide I5
. Notice how I5
was
added to the end of the list of things provided directly by C
.
If we ask a class to implement an interface that extends an interface it already implements, that interface will go at the beginning of the list, in order to preserve a consistent resolution order.
>>> class I6(I5): pass
>>> class I7(IA): pass
>>> classImplements(C, I6, I7)
>>> [i.getName() for i in implementedBy(C)]
['I6', 'I1', 'I2', 'I5', 'I7', 'IA', 'IB']
>>> is_consistent(implementedBy(C))
True
This cannot be used to introduce duplicates.
>>> classImplements(C, IA, IB, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I6', 'I1', 'I2', 'I5', 'I7', 'IA', 'IB']
classImplementsFirst
- zope.interface.classImplementsFirst(cls, iface)[source]
Declare that instances of cls additionally provide iface.
The second argument is an interface or interface specification. It is added as the highest priority (first in the IRO) interface; no attempt is made to keep a consistent resolution order.
New in version 5.0.0.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import classImplements
>>> from zope.interface import classImplementsFirst
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(IA)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsFirst(C, I2)
>>> classImplementsFirst(C, I1)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2', 'IA', 'IB']
Instances of C
provide I1
, I2
, I5
, and whatever
interfaces instances of A
and B
provide.
>>> classImplementsFirst(C, I5)
>>> [i.getName() for i in implementedBy(C)]
['I5', 'I1', 'I2', 'IA', 'IB']
Instances of C
now also provide I5
. Notice how I5
was
added to the beginning of the list of things provided directly by
C
. Unlike classImplements
, this ignores interface inheritance
and does not attempt to ensure a consistent resolution order (except
that it continues to elide interfaces already implemented through
class inheritance):
.. doctest::
>>> class IBA(IB, IA):
... pass
>>> classImplementsFirst(C, IBA)
>>> classImplementsFirst(C, IA)
>>> [i.getName() for i in implementedBy(C)]
['IBA', 'I5', 'I1', 'I2', 'IA', 'IB']
This cannot be used to introduce duplicates.
>>> len(implementedBy(C).declared)
4
>>> classImplementsFirst(C, IA)
>>> classImplementsFirst(C, IBA)
>>> classImplementsFirst(C, IA)
>>> classImplementsFirst(C, IBA)
>>> [i.getName() for i in implementedBy(C)]
['IBA', 'I5', 'I1', 'I2', 'IA', 'IB']
>>> len(implementedBy(C).declared)
4
directlyProvides
- zope.interface.directlyProvides(object, *interfaces)[source]
Declare interfaces declared directly for an object
The arguments after the object are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) replace interfaces previously declared for the object.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import providedBy
>>> from zope.interface import directlyProvides
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> @implementer(IA1, IA2)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> @implementer(IC)
... class C(A, B):
... pass
>>> ob = C()
>>> directlyProvides(ob, I1, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
The object, ob
provides I1
, I2
, and whatever interfaces
instances have been declared for instances of C
.
To remove directly provided interfaces, use directlyProvidedBy
and
subtract the unwanted interfaces. For example:
>>> from zope.interface import directlyProvidedBy
>>> directlyProvides(ob, directlyProvidedBy(ob)-I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0
removes I2
from the interfaces directly provided by ob
. The object,
ob
no longer directly provides I2
, although it might still
provide I2
if its class implements I2
.
To add directly provided interfaces, use directlyProvidedBy
and
include additional interfaces. For example:
>>> int(I2 in providedBy(ob))
0
>>> from zope.interface import directlyProvidedBy
>>> directlyProvides(ob, directlyProvidedBy(ob), I2)
adds I2
to the interfaces directly provided by ob
:
>>> int(I2 in providedBy(ob))
1
We need to avoid setting this attribute on meta classes that don’t support descriptors.
We can do away with this check when we get rid of the old EC
alsoProvides
- zope.interface.alsoProvides(object, *interfaces)[source]
Declare interfaces declared directly for an object
The arguments after the object are one or more interfaces or interface specifications (
IDeclaration
objects).The interfaces given (including the interfaces in the specifications) are added to the interfaces previously declared for the object.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import alsoProvides
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class IA1(Interface): pass
...
>>> class IA2(Interface): pass
...
>>> class IB(Interface): pass
...
>>> class IC(Interface): pass
...
>>> @implementer(IA1, IA2)
... class A(object):
... pass
>>> @implementer(IB)
... class B(object):
... pass
>>> @implementer(IC)
... class C(A, B):
... pass
>>> ob = C()
>>> directlyProvides(ob, I1)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
0
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
>>> alsoProvides(ob, I2)
>>> int(I1 in providedBy(ob))
1
>>> int(I2 in providedBy(ob))
1
>>> int(IA1 in providedBy(ob))
1
>>> int(IA2 in providedBy(ob))
1
>>> int(IB in providedBy(ob))
1
>>> int(IC in providedBy(ob))
1
The object, ob
provides I1
, I2
, and whatever interfaces
instances have been declared for instances of C
. Notice that the
alsoProvides
just extends the provided interfaces.
noLongerProvides
- zope.interface.noLongerProvides(object, interface)[source]
Removes a directly provided interface from an object.
Consider the following two interfaces:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
I1
is provided through the class, I2
is directly provided
by the object:
>>> @implementer(I1)
... class C(object):
... pass
>>> c = C()
>>> alsoProvides(c, I2)
>>> I2.providedBy(c)
True
Remove I2
from c
again:
>>> from zope.interface import noLongerProvides
>>> noLongerProvides(c, I2)
>>> I2.providedBy(c)
False
Removing an interface that is provided through the class is not possible:
>>> noLongerProvides(c, I1)
Traceback (most recent call last):
...
ValueError: Can only remove directly provided interfaces.
provider
- class zope.interface.provider(*interfaces)[source]
Bases:
object
Declare interfaces provided directly by a class
This function is called in a class definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The given interfaces (including the interfaces in the specifications) are used to create the class’s direct-object interface specification. An error will be raised if the module class has an direct interface specification. In other words, it is an error to call this function more than once in a class definition.
Note that the given interfaces have nothing to do with the interfaces implemented by instances of the class.
This function is provided for convenience. It provides a more convenient way to call
directlyProvides
for a class. For example:@provider(I1) class C: pass
is equivalent to calling:
directlyProvides(C, I1)
after the class has been created.
For example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> from zope.interface import provider
>>> class IFooFactory(Interface):
... pass
>>> class IFoo(Interface):
... pass
>>> @implementer(IFoo)
... @provider(IFooFactory)
... class C(object):
... pass
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
Which is equivalent to:
>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> @implementer(IFoo)
... class C(object):
... pass
>>> directlyProvides(C, IFooFactory)
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']
moduleProvides
- zope.interface.moduleProvides(*interfaces)[source]
Declare interfaces provided by a module
This function is used in a module definition.
The arguments are one or more interfaces or interface specifications (
IDeclaration
objects).The given interfaces (including the interfaces in the specifications) are used to create the module’s direct-object interface specification. An error will be raised if the module already has an interface specification. In other words, it is an error to call this function more than once in a module definition.
This function is provided for convenience. It provides a more convenient way to call directlyProvides. For example:
moduleProvides(I1)
is equivalent to:
directlyProvides(sys.modules[__name__], I1)
named
For example:
>>> from zope.interface.declarations import named
>>> @named('foo')
... class Foo(object):
... pass
>>> Foo.__component_name__
'foo'
When registering an adapter or utility component, the registry looks for the
__component_name__
attribute and uses it, if no name was explicitly
provided.
Querying The Interfaces Of Objects
All of these functions return an
IDeclaration
.
You’ll notice that an IDeclaration
is a type of
ISpecification
, as is
zope.interface.Interface
, so they share some common behaviour.
- interface zope.interface.interfaces.IDeclaration[source]
Extends:
zope.interface.interfaces.ISpecification
Interface declaration
Declarations are used to express the interfaces implemented by classes or provided by objects.
- __contains__(interface)
Test whether an interface is in the specification
Return true if the given interface is one of the interfaces in the specification and false otherwise.
- __iter__()
Return an iterator for the interfaces in the specification
- flattened()
Return an iterator of all included and extended interfaces
An iterator is returned for all interfaces either included in or extended by interfaces included in the specifications without duplicates. The interfaces are in “interface resolution order”. The interface resolution order is such that base interfaces are listed after interfaces that extend them and, otherwise, interfaces are included in the order that they were defined in the specification.
- __sub__(interfaces)
Create an interface specification with some interfaces excluded
The argument can be an interface or an interface specifications. The interface or interfaces given in a specification are subtracted from the interface specification.
Removing an interface that is not in the specification does not raise an error. Doing so has no effect.
Removing an interface also removes sub-interfaces of the interface.
- __add__(interfaces)
Create an interface specification with some interfaces added
The argument can be an interface or an interface specifications. The interface or interfaces given in a specification are added to the interface specification.
Adding an interface that is already in the specification does not raise an error. Doing so has no effect.
- __nonzero__()
Return a true value of the interface specification is non-empty
implementedBy
- zope.interface.implementedBy(cls)[source]
Return the interfaces implemented for a class’ instances
The value returned is an
IDeclaration
.
Consider the following example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer
>>> from zope.interface import classImplementsOnly
>>> from zope.interface import implementedBy
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I4(Interface): pass
...
>>> @implementer(I3)
... class A(object):
... pass
>>> @implementer(I4)
... class B(object):
... pass
>>> class C(A, B):
... pass
>>> classImplementsOnly(C, I1, I2)
>>> [i.getName() for i in implementedBy(C)]
['I1', 'I2']
Instances of C
provide only I1
, I2
, and regardless of
whatever interfaces instances of A
and B
implement.
Another example:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> @implementer(I2)
... class C1(object):
... pass
>>> @implementer(I3)
... class C2(C1):
... pass
>>> [i.getName() for i in implementedBy(C2)]
['I3', 'I2']
Really, any object should be able to receive a successful answer, even an instance:
>>> class Callable(object):
... def __call__(self):
... return self
>>> implementedBy(Callable())
classImplements(builtins.?)
Note that the name of the spec ends with a ‘?’, because the Callable
instance does not have a __name__
attribute.
This also manages storage of implementation specifications.
providedBy
directlyProvidedBy
- zope.interface.directlyProvidedBy(object)[source]
Return the interfaces directly provided by the given object
The value returned is an
IDeclaration
.
Classes
Declarations
Declaration objects implement the API defined by
IDeclaration
.
- class zope.interface.Declaration(*bases)[source]
Bases:
Specification
Interface declarations
Exmples for Declaration.__contains__()
:
>>> from zope.interface.declarations import Declaration
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> int(I1 in spec)
0
>>> int(I2 in spec)
1
>>> int(I3 in spec)
1
>>> int(I4 in spec)
1
Exmples for Declaration.__iter__()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> i = iter(spec)
>>> [x.getName() for x in i]
['I4', 'I2', 'I3']
>>> list(i)
[]
Exmples for Declaration.flattened()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration(I2, I3)
>>> spec = Declaration(I4, spec)
>>> i = spec.flattened()
>>> [x.getName() for x in i]
['I4', 'I2', 'I3', 'I1', 'Interface']
>>> list(i)
[]
Exmples for Declaration.__sub__()
:
>>> from zope.interface import Interface
>>> class I1(Interface): pass
...
>>> class I2(I1): pass
...
>>> class I3(Interface): pass
...
>>> class I4(I3): pass
...
>>> spec = Declaration()
>>> [iface.getName() for iface in spec]
[]
>>> spec -= I1
>>> [iface.getName() for iface in spec]
[]
>>> spec -= Declaration(I2)
>>> [iface.getName() for iface in spec]
[]
>>> spec = Declaration(I2, I4)
>>> [iface.getName() for iface in spec]
['I2', 'I4']
>>> [iface.getName() for iface in spec - I4]
['I2']
>>> [iface.getName() for iface in spec - I1]
['I4']
>>> [iface.getName() for iface
... in spec - Declaration(I4)]
['I2']
Exmples for Declaration.__add__()
:
>>> from zope.interface import Interface
>>> class IRoot1(Interface): pass
...
>>> class IDerived1(IRoot1): pass
...
>>> class IRoot2(Interface): pass
...
>>> class IDerived2(IRoot2): pass
...
>>> spec = Declaration()
>>> [iface.getName() for iface in spec]
[]
>>> [iface.getName() for iface in spec+IRoot1]
['IRoot1']
>>> [iface.getName() for iface in IRoot1+spec]
['IRoot1']
>>> spec2 = spec
>>> spec += IRoot1
>>> [iface.getName() for iface in spec]
['IRoot1']
>>> [iface.getName() for iface in spec2]
[]
>>> spec2 += Declaration(IDerived2, IRoot2)
>>> [iface.getName() for iface in spec2]
['IDerived2', 'IRoot2']
>>> [iface.getName() for iface in spec+spec2]
['IRoot1', 'IDerived2', 'IRoot2']
>>> [iface.getName() for iface in spec2+spec]
['IDerived2', 'IRoot2', 'IRoot1']
>>> [iface.getName() for iface in (spec+spec2).__bases__]
['IRoot1', 'IDerived2', 'IRoot2']
>>> [iface.getName() for iface in (spec2+spec).__bases__]
['IDerived2', 'IRoot2', 'IRoot1']
ProvidesClass
Descriptor semantics (via Provides.__get__
):
>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
... pass
>>> class C(object):
... pass
>>> from zope.interface.declarations import ProvidesClass
>>> C.__provides__ = ProvidesClass(C, IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> getattr(C(), '__provides__', 0)
0
Implementation Details
The following section discusses some implementation details and demonstrates their use. You’ll notice that they are all demonstrated using the previously-defined functions.
Provides
- zope.interface.Provides(*interfaces)[source]
Declaration for an instance of cls.
The correct signature is
cls, *interfaces
. The cls is necessary to avoid the construction of inconsistent resolution orders.Instance declarations are shared among instances that have the same declaration. The declarations are cached in a weak value dictionary.
In the examples below, we are going to make assertions about the size of the weakvalue dictionary. For the assertions to be meaningful, we need to force garbage collection to make sure garbage objects are, indeed, removed from the system. Depending on how Python is run, we may need to make multiple calls to be sure. We provide a collect function to help with this:
>>> import gc
>>> def collect():
... for i in range(4):
... gc.collect()
>>> collect()
>>> from zope.interface import directlyProvides
>>> from zope.interface.declarations import InstanceDeclarations
>>> before = len(InstanceDeclarations)
>>> class C(object):
... pass
>>> from zope.interface import Interface
>>> class I(Interface):
... pass
>>> c1 = C()
>>> c2 = C()
>>> len(InstanceDeclarations) == before
True
>>> directlyProvides(c1, I)
>>> len(InstanceDeclarations) == before + 1
True
>>> directlyProvides(c2, I)
>>> len(InstanceDeclarations) == before + 1
True
>>> del c1
>>> collect()
>>> len(InstanceDeclarations) == before + 1
True
>>> del c2
>>> collect()
>>> len(InstanceDeclarations) == before
True
ObjectSpecification
- zope.interface.declarations.ObjectSpecification(direct, cls)[source]
Provide object specifications
These combine information for the object and for it’s classes.
For example:
>>> from zope.interface import Interface
>>> from zope.interface import implementer_only
>>> class I1(Interface): pass
...
>>> class I2(Interface): pass
...
>>> class I3(Interface): pass
...
>>> class I31(I3): pass
...
>>> class I4(Interface): pass
...
>>> class I5(Interface): pass
...
>>> @implementer(I1)
... class A(object):
... pass
>>> class B(object):
... __implemented__ = I2
>>> @implementer(I31)
... class C(A, B):
... pass
>>> c = C()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I31', 'I1', 'I2']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I31', 'I3', 'I1', 'I2', 'Interface']
>>> int(I1 in providedBy(c))
1
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
0
>>> @implementer_only(I31)
... class COnly(A, B):
... pass
>>> @implementer(I5)
... class D(COnly):
... pass
>>> c = D()
>>> directlyProvides(c, I4)
>>> [i.getName() for i in providedBy(c)]
['I4', 'I5', 'I31']
>>> [i.getName() for i in providedBy(c).flattened()]
['I4', 'I5', 'I31', 'I3', 'Interface']
>>> int(I1 in providedBy(c))
0
>>> int(I3 in providedBy(c))
0
>>> int(providedBy(c).extends(I3))
1
>>> int(providedBy(c).extends(I1))
0
>>> int(providedBy(c).extends(I31))
1
>>> int(providedBy(c).extends(I5))
1
ObjectSpecificationDescriptor
- class zope.interface.declarations.ObjectSpecificationDescriptor[source]
Bases:
object
Implement the
__providedBy__
attributeThe
__providedBy__
attribute computes the interfaces provided by an object. If an object has an__provides__
attribute, that is returned. Otherwise,implementedBy
the cls is returned.Changed in version 5.4.0: Both the default (C) implementation and the Python implementation now let exceptions raised by accessing
__provides__
propagate. Previously, the C version ignored all exceptions.Changed in version 5.4.0: The Python implementation now matches the C implementation and lets a
__provides__
ofNone
override what the class is declared to implement.
For example:
>>> from zope.interface import Interface
>>> class IFoo(Interface): pass
...
>>> class IFooFactory(Interface): pass
...
>>> @implementer(IFoo)
... @provider(IFooFactory)
... class C(object):
... pass
>>> [i.getName() for i in C.__providedBy__]
['IFooFactory']
>>> [i.getName() for i in C().__providedBy__]
['IFoo']
Get an ObjectSpecification bound to either an instance or a class, depending on how we were accessed.