Skip to content

Commit

Permalink
Fix FP for no-member: function attributes from decorator (#9308)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jan 10, 2024
1 parent 511354a commit 4739f84
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9246.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Avoid false positives for ``no-member`` involving function
attributes supplied by decorators.

Closes #9246
8 changes: 7 additions & 1 deletion pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ def visit_assignattr(self, node: nodes.AssignAttr) -> None:
def visit_delattr(self, node: nodes.DelAttr) -> None:
self.visit_attribute(node)

# pylint: disable = too-many-branches
# pylint: disable = too-many-branches, too-many-statements
@only_required_for_messages("no-member", "c-extension-no-member")
def visit_attribute(
self, node: nodes.Attribute | nodes.AssignAttr | nodes.DelAttr
Expand Down Expand Up @@ -1128,6 +1128,12 @@ def visit_attribute(
except astroid.DuplicateBasesError:
continue
except astroid.NotFoundError:
# Avoid false positive in case a decorator supplies member.
if (
isinstance(owner, (astroid.FunctionDef, astroid.BoundMethod))
and owner.decorators
):
continue
# This can't be moved before the actual .getattr call,
# because there can be more values inferred and we are
# stopping after the first one which has the attribute in question.
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/n/no/no_member_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Attributes supplied by a decorator."""

from functools import lru_cache


class SomeClass: # pylint: disable=too-few-public-methods
"""https://github.com/pylint-dev/pylint/issues/9246"""
@classmethod
@lru_cache
def __cached_fun(cls, arg: int) -> str:
return str(arg)

@classmethod
def cache_clear(cls):
"""__cached_fun()'s @cache decorator supplies cache_clear()."""
cls.__cached_fun.cache_clear()

0 comments on commit 4739f84

Please sign in to comment.