Skip to content

false positive for type-hinted Enum class instance variable #7402

@huwcbjones

Description

@huwcbjones

Bug description

As per https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html#classes, you can optionally declare instance variables in the class body. For normal classes, this is fine, but for Enums pylint raises a false positive

Define an enum with a type-hinted instance variable and you get an invalid name! 😞

class MyEnum(str, Enum):
    attr: int

    VALUE = "a", 1

    def __new__(cls, value, attr=None):
        obj = str.__new__(cls, value)
        obj._value_ = value
        obj.attr = attr
        return obj

Configuration

No response

Command used

pylint --disable=all --enable=invalid-name mwe.py

(pylint mwe.py also works, I just wanted to filter out the irrelavent messages)

Pylint output

************* Module mwe
mwe.py:5:4: C0103: Class constant name "attr" doesn't conform to UPPER_CASE naming style (invalid-name)

--------------------------------------------------------------------
Your code has been rated at 8.89/10

Expected behavior

No error

Pylint version

pylint 2.12.2/2.15.0
astroid 2.9.3/2.12.5
Python 3.9.1 (default, Mar 15 2021, 18:22:18)
[Clang 12.0.0 (clang-1200.0.32.27)]

OS / Environment

macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions