check if class really implements api?

K

Karsten W.

Hello,
with the zope.interface module I can query if my class claims to
implement my API. This
is what the method implementedBy is for.

However, I find myself constantly changing the API and I am looking for
a more reliable way
to check my implementation classes. Is there a way to check if at least
all function
names of the API are defined by the class and if their parameter lists
fit the API? A way
to test this would save me a lot of time..

Kind regards,
Karsten.
 
H

Heiko Wundram

Karsten said:
However, I find myself constantly changing the API and I am looking for
a more reliable way
to check my implementation classes. Is there a way to check if at least
all function
names of the API are defined by the class and if their parameter lists
fit the API? A way
to test this would save me a lot of time..

To make it short: no, there is no reliable way to test this.

To make it longer: yes, you can, with several kludges (and limitations),
test this sort of thing. For example, start by running a dir() on the
interface definition class, and for each name you get returned check
whether it is a callable/function, then in case it is, have a look at the
corresponding class claiming to implement the interface, check if the name
exists, if it is a callable, and whether the method signature is
appopriately similar.

Most of the latter can be done using the introspection support present in
Python, which is nicely wrapped up for you in a module and whose
documentation you can find at:

http://www.python.org/doc/2.4.2/lib/module-inspect.html

Be aware that this does not check for methods which might be added at
runtime to a class, that you might have to create more elaborate tests to
check whether a class that might be callable has the correct method
signature, and several other quirks (such as a function in the interface
prototype taking five arguments, whereas in the class definition taking
only a single catch-all argument).

See how much of a can of worm this is? That's why you should rely on
duck-typing: if it quacks like a duck and walks like a duck, chances are
you really have a duck. And the only reliable way to test that is to let it
run.

--- Heiko.
 
T

Terry Hancock

with the zope.interface module I can query if my class
claims to implement my API. This
is what the method implementedBy is for.

However, I find myself constantly changing the API and I
am looking for a more reliable way
to check my implementation classes. Is there a way to
check if at least all function
names of the API are defined by the class and if their
parameter lists fit the API? A way
to test this would save me a lot of time..

You want:

Zope 2.5.1:
Interface.verify.verify_class_implementation

or

Zope 3.x:
zope.interface.verify

You'll want to read the source modules for documentation and
usage, as I don't think these are particularly well
documented anywhere else.

They are somewhat different in API between the two interface
implementations, but both do essentially the same thing --
they check the class against the interface object. This
will only tell you whether the methods are there and have
the same calling profile -- it obviously can't tell you
whether they do what you expect (or indeed do anything at
all).

But I had the same problem you're having, and I found them
useful.

Cheers,
Terry
 
K

Karsten W.

Terry said:
You want:
[...]
Zope 3.x:
zope.interface.verify

This is exactly what I was looking for! There is a function verifyClass
and there is
a TestCase for verify.py which teaches how to use it.

Thanks a lot!
Karsten.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,275
Messages
2,571,381
Members
48,070
Latest member
nick_tyson

Latest Threads

Top