From 41d2349c7b84714539a9979b5344294029c984ab Mon Sep 17 00:00:00 2001 From: Ikraduya Edian Date: Sun, 28 Feb 2021 03:27:15 +0700 Subject: [PATCH] Fix False positive on Enum.__members__.items() (#4135) * Fix false positive on Enum.__members__.{values, keys} * Fix False positive on Enum.__members__.items() Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Pierre Sassoulas --- ChangeLog | 3 +++ pylint/checkers/typecheck.py | 12 ++++++++++++ tests/functional/m/member_checks.py | 14 ++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/ChangeLog b/ChangeLog index df57bcf571..d3976d1e31 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,9 @@ What's New in Pylint 2.7.2? .. Put bug fixes that will be cherry-picked to latest major version here +* Fix False Positive on `Enum.__members__.items()`, `Enum.__members__.values`, and `Enum.__members__.keys` + Closes #4123 + What's New in Pylint 2.7.1? =========================== diff --git a/pylint/checkers/typecheck.py b/pylint/checkers/typecheck.py index ad4ca70472..65f79c18bb 100644 --- a/pylint/checkers/typecheck.py +++ b/pylint/checkers/typecheck.py @@ -505,6 +505,18 @@ def _emit_no_member(node, owner, owner_name, ignored_mixins=True, ignored_none=T return False except astroid.NotFoundError: return True + if ( + owner.parent + and isinstance(owner.parent, astroid.ClassDef) + and owner.parent.name == "EnumMeta" + and owner_name == "__members__" + and node.attrname in ["items", "values", "keys"] + ): + print(node.attrname) + # Avoid false positive on Enum.__members__.{items(), values, keys} + # See https://github.com/PyCQA/pylint/issues/4123 + return False + return True diff --git a/tests/functional/m/member_checks.py b/tests/functional/m/member_checks.py index 30f16c58c6..291daee125 100644 --- a/tests/functional/m/member_checks.py +++ b/tests/functional/m/member_checks.py @@ -217,3 +217,17 @@ def __init__(self, flag): else: self.attribute = [] self.attribute.append(1) + +from enum import Enum +class Animal(Enum): + ANT = 1 + BEE = 2 + CAT = 3 + DOG = 4 +# To test false positive no-member on Enum.__members__.items() +for itm in Animal.__members__.items(): + print(itm) +for keyy in Animal.__members__.keys: + print(keyy) +for vall in Animal.__members__.values: + print(vall)