PY-46623 Add setter and deleter to cached_property

GitOrigin-RevId: 97c7703a89d0e1c49f829aed3474006ac7e5324a
This commit is contained in:
evgeny.bovykin
2025-03-05 15:14:53 +01:00
committed by intellij-monorepo-bot
parent 08254bb652
commit 3df8d77318
10 changed files with 57 additions and 0 deletions

View File

@@ -109,6 +109,7 @@ public final @NonNls class PyNames {
public static final String SETTER = "setter";
public static final String DELETER = "deleter";
public static final String GETTER = "getter";
public static final String CACHED_PROPERTY = "cached_property";
public static final String ALL = "__all__";
public static final String SLOTS = "__slots__";

View File

@@ -43,6 +43,7 @@ public final class PyKnownDecoratorUtil {
FUNCTOOLS_WRAPS("functools.wraps"),
FUNCTOOLS_TOTAL_ORDERING("functools.total_ordering"),
FUNCTOOLS_SINGLEDISPATCH("functools.singledispatch"),
FUNCTOOLS_CACHED_PROPERTY("functools.cached_property"),
ABC_ABSTRACTMETHOD("abc.abstractmethod"),
ABC_ABSTRACTCLASSMETHOD("abc.abstractclassmethod"),
@@ -121,6 +122,7 @@ public final class PyKnownDecoratorUtil {
private static final Set<KnownDecorator> PROPERTY_DECORATORS = EnumSet.of(PROPERTY,
ABC_ABSTRACTPROPERTY,
PYRAMID_DECORATOR_REIFY,
FUNCTOOLS_CACHED_PROPERTY,
DJANGO_UTILS_FUNCTIONAL_CACHED_PROPERTY,
KOMBU_UTILS_CACHED_PROPERTY);

View File

@@ -683,6 +683,12 @@ public class PyClassImpl extends PyBaseElementImpl<PyClassStub> implements PyCla
decoName = knownName;
}
}
if (PyNames.CACHED_PROPERTY.equals(decoName)) {
getter = new Maybe<>(method);
setter = new Maybe<>(method);
deleter = new Maybe<>(method);
break;
}
if (PyNames.PROPERTY.equals(decoName) ||
PyKnownDecoratorUtil.isPropertyDecorator(deco, TypeEvalContext.codeInsightFallback(getProject())) ||
qname.matches(decoratorName, PyNames.GETTER)) {

View File

@@ -0,0 +1,4 @@
from foo import Foo
f = Foo()
del f.foo
f.foo = ""

View File

@@ -0,0 +1,8 @@
from functools import cached_property
class Foo:
def __init__(self):
pass
@cached_property
def foo(self):
pass

View File

@@ -0,0 +1,4 @@
from foo import Foo
f = Foo()
del f.foo
f.foo = ""

View File

@@ -0,0 +1,8 @@
from django.utils.functional import cached_property
class Foo:
def __init__(self):
pass
@cached_property
def foo(self):
pass

View File

@@ -0,0 +1,4 @@
from foo import Foo
f = Foo()
del f.foo
f.foo = ""

View File

@@ -0,0 +1,8 @@
from kombu.utils import cached_property
class Foo:
def __init__(self):
pass
@cached_property
def foo(self):
pass

View File

@@ -37,6 +37,18 @@ public class PyPropertyAccessInspectionTest extends PyInspectionTestCase {
);
}
public void testCachedProperty() {
doMultiFileTest();
}
public void testDjangoCachedProperty() {
doMultiFileTest();
}
public void testKombuCachedProperty() {
doMultiFileTest();
}
@NotNull
@Override
protected Class<? extends PyInspection> getInspectionClass() {