attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Subclassed decorator methods not working

Open SoundsSerious opened this issue 4 years ago • 6 comments

My framework relies on (smart) subclassing, but I've also got it to work with attrs mostly. Running into a problem like the following.

@attr.s
class BaseClass():
    
    value = attr.ib(default=10)

@attr.s
class TestClass(BaseClass):
    
    @value.default
    def _not_value(self):
        return 20

SoundsSerious avatar Jan 28 '21 18:01 SoundsSerious

This throws an error like:

<ipython-input-12-7baf0268d8f2> in TestClass()
      7 class TestClass(BaseClass):
      8 
----> 9     @value.default
     10     def _not_value(self):
     11         return 20

NameError: name 'value' is not defined

SoundsSerious avatar Jan 28 '21 18:01 SoundsSerious

I don't understand why this wouldn't work, but it seems attrs is heavily reworking the new / init system so its easy for something like this to go over my head.

Shouldn't TestClass inherit value from BaseClass?

Is there a work around?

SoundsSerious avatar Jan 28 '21 18:01 SoundsSerious

seems like this is more of a python issue https://stackoverflow.com/questions/3782040/python-decorators-that-are-part-of-a-base-class-cannot-be-used-to-decorate-membe

SoundsSerious avatar Jan 28 '21 18:01 SoundsSerious

Yeah… this isn't attrs.

wsanchez avatar Jan 28 '21 18:01 wsanchez

@wsanchez i understand closing this as it isn't something python directly supports, however it seems to be a strongly suggested method from the attrs docs which doesn't work in the case of subclasses.

In reviewing other issues before I posted this one, I found a solution that could potentially alleviate this by relying on the attrs namespace:

@attr.default('value'):
def _not_value(self):
    ....

SoundsSerious avatar Jan 28 '21 18:01 SoundsSerious

Ah, let's re-open for consideration in the docs.

wsanchez avatar Jan 28 '21 19:01 wsanchez