Skip to content

Cached properties error with named tuples.Β #168

@Qu4tro

Description

@Qu4tro

Hello there. Currently there's no way to use cached properties with namedTuples (or other immutable classes implemented the same way)

Trace:

AttributeError                            Traceback (most recent call last)
<ipython-input-3-7deeb8fbc12c> in <module>
----> 1 r.undirected

~/.local/share/virtualenvs/myvenv/lib/python3.7/site-packages/cached_property.py in __get__(self, obj, cls)
     33             return self._wrap_in_coroutine(obj)
     34 
---> 35         value = obj.__dict__[self.func.__name__] = self.func(obj)
     36         return value
     37 

AttributeError: 'MyClass' object has no attribute '__dict__'

Reproduced in https://github.com/Qu4tro/cachedproperty_immutable_classes

There was a change in python 3.5.1 and NamedTuple no longer has the __dict__ attribute. More information here: https://stackoverflow.com/a/34166617

I totally understand why it's failing, so my question is:

  • Are you interested in supporting this use-case?
  • If yes - Any implementation ideas flying around?
  • If not - Maybe a small note on the README would be useful.

Lmk what you think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions