Skip to content

slots=True classes are not weak-referenceable #174

@gmacon

Description

@gmacon

This code:

import weakref
import attr

@attr.s(slots=True)
class Foo(object):
    x = attr.ib()

weakref.ref(Foo('x'))

produces this error:

Traceback (most recent call last):
  File "slotsref.py", line 8, in <module>
    weakref.ref(Foo('x'))
TypeError: cannot create weak reference to 'Foo' object

In the docs for __slots__, it says "Without a __weakref__ variable for each instance, classes defining __slots__ do not support weak references to its instances. If weak reference support is needed, then add '__weakref__' to the sequence of strings in the __slots__ declaration."

Adding '__weakref__' to the list isn't hard, but I wanted to see if people had any opinions about whether this should happen transparently, or if you should have to opt-in to it by saying @attr.s(weakref=True).

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