Skip to content

Commit

Permalink
Fix False positive on Enum.__members__.items() (#4135)
Browse files Browse the repository at this point in the history
* Fix false positive on Enum.__members__.{values, keys}
* Fix False positive on Enum.__members__.items()

Co-authored-by: Marc Mueller <[email protected]>
Co-authored-by: Pierre Sassoulas <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2021
1 parent 8cbcf87 commit 41d2349
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
===========================
Expand Down
12 changes: 12 additions & 0 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
14 changes: 14 additions & 0 deletions tests/functional/m/member_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 41d2349

Please sign in to comment.