Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#29478 closed Bug (fixed)

cached_property decorator doesn't work with mangled method names

Reported by: Thomas Grainger Owned by: nobody
Component: Utilities Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

import itertools

from django.utils.functional import cached_property

count = itertools.count()
count2 = itertools.count()
count3 = itertools.count()


class Foo:
    @cached_property
    def __foo(self):
        return next(count)

    @cached_property
    def foo2(self):
        return next(count2)

    @property
    def foo3(self):
        return next(count3)

    def run(self):
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo', self.__foo)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo2', self.foo2)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)
        print('foo3', self.foo3)


Foo().run()


"""
python cached_property_test.py 
foo 0
foo 1
foo 2
foo 3
foo2 0
foo2 0
foo2 0
foo2 0
foo2 0
foo3 0
foo3 1
foo3 2
foo3 3
foo3 4
"""

Odd it's not been reported before: https://code.djangoproject.com/search?q=cached_property+mangled

Change History (11)

comment:1 by Tim Graham, 7 years ago

Component: UncategorizedUtilities
Has patch: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by Harro, 6 years ago

Had a look at this, and the Foo instance dict does contain the __foo (and the foo2, but not the foo3 as expected).

I think this is more a python thing where the double underscore is special and that is the reason it doesn't work and that it has nothing to do with the @cached_property decorator.

Last edited 6 years ago by Tim Graham (previous) (diff)

comment:3 by Thomas Grainger, 6 years ago

Patch needs improvement: unset

comment:4 by Tim Graham <timograham@…>, 6 years ago

In 09199734:

Refs #29478 -- Doc'd how to use cached_property with a mangled name.

comment:5 by Tim Graham <timograham@…>, 6 years ago

In 2a637a45:

[2.1.x] Refs #29478 -- Doc'd how to use cached_property with a mangled name.

Backport of 09199734d383691ecf6d163894b447ca45e0ef82 from master

comment:6 by Tim Graham, 6 years ago

Patch needs improvement: set

comment:7 by Sergey Fedoseev, 6 years ago

Patch needs improvement: unset

comment:8 by Tim Graham <timograham@…>, 6 years ago

In 80ba7a8:

Fixed cached_properties that share a common property.

The aliases aren't cached and thus the old usage will be an error after
refs #29478.

comment:9 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In 06076999:

Fixed #29478 -- Added support for mangled names to cached_property.

Co-Authored-By: Sergey Fedoseev <fedoseev.sergey@…>

comment:10 by Tim Graham <timograham@…>, 6 years ago

In 3b64e2b7:

Refs #29478 -- Clarified cached_property 2.2 release note.

comment:11 by Tim Graham <timograham@…>, 6 years ago

In c3655b1:

[2.2.x] Refs #29478 -- Clarified cached_property 2.2 release note.

Backport of 3b64e2b77570c73a0d7092124fa73439e0fb4305 from master.

Note: See TracTickets for help on using tickets.
Back to Top