Skip to content

Commit b16448b

Browse files
committed
Ignore cached_property in method-hidden check (pylint-dev#8753)
1 parent 2acca90 commit b16448b

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

pylint/checkers/classes/class_checker.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
_AccessNodes = Union[nodes.Attribute, nodes.AssignAttr]
5050

5151
INVALID_BASE_CLASSES = {"bool", "range", "slice", "memoryview"}
52+
CACHED_PROPETIES = {"property", "cached_property"}
5253
BUILTIN_DECORATORS = {"builtins.property", "builtins.classmethod"}
5354
ASTROID_TYPE_COMPARATORS = {
5455
nodes.Const: lambda a, b: a.value == b.value,
@@ -1252,11 +1253,18 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
12521253
# attribute affectation will call this method, not hiding it
12531254
return
12541255
if isinstance(decorator, nodes.Name):
1255-
if decorator.name == "property":
1256+
if decorator.name in CACHED_PROPETIES:
12561257
# attribute affectation will either call a setter or raise
12571258
# an attribute error, anyway not hiding the function
12581259
return
12591260

1261+
if (
1262+
isinstance(decorator, nodes.Attribute)
1263+
and decorator.expr.name == "functools"
1264+
and decorator.attrname == "cached_property"
1265+
):
1266+
return
1267+
12601268
# Infer the decorator and see if it returns something useful
12611269
inferred = safe_infer(decorator)
12621270
if not inferred:

tests/functional/m/method_hidden.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=unused-private-member
33
"""check method hiding ancestor attribute
44
"""
5+
import functools
56

67

78
class Abcd:
@@ -113,6 +114,12 @@ def _protected(self): # [method-hidden]
113114
pass
114115

115116

117+
class CachedChild(Parent):
118+
@functools.cached_property
119+
def _protected(self):
120+
pass
121+
122+
116123
class ParentTwo:
117124
def __init__(self):
118125
self.__private = None

tests/functional/m/method_hidden.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
method-hidden:17:4:17:12:Cdef.abcd:An attribute defined in functional.m.method_hidden line 11 hides this method:UNDEFINED
2-
method-hidden:85:4:85:11:One.one:An attribute defined in functional.m.method_hidden line 83 hides this method:UNDEFINED
3-
method-hidden:112:4:112:18:Child._protected:An attribute defined in functional.m.method_hidden line 108 hides this method:UNDEFINED
1+
method-hidden:18:4:18:12:Cdef.abcd:An attribute defined in functional.m.method_hidden line 12 hides this method:UNDEFINED
2+
method-hidden:86:4:86:11:One.one:An attribute defined in functional.m.method_hidden line 84 hides this method:UNDEFINED
3+
method-hidden:113:4:113:18:Child._protected:An attribute defined in functional.m.method_hidden line 109 hides this method:UNDEFINED

0 commit comments

Comments
 (0)