Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

cached_property no longer works as a data descriptor in Python 3.12 #106292

Closed
Assignees
carljm
Labels
3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@treyhunner

Description

@treyhunner

Bug report

In Python 3.11 and below, whencached_property was inherited from, the__get__ method would always check for the attribute in the cache.
In Python 3.12.0b3 (since#101890 it seems) the__get__ method no longer checks the cache.

This isn't an issue for typical use, but it might be an issue for subclasses.
For example here's a version ofcached_property that inherits from thefunctools version but also allows for asetter (just as@property does).

Since this child class adds a__set__ method, the__get__ method infunctools.cached_property will be calledbefore the__dict__ attribute is accessed.

"""Demonstration of cached_property difference in Python 3.12.0b3."""importfunctoolsclasssettable_cached_property(functools.cached_property):def__init__(self,func):super().__init__(func)self._setter=self._deleter=Nonedefsetter(self,setter):self._setter=setterreturnselfdef__set__(self,obj,value):ifself._setter:self._setter(obj,value)obj.__dict__.pop(self.attrname,None)else:obj.__dict__[self.attrname]=valueclassThing:@settable_cached_propertydefx(self):returnself.ything=Thing()thing.y=4print(f"{thing.x=} (should be 4)")thing.y=5print(f"{thing.x=} (should still be 4)")

This new behavior may be intended, but I wanted to make a note of it because it does break a previous (undocumented I believe?) assumption thatcached_property could be inherited from and turned into a data descriptor.

Your environment

Python 3.12.0b3 on Ubuntu Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp