Skip to content

attr'd classes don't have default attributes? #1477

@smurfix

Description

@smurfix

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions