Skip to content

Commit a1443ed

Browse files
github-actions[bot]mbyrnepr2Pierre-Sassoulas
authored
Fix a crash for Enum class decorated with dataclass (#9104) (#9112)
* Fix a crash when an enum class which is also decorated with a ``dataclasses.dataclass`` decorator is defined. Closes #9100 * Update test to mention the `astroid` issue that addresses the missing ``__members__`` object. * Use an `if` instead of the `try/except` block. * Update pylint/checkers/utils.py Co-authored-by: Daniël van Noord <[email protected]> --------- Co-authored-by: Daniël van Noord <[email protected]> (cherry picked from commit 2c3425d) Co-authored-by: Mark Byrne <[email protected]> Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 2d8a894 commit a1443ed

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

doc/whatsnew/fragments/9100.other

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when an enum class which is also decorated with a ``dataclasses.dataclass`` decorator is defined.
2+
3+
Closes #9100

pylint/checkers/utils.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2281,5 +2281,8 @@ def is_enum_member(node: nodes.AssignName) -> bool:
22812281
):
22822282
return False
22832283

2284-
enum_member_objects = frame.locals.get("__members__")[0].items
2285-
return node.name in [name_obj.name for value, name_obj in enum_member_objects]
2284+
members = frame.locals.get("__members__")
2285+
# A dataclass is one known case for when `members` can be `None`
2286+
if members is None:
2287+
return False
2288+
return node.name in [name_obj.name for value, name_obj in members[0].items]

tests/functional/i/invalid/invalid_name/invalid_name_enum.py

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# pylint: disable=too-few-public-methods
33

44

5+
from dataclasses import dataclass
56
from enum import Enum
67

78

@@ -28,3 +29,11 @@ def __init__(self, red: int, green: int, blue: int) -> None:
2829
def as_hex(self) -> str:
2930
"""Get hex 'abcdef' representation for a color."""
3031
return f'{self.red:0{2}x}{self.green:0{2}x}{self.blue:0{2}x}'
32+
33+
34+
@dataclass
35+
class Something(str, Enum):
36+
""" A false positive for ``invalid-name``
37+
which should be fixed by https://github.com/pylint-dev/astroid/issues/2317
38+
"""
39+
ASD: str = 1 # [invalid-name]
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
invalid-name:16:4:16:14:Color:"Class constant name ""aquamarine"" doesn't conform to UPPER_CASE naming style":HIGH
1+
invalid-name:17:4:17:14:Color:"Class constant name ""aquamarine"" doesn't conform to UPPER_CASE naming style":HIGH
2+
invalid-name:39:4:None:None:Something:"Attribute name ""ASD"" doesn't conform to snake_case naming style":HIGH

0 commit comments

Comments
 (0)