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

Passing these=d to attr.s skips attributes defined in class body #621

Open
perkinslr opened this issue Jan 28, 2020 · 2 comments
Open

Passing these=d to attr.s skips attributes defined in class body #621

perkinslr opened this issue Jan 28, 2020 · 2 comments
Labels

Comments

@perkinslr
Copy link

perkinslr commented Jan 28, 2020

@attr.s(these=dict(a=attr.ib()))
class test:
    b: int = attr.ib(default=0)

c = test(5)
print(c.b)

This fails even if passing auto_attribs=True, albeit in a slightly different way.

The obvious workaround is to only put attributes in these, but that is harder for code analysis tools to understand (it lacks type annotations, even if the tools are attrs aware).

The second workaround would be to put the attributes from these into the class body, but that doesn't work if the attribute is inherited or describe a data descriptor (like getter/setters).

The final workaround is to duplicate the definition into both these and the class body, but that creates an opportunity for the two to get out of sync.

Anyway, ideally, attrs would add anything from the class body which is an instance of _CountingAttr, (and anything from the annotations dict if auto_attribs is set). If that is not practical, then a warning or exception should be raised rather than having weird runtime behavior.

@wsanchez wsanchez added the Bug label Jan 29, 2020
@wbolster
Copy link
Member

taking one step back, what is the use case for using both styles at the same time?

@perkinslr
Copy link
Author

I believe I described the use case already, but to reiterate, passing a long list of attributes via the these parameters is ugly, but if you want to include an inherited data descriptor in your init/repr,eq and so on, you cannot put that annotation in the class body (maybe you can with the auto_attribs, but I don't think so).

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

No branches or pull requests

3 participants