python-attrs / attrs

Python Classes Without Boilerplate
https://www.attrs.org/
MIT License
5.27k stars 367 forks source link

Subclassed decorator methods not working #753

Open SoundsSerious opened 3 years ago

SoundsSerious commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

wsanchez commented 3 years ago

Yeah… this isn't attrs.

SoundsSerious commented 3 years ago

@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):
    ....
wsanchez commented 3 years ago

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