Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check setter and deleter of property? #51

Open
ashkan-khd opened this issue Jul 30, 2023 · 0 comments
Open

Check setter and deleter of property? #51

ashkan-khd opened this issue Jul 30, 2023 · 0 comments

Comments

@ashkan-khd
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant