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

Document in the main Enum page that super().__new__ should not be called #115821

Closed
Gouvernathor opened this issue Feb 22, 2024 · 2 comments
Closed
Labels
docs Documentation in the Doc dir

Comments

@Gouvernathor
Copy link
Contributor

Gouvernathor commented Feb 22, 2024

The HowTo page says here that super().__new__ should not be called in user-defined __new__ methods of an Enum (subclass), along with a short explanation for why it is the case.

But this is a hard limitation, not a bonus explanation. When I tried using the super, I ended up with an exception I didn't understand and the doc entry for Enum.__new__ didn't help me at all as per why that was failing.

The explanation should probably remain in the howto page, but a similar red warning should be added to Enum.__new__, saying that calling super().__new__ inside user-defined __new__ functions is forbidden.

Linked PRs

@Gouvernathor Gouvernathor added the docs Documentation in the Doc dir label Feb 22, 2024
@ethanfurman
Copy link
Member

That's a good idea.

Separately, how could the error message you received be made clearer?

@Gouvernathor
Copy link
Contributor Author

Gouvernathor commented Feb 22, 2024

The traceback was as follows :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\...\parliamentarch\__init__.py", line 54, in <module>
    class FillingStrategy(enum.StrEnum):
  File "C:\Program Files\Python312\Lib\enum.py", line 593, in __new__
    raise exc.with_traceback(tb)
  File "C:\Program Files\Python312\Lib\enum.py", line 583, in __new__
    enum_class = super().__new__(metacls, cls, bases, classdict, **kwds)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\enum.py", line 266, in __set_name__
    enum_member = enum_class._new_member_(enum_class, *args)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\...\parliamentarch\__init__.py", line 56, in __new__
    super().__new__(cls, value)
  File "C:\Program Files\Python312\Lib\enum.py", line 1138, in __new__
    raise TypeError("%r has no members defined" % cls)
TypeError: <enum 'FillingStrategy'> has no members defined
>>>

I couldn't tell you how to solve it because I don't know any more about the situation than what the howto warning says.
If you're only asking about wording, then maybe "Using super().__new__ is forbidden in Enum subclasses" or something like that.

ethanfurman added a commit that referenced this issue Feb 28, 2024
…ew__() (GH-116065)

Add note to not call super().__new__() in docs.
ethanfurman added a commit that referenced this issue Feb 28, 2024
…H-116063)

docs now state to not call super().__new__
if super().__new__ is called, a better error message is now used
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
…w__() (pythonGH-116063)

docs now state to not call super().__new__
if super().__new__ is called, a better error message is now used
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…w__() (pythonGH-116063)

docs now state to not call super().__new__
if super().__new__ is called, a better error message is now used
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…w__() (pythonGH-116063)

docs now state to not call super().__new__
if super().__new__ is called, a better error message is now used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants