python-interface icon indicating copy to clipboard operation
python-interface copied to clipboard

Check setter and deleter of property?

Open ashkan-khd opened this issue 2 years ago • 0 comments

According to docs,

Interfaces can declare non-method attributes that should be provided by implementations using property...

In the meanwhile it seems that the module does not make any difference between setter, getter, and deleter of a property.

class IWalk(interface.Interface):

    def walk(self):
        pass

    @property
    def shoes(self):
        pass

    @shoes.setter
    def shoes(self, value):
        pass

    @shoes.deleter
    def shoes(self):
        pass


class Human(interface.implements(IWalk)):

    def __init__(self, shoes):
        self._shoes = shoes

    def walk(self):
        shoes = self.shoes
        print('I am walking {}'.format(f'with my {shoes}' if shoes else 'barefoot'))

    @property
    def shoes(self):
        return self._shoes

The above code runs perfectly fine, though Human does not provide any implementations for setter and deleter of the shoes property. The problem gets more significant when a client of the IWalk interface tries to set (or delete) the shoes property:

def set_shoes_and_walk(walker: IWalk):
    walker.shoes = 'Nike'
    walker.walk()
---------------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ashk/Codes/tinterfaces/tpi/main.py", line 69, in <module>
    main()
  File "/home/ashk/Codes/tinterfaces/tpi/main.py", line 65, in main
    set_shoes_and_walk(h)
  File "/home/ashk/Codes/tinterfaces/tpi/main.py", line 59, in set_shoes_and_walk
    walker.shoes = 'Nike'
AttributeError: can't set attribute

ashkan-khd avatar Jul 30 '23 08:07 ashkan-khd