Skip to content

cached_property issue / descriptor issue #1833

Closed
@ep12

Description

@ep12

Feature

I have a class like this:

from functools import cached_property
def f(v):
    # do something
    ...

class C:
    def __init__(self, data: dict):
        self.data = data
    
    @cached_property
    def calculated_data(self) -> dict:
         return {k: f(v) for k, v in self.data.items()}

(For the exact implementation see ep12/PyOPM, but the implementation is not really important in this case.)
Something like

c = C(some_dict)
for k, v in c.calculated_data.items():
    # do something
    ...

results in an error:

Traceback (most recent call last):
  File "basic.py", line 17, in <module>
    m = p.match({0, 1, 2})  # not a dict -> m is None
  File "/home/simon/git/PyOPM/pyopm/core.py", line 263, in match
    for kt, v in self.compiled_pattern.items():
  File "/home/simon/git/RustPython/Lib/functools.py", line 951, in __get__
    "Cannot use cached_property instance without calling __set_name__ on it.")
TypeError: Cannot use cached_property instance without calling __set_name__ on it.

I think it might be related to the descriptor implementation, but __set_name__ does not seem to be called using rustpython. Everything works fine with cpython and pypy.
Since the cached_property implementation of rustpython and cpython is exactly the same, this might be an internal issue. I think that cached_property.__set_name__ should be called with name=func.__name__ (which is 'calculated_data', of course)... I think python itself performs this call (I could not find a line of python where __set_name__ gets called), so maybe the rustpython implementation does not do that at the moment.

I haven't looked at the rustpython source code (my rust skills are extremely limited) but maybe someone else knows where to have a look...

Python Documentation

cpython doc: implementing descriptors

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions