Skip to content

Commit

Permalink
Exempt parents with only annotations from invalid-enum-extension (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Jul 8, 2023
1 parent f815e9f commit 538a8fe
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8830.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exempt parents with only type annotations from the ``invalid-enum-extension``
message.

Closes #8830
21 changes: 15 additions & 6 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,12 +893,21 @@ def _check_consistent_mro(self, node: nodes.ClassDef) -> None:
def _check_enum_base(self, node: nodes.ClassDef, ancestor: nodes.ClassDef) -> None:
members = ancestor.getattr("__members__")
if members and isinstance(members[0], nodes.Dict) and members[0].items:
self.add_message(
"invalid-enum-extension",
args=ancestor.name,
node=node,
confidence=INFERENCE,
)
for _, name_node in members[0].items:
# Exempt type annotations without value assignments
if all(
isinstance(item.parent, nodes.AnnAssign)
and item.parent.value is None
for item in ancestor.getattr(name_node.name)
):
continue
self.add_message(
"invalid-enum-extension",
args=ancestor.name,
node=node,
confidence=INFERENCE,
)
break

if ancestor.is_subtype_of("enum.IntFlag"):
# Collect integer flag assignments present on the class
Expand Down
27 changes: 27 additions & 0 deletions tests/functional/i/invalid/invalid_enum_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ class D(C):
x = 3


# Similarly, items that are only type annotations are okay.
class ColorEnum(Enum):
red: int
green: int
blue: int

def __init__(self, red: int, green: int, blue: int) -> None:
self.red = red
self.green = green
self.blue = blue


class Pastel(ColorEnum):
SAGE = (170, 200, 167)


class IncorrectColorEnum(Enum):
red: None = None

def __init__(self, red: None) -> None:
self.red = red


class IncorrectPastel(IncorrectColorEnum): # [invalid-enum-extension]
SOME_COLOR = 170


class CustomFlags(IntFlag):
SUPPORT_OPEN = 1
SUPPORT_CLOSE = 2
1 change: 1 addition & 0 deletions tests/functional/i/invalid/invalid_enum_extension.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
invalid-enum-extension:12:0:12:7:B:"Extending inherited Enum class ""A""":INFERENCE
invalid-enum-extension:47:0:47:21:IncorrectPastel:"Extending inherited Enum class ""IncorrectColorEnum""":INFERENCE

0 comments on commit 538a8fe

Please sign in to comment.