From 2c3425dc429293a77a86cd11361a8b268aa8fc91 Mon Sep 17 00:00:00 2001 From: Mark Byrne <31762852+mbyrnepr2@users.noreply.github.com> Date: Wed, 4 Oct 2023 07:39:57 +0200 Subject: [PATCH] Fix a crash for Enum class decorated with dataclass (#9104) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 <13665637+DanielNoord@users.noreply.github.com> --------- Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com> --- doc/whatsnew/fragments/9100.other | 3 +++ pylint/checkers/utils.py | 7 +++++-- .../i/invalid/invalid_name/invalid_name_enum.py | 9 +++++++++ .../i/invalid/invalid_name/invalid_name_enum.txt | 3 ++- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 doc/whatsnew/fragments/9100.other diff --git a/doc/whatsnew/fragments/9100.other b/doc/whatsnew/fragments/9100.other new file mode 100644 index 0000000000..75e19175b9 --- /dev/null +++ b/doc/whatsnew/fragments/9100.other @@ -0,0 +1,3 @@ +Fix a crash when an enum class which is also decorated with a ``dataclasses.dataclass`` decorator is defined. + +Closes #9100 diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index d3a344c9c6..eb9d52d4d5 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -2281,5 +2281,8 @@ def is_enum_member(node: nodes.AssignName) -> bool: ): return False - enum_member_objects = frame.locals.get("__members__")[0].items - return node.name in [name_obj.name for value, name_obj in enum_member_objects] + members = frame.locals.get("__members__") + # A dataclass is one known case for when `members` can be `None` + if members is None: + return False + return node.name in [name_obj.name for value, name_obj in members[0].items] diff --git a/tests/functional/i/invalid/invalid_name/invalid_name_enum.py b/tests/functional/i/invalid/invalid_name/invalid_name_enum.py index 5338a03693..0857b25a5c 100644 --- a/tests/functional/i/invalid/invalid_name/invalid_name_enum.py +++ b/tests/functional/i/invalid/invalid_name/invalid_name_enum.py @@ -2,6 +2,7 @@ # pylint: disable=too-few-public-methods +from dataclasses import dataclass from enum import Enum @@ -28,3 +29,11 @@ def __init__(self, red: int, green: int, blue: int) -> None: def as_hex(self) -> str: """Get hex 'abcdef' representation for a color.""" return f'{self.red:0{2}x}{self.green:0{2}x}{self.blue:0{2}x}' + + +@dataclass +class Something(str, Enum): + """ A false positive for ``invalid-name`` + which should be fixed by https://github.com/pylint-dev/astroid/issues/2317 + """ + ASD: str = 1 # [invalid-name] diff --git a/tests/functional/i/invalid/invalid_name/invalid_name_enum.txt b/tests/functional/i/invalid/invalid_name/invalid_name_enum.txt index 9383a49dbf..9e1de87595 100644 --- a/tests/functional/i/invalid/invalid_name/invalid_name_enum.txt +++ b/tests/functional/i/invalid/invalid_name/invalid_name_enum.txt @@ -1 +1,2 @@ -invalid-name:16:4:16:14:Color:"Class constant name ""aquamarine"" doesn't conform to UPPER_CASE naming style":HIGH +invalid-name:17:4:17:14:Color:"Class constant name ""aquamarine"" doesn't conform to UPPER_CASE naming style":HIGH +invalid-name:39:4:None:None:Something:"Attribute name ""ASD"" doesn't conform to snake_case naming style":HIGH