python-attrs / python-attrs/attrs
attr'd classes don't have default attributes?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
Today I stumbled over this one:
from attrs import define,field
from dataclasses import dataclass
@define
class N1:
foo:int|None = field(default=None)
class N2:
foo:int|None = None
def __init__(self, foo):
self.foo = 123
@dataclass
class N3:
foo:int|None = None
n3 = N3(foo=123)
del n3.foo
assert n3.foo is None
n2 = N2(foo=123)
del n2.foo
assert n2.foo is None
n1 = N1(foo=123)
del n1.foo
n1.foo # AttributeError
This is not what I'd expect when I decide to attrs-ize an existing class. Thus please either fix this, or add a reasonably prominent section that documents this (and other possible pitfalls) to the manual.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the issue's @define, field, and deletion example to reproduce the AttributeError, then inspect how attrs handles defaults after an instance attribute is deleted. Compare the behavior with the dataclass and hand-written class examples. Done means either the default is restored as expected or the manual prominently documents this pitfall and related migration concerns.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100