Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive invalid-enum-extension for Enum parent class with annotations #8830

Closed
rarredon opened this issue Jul 7, 2023 · 2 comments · Fixed by #8834
Closed

False positive invalid-enum-extension for Enum parent class with annotations #8830

rarredon opened this issue Jul 7, 2023 · 2 comments · Fixed by #8834
Assignees
Labels
enum False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@rarredon
Copy link

rarredon commented Jul 7, 2023

Bug description

Pylint incorrectly flags an Enum inherited from a parent Enum which has annotations. This is useful when overriding __new__ or __init__ on the parent Enum to add extra data to the Enum members while also keeping the codebase typed. Example:

example.py

from enum import Enum


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)

Configuration

No response

Command used

pylint example.py

Pylint output

************* Module example
example.py:14:0: E0244: Extending inherited Enum class "ColorEnum" (invalid-enum-extension)

Expected behavior

No output

Pylint version

pylint 2.17.4
astroid 2.15.5
Python 3.8.16 (default, Mar 15 2023, 13:36:46)
[Clang 14.0.0 (clang-1400.0.29.202)]

OS / Environment

macOS Ventura 13.2.1

Additional dependencies

No response

@rarredon rarredon added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 7, 2023
@Why-not-now
Copy link

Why-not-now commented Jul 7, 2023

I think you misunderstood how enum is used. An example of the usage of Enum is to enumerate a list of limited selection of values (i.e. the days of the week). Here is the code for the example mentioned.

from enum import Enum
class Weekday(Enum):
    MONDAY = 1
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7

As you can see, Enum shouldn't be used to store values within its classes in the same way you store 3D coordinates into a class.

You can learn more about enum over here.

@rarredon
Copy link
Author

rarredon commented Jul 7, 2023

@Why-not-now there are several examples from the python docs involving Enums that store additional data to the Enum members. See for example https://docs.python.org/3/howto/enum.html#planet

@jacobtylerwalls jacobtylerwalls added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jul 7, 2023
@jacobtylerwalls jacobtylerwalls self-assigned this Jul 7, 2023
@jacobtylerwalls jacobtylerwalls added this to the 2.17.5 milestone Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enum False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants