Skip to content

Using cached_property results in undesired references #1220

@AdrianSosic

Description

@AdrianSosic

I'm excited to see that the latest attrs version 23.2.0 now supports using cached_property in combination with slotted classes 🥇👍🏼 I was about to enable this feature in my code base but then noticed the following issue: the garbage collection step described here no longer seems to work. I haven't checked how exactly the use of cached_property was enabled, but the applied mechanism seems to keep some references to the original classes and thus messes with the garbage collector, which I think is an undesired side-effect.

Here a minimal example to reproduce the issue:

import gc
from functools import cached_property
from attrs import define

@define
class Base:
    pass

@define
class SubClass(Base):
    @cached_property
    def value(self) -> int:
        return 0

gc.collect()
print(Base.__subclasses__())

With the cached_property decorator, this gives the following output:

[<class '__main__.SubClass'>, <class '__main__.SubClass'>]

while removing it yields the desired result:

[<class '__main__.SubClass'>]

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