Skip to content

Subclassed decorator methods not working #753

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

Open
SoundsSerious opened this issue Jan 28, 2021 · 6 comments
Open

Subclassed decorator methods not working #753

SoundsSerious opened this issue Jan 28, 2021 · 6 comments

Comments

@SoundsSerious
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

@wsanchez
Copy link

Yeah… this isn't attrs.

@SoundsSerious
Copy link
Author

SoundsSerious commented Jan 28, 2021

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants