check if class really implements api?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karsten W.

    #1

    check if class really implements api?

    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.

  • Heiko Wundram

    #2
    Re: check if class really implements api?

    Karsten W. wrote:[color=blue]
    > 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..[/color]

    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:



    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.

    Comment

    • Terry Hancock

      #3
      Re: check if class really implements api?

      On 3 Jan 2006 12:56:58 -0800
      "Karsten W." <DonQuixoteVonL aMancha@gmx.net > wrote:[color=blue]
      > 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..[/color]

      You want:

      Zope 2.5.1:
      Interface.verif y.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

      --
      Terry Hancock (hancock@Anansi Spaceworks.com)
      Anansi Spaceworks http://www.AnansiSpaceworks.com

      Comment

      • Karsten W.

        #4
        Re: check if class really implements api?


        Terry Hancock schrieb:
        [color=blue]
        > You want:
        >[/color]
        [...][color=blue]
        > Zope 3.x:
        > zope.interface. verify
        >[/color]

        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.

        Comment

        Working...