observed icon indicating copy to clipboard operation
observed copied to clipboard

observable properties doesn't work with python 3.5

Open Carnou opened this issue 8 years ago • 5 comments

This is largely discussed at your answer on StackOverflow, but I'll summarize here.

Using the @observableMethod decorator on a method that is part of a property doesn't work in (c)python 3.5. Some problem occurs when the property is correctly bound to the class instance. For example,

class Bar(object):
    def __init__(self):
        self._bbb = "bar"
    @property
    def bbb(self):
        return self._bbb
    @bbb.setter
    @observable_method
    def setbbb(self, value):
            self._bbb = value

will cause the following error when you create an instance of Bar and try to set bar.bbb

Traceback (most recent call last): File "", line 1, in TypeError: 'ObservableMethodManager_PersistOnInstances' object is not callable

Using explicit properties as below causes the same issue:

class Bar(object):
    def __init__(self):
        self._bbb = "bar"
    def getbbb(self):
        return self._bbb
    @observable_method
        def setbbb(self, value):
            self._bbb = value
    bbb = property(getbbb, setbbb)

Interestingly, a hackneyed attempt where the property is completely distinct from the class doesn't have the issue. The following works:

class Bar(object):
    def __init__(self):
        self._bbb = "bar"
    def getbbb(self):
        return self._bbb
    @observable_method
    def setbbb(self, value):
        self._bbb = value

b = Bar()

prop = property(Bar.getbbb)
prop = prop.setter(Bar.setbbb)

def cb(val):
print("Called, {0}".format(val))

b.setbbb.add_observer(cb)
# >>> True
prop.__get__(b)
# >>> 'bar'
prop.__set__(b, 'BAR')
# >>> Called, BAR
b._bbb
# >>> 'BAR'

Carnou avatar Jan 10 '17 21:01 Carnou

@Carnou did you wind up fixing this? Pull requests are always welcomed.

DanielSank avatar Jul 27 '17 18:07 DanielSank

@DanielSank, no, I never did. In the Stack Overflow chat, I said I should, but I never actually touched your code. I'm still making use of the subclass workaround, though!

Carnou avatar Jul 27 '17 18:07 Carnou

Hi @DanielSank, any update on this? I was actually hoping to use your package because of this use case specifically!

Trezorro avatar Dec 16 '20 14:12 Trezorro

@Trezorro I have not done any work on this issue. Would you like to try?

DanielSank avatar Dec 18 '20 08:12 DanielSank

Lurker here: I tried to find out more information about python’s built in property decorator. It’s implemented in C so probably the only way forward here is to implements your own @property decorator which integrates with observed.

For instance, this is a reimplementation of python’s property decorator which introduces caching, https://github.com/pydanny/cached-property/blob/master/cached_property.py

I would use the above decorator and debug this. At least you have the code and can set breakpoints to see what is going on.

danieljfarrell avatar Dec 18 '20 09:12 danieljfarrell