Skip to content

Commit

Permalink
Ignore cached_property in method-hidden check (pylint-dev#8753)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 committed Jun 10, 2023
1 parent 2acca90 commit d51d804
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
10 changes: 9 additions & 1 deletion pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
_AccessNodes = Union[nodes.Attribute, nodes.AssignAttr]

INVALID_BASE_CLASSES = {"bool", "range", "slice", "memoryview"}
ALLOWED_PROPETIES = {"property", "cached_property"}
BUILTIN_DECORATORS = {"builtins.property", "builtins.classmethod"}
ASTROID_TYPE_COMPARATORS = {
nodes.Const: lambda a, b: a.value == b.value,
Expand Down Expand Up @@ -1252,11 +1253,18 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
# attribute affectation will call this method, not hiding it
return
if isinstance(decorator, nodes.Name):
if decorator.name == "property":
if decorator.name in ALLOWED_PROPETIES:
# attribute affectation will either call a setter or raise
# an attribute error, anyway not hiding the function
return

if (
isinstance(decorator, nodes.Attribute)
and decorator.expr.name == "functools"
and decorator.attrname == "cached_property"
):
return

# Infer the decorator and see if it returns something useful
inferred = safe_infer(decorator)
if not inferred:
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/m/method_hidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# pylint: disable=unused-private-member
"""check method hiding ancestor attribute
"""
import functools


class Abcd:
Expand Down Expand Up @@ -113,6 +114,12 @@ def _protected(self): # [method-hidden]
pass


class CachedChild(Parent):
@functools.cached_property
def _protected(self):
pass


class ParentTwo:
def __init__(self):
self.__private = None
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/m/method_hidden.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
method-hidden:17:4:17:12:Cdef.abcd:An attribute defined in functional.m.method_hidden line 11 hides this method:UNDEFINED
method-hidden:85:4:85:11:One.one:An attribute defined in functional.m.method_hidden line 83 hides this method:UNDEFINED
method-hidden:112:4:112:18:Child._protected:An attribute defined in functional.m.method_hidden line 108 hides this method:UNDEFINED
method-hidden:18:4:18:12:Cdef.abcd:An attribute defined in functional.m.method_hidden line 12 hides this method:UNDEFINED
method-hidden:86:4:86:11:One.one:An attribute defined in functional.m.method_hidden line 84 hides this method:UNDEFINED
method-hidden:113:4:113:18:Child._protected:An attribute defined in functional.m.method_hidden line 109 hides this method:UNDEFINED

0 comments on commit d51d804

Please sign in to comment.