Skip to content

Commit 40d853c

Browse files
authored
Add __set__ to functools.cached_property (#9762)
1 parent 7b975dc commit 40d853c

6 files changed

Lines changed: 34 additions & 0 deletions

File tree

stdlib/functools.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ if sys.version_info >= (3, 8):
148148
@overload
149149
def __get__(self, instance: object, owner: type[Any] | None = None) -> _T: ...
150150
def __set_name__(self, owner: type[Any], name: str) -> None: ...
151+
# __set__ is not defined at runtime, but @cached_property is designed to be settable
152+
def __set__(self, instance: object, value: _T) -> None: ...
151153
if sys.version_info >= (3, 9):
152154
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
153155

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
5+
if sys.version_info >= (3, 8):
6+
from functools import cached_property
7+
from typing_extensions import assert_type
8+
9+
class A:
10+
def __init__(self, x: int):
11+
self.x = x
12+
13+
@cached_property
14+
def x(self) -> int:
15+
return 0
16+
17+
assert_type(A(x=1).x, int)
18+
19+
class B:
20+
@cached_property
21+
def x(self) -> int:
22+
return 0
23+
24+
def check_cached_property_settable(x: int) -> None:
25+
b = B()
26+
assert_type(b.x, int)
27+
b.x = x
28+
assert_type(b.x, int)

tests/stubtest_allowlists/py310.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ builtins.property.__set_name__ # Doesn't actually exist
1616
contextlib.AbstractAsyncContextManager.__class_getitem__
1717
contextlib.AbstractContextManager.__class_getitem__
1818
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
19+
functools.cached_property.__set__ # Stub is a while lie; see comments in the stub
1920
gettext.install
2021
gettext.translation
2122
hmac.new # Stub is a white lie; see comments in the stub

tests/stubtest_allowlists/py311.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum.auto.__init__
2020
enum.auto.value
2121
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
2222
ftplib.FTP.trust_server_pasv_ipv4_address
23+
functools.cached_property.__set__ # Stub is a while lie; see comments in the stub
2324
ipaddress.IPv4Interface.hostmask
2425
ipaddress.IPv6Interface.hostmask
2526
ipaddress._BaseNetwork.broadcast_address

tests/stubtest_allowlists/py38.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dummy_threading.Thread.native_id
4646
dummy_threading.local.__new__
4747
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
4848
ftplib.FTP.trust_server_pasv_ipv4_address # Dangerous to use, intentionally undocumented, intentionally missing from typeshed. #6154
49+
functools.cached_property.__set__ # Stub is a while lie; see comments in the stub
4950
gettext.install # codeset default value is ['unspecified'] so can't be specified
5051
gettext.translation # codeset default value is ['unspecified'] so can't be specified
5152
hmac.new # Stub is a white lie; see comments in the stub

tests/stubtest_allowlists/py39.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ distutils.core.extension_keywords
4040
distutils.core.gen_usage
4141
distutils.core.setup_keywords
4242
fractions.Fraction.__new__ # overload is too complicated for stubtest to resolve
43+
functools.cached_property.__set__ # Stub is a while lie; see comments in the stub
4344
gettext.install
4445
gettext.translation
4546
hmac.new # Stub is a white lie; see comments in the stub

0 commit comments

Comments
 (0)