Skip to content

Check setter and deleter of property? #51

Open
@ashkan-khd

Description

@ashkan-khd

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions