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

Default implementation for Interface property setters #49

Open
Diogo-Rossi opened this issue Apr 4, 2022 · 1 comment
Open

Default implementation for Interface property setters #49

Diogo-Rossi opened this issue Apr 4, 2022 · 1 comment

Comments

@Diogo-Rossi
Copy link

Hi @ssanderson, congratulations for this package! (btw, it should be included in python's stdlib)

I'd like to tell there may be an issue when there is a default property setter.

from interface import Interface, implements, default

class Person(Interface):
    
    def set_name(self, value):
        pass
    
    def get_name(self):
        pass
    
    @default
    @property
    def name(self):
        return self.get_name()
    
    @default
    @name.setter
    def name(self,value):
        self.set_name(value)

class MyPerson(implements(Person)):
    
    def set_name(self, value):
        self._name = value
    
    def get_name(self):
        return self._name

The above code leads to the following

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File [...]:9, in <module>
      6 from __future__ import annotations
      7 from interface import Interface, default, implements
----> 9 class MultiInputDialogInterface(Interface):
     11     @default
     12     @property
     13     def windowtitle(self) -> str:
     14         return "Multiple Input Dialog"

File [...]:17, in MultiInputDialogInterface()
     11 @default
     12 @property
     13 def windowtitle(self) -> str:
     14     return "Multiple Input Dialog"
     16 @default
---> 17 @windowtitle.setter
     18 def windowtitle(self, title: str) -> None:
     19     raise NotImplementedError
     21 @default
     22 def set_windowtitle(self, value:str) -> MultiInputDialogInterface:

AttributeError: 'default' object has no attribute 'setter'

Is that ok or did I miss something?

@Diogo-Rossi
Copy link
Author

Diogo-Rossi commented Apr 4, 2022

Well, I solved it adding the @default decorator only to te setter method.

Is that the desired behaviour?

This code is running fine:

from interface import Interface, implements, default

class Person(Interface):
    
    def set_name(self, value):
        pass
    
    def get_name(self):
        pass
    
    @property
    def name(self):
        print('getting name, which is:')
        return self.get_name()
    
    @default
    @name.setter
    def name(self,value):
        print('setting name to:',value)
        self.set_name(value)

class MyPerson(implements(Person)):
    
    def set_name(self, value):
        self._name = value
    
    def get_name(self):
        return self._name

p = MyPerson()
p.name = 'John Doe'
print(p.name)

The output is:

setting name to: John Doe
getting name, which is:
John Doe

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