Skip to content

cached properties should be pickled #1420

Open
@Redoubts

Description

@Redoubts

Consider the following

 @attrs.define
 class A:
     @functools.cached_property
     def x(self) -> list[int]:
         print('caching')
         return list(range(5))

The following behavior is expected:

In []: attrs.__version__
Out[]: '25.3.0'

In []: a = A()

In []: a.x
caching
Out[]: [0, 1, 2, 3, 4]

In []: a.x
Out[]: [0, 1, 2, 3, 4]

a.x is cached. However, if I round trip through pickle:

In []: pickle.loads(pickle.dumps(a)).x
caching
Out[]: [0, 1, 2, 3, 4]

the cached property is rebuilt. Usually when I used cached properties, it's to hold expensive data, and it would be nice to capture this in a pickle. Can this be supported?

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