pythoninterfacezope.interface

How to declare @staticmethod in zope.interface


I try to create interface with @staticmethod and @classmethod. Declaring class method is simple. But I can't find the correct way to declare static method.

Consider class interface and its implementation:

#!/usr/bin/python3
from zope.interface import Interface, implementer, verify


class ISerializable(Interface):

    def from_dump(slice_id, intex_list, input_stream):
        '''Loads from dump.'''

    def dump(out_stream):
        '''Writes dump.'''

    def load_index_list(input_stream):
        '''staticmethod'''


@implementer(ISerializable)
class MyObject(object):

    def dump(self, out_stream):
        pass

    @classmethod
    def from_dump(cls, slice_id, intex_list, input_stream):
        return cls()

    @staticmethod
    def load_index_list(stream):
        pass

verify.verifyClass(ISerializable, MyObject)
verify.verifyObject(ISerializable, MyObject())
verify.verifyObject(ISerializable, MyObject.from_dump(0, [], 'stream'))

Output:

Traceback (most recent call last):
  File "./test-interface.py", line 31, in <module>
    verify.verifyClass(ISerializable, MyObject)
  File "/usr/local/lib/python3.4/dist-packages/zope/interface/verify.py", line 102, in verifyClass
    return _verify(iface, candidate, tentative, vtype='c')
  File "/usr/local/lib/python3.4/dist-packages/zope/interface/verify.py", line 97, in _verify
    raise BrokenMethodImplementation(name, mess)
zope.interface.exceptions.BrokenMethodImplementation: The implementation of load_index_list violates its contract
        because implementation doesn't allow enough arguments.

How should I correctly declare static method in this interface?


Solution

  • Obviously the verifyClass does not understand either classmethod or staticmethod properly. The problem is that in Python 3, if you do getattr(MyObject, 'load_index_list') in Python 3, you get a bare function, and verifyClass thinks it is yet another unbound method, and then expects that the implicit self be the first argument.

    The easiest fix is to use a classmethod there instead of a staticmethod.

    I guess someone could also do a bug report.