Skip to content

gh-128080: remove unnecessary __init__ method from Enum - #128081

Merged
ethanfurman merged 2 commits into
python:mainfrom
tungol:enum_init
Dec 19, 2024
Merged

gh-128080: remove unnecessary __init__ method from Enum#128081
ethanfurman merged 2 commits into
python:mainfrom
tungol:enum_init

Conversation

@tungol

@tungoltungol commented Dec 18, 2024

Copy link
Copy Markdown
Contributor

Enum gained an __init__ method in 3.11 which just passes:

def__init__(self, *args, **kwds):
pass

That was added in #30582. One person questioned it at the time, but there was no response. The best I can figure is that it was related to special handling of __init__ in EnumType.__dir__, which you can see here:

cpython/Lib/enum.py

Lines 768 to 779 in 3852269

def__dir__(cls):
# TODO: check for custom __init__, __new__, __format__, __repr__, __str__, __init_subclass__
# on object-based enums
ifcls._member_type_isobject:
interesting=set(cls._member_names_)
ifcls._new_member_isnotobject.__new__:
interesting.add('__new__')
ifcls.__init_subclass__isnotobject.__init_subclass__:
interesting.add('__init_subclass__')
formethodin ('__init__', '__format__', '__repr__', '__str__'):
ifgetattr(cls, method) notin (getattr(Enum, method), getattr(Flag, method)):
interesting.add(method)

That special handling of __init__ was removed by #30677 prior to 3.11 being released, which leaves no plausible function to Enum.__init__ that I can see. It muddies introspection of the type, so I think we should remove it if that's really the case.

@ZeroIntensity

Copy link
Copy Markdown
Member

I've fixed the failing macOS CI for you :)

@ethanfurman
ethanfurman merged commit c14db20 into python:mainDec 19, 2024
@tungol
tungol deleted the enum_init branch December 20, 2024 00:20
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Dec 23, 2024
…onGH-128081)
remove unnecessary __init__ method from Enum
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
…onGH-128081)
remove unnecessary __init__ method from Enum
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@tungol@ZeroIntensity@ethanfurman