python-attrs / python-attrs/attrs

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

Open
#621 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

@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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the attr.s handling of the these argument and inspect how class-body _CountingAttr instances and the annotations dict are processed when auto_attribs is enabled. Reproduce the shown test class and verify that attributes declared in the class body are retained alongside those supplied through these, or that the conflicting usage is rejected clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.