This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Improve error message when attempting to extend an enum with `__call__`
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: ethan.furman, pablogsal, sobolevn
Priority: normal Keywords: patch

Created on 2022-01-03 13:23 by sobolevn, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30357 merged sobolevn, 2022-01-03 13:34
Messages (2)
msg409583 - (view) Author: Nikita Sobolev (sobolevn) * (Python triager) Date: 2022-01-03 13:23
Right now when creating a new `Enum`, we check not to extend `Enum` with existing `_member_names_`:

```python
Python 3.11.0a3+ (heads/main:8d7644fa64, Dec 30 2021, 13:00:40) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class A(enum.Enum):
...   a = 1
... 
>>> class B(A): pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 398, in __prepare__
    metacls._check_for_existing_members(cls, bases)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 850, in _check_for_existing_members
    raise TypeError(
    ^^^^^^^^^^^^^^^^
TypeError: B: cannot extend enumeration 'A'
```

But when we try to use `A()` call to do the same, where what happens:

```
Python 3.11.0a3+ (heads/main:8d7644fa64, Dec 30 2021, 13:00:40) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import enum
>>> class A(enum.Enum):
...   a = 1
... 
>>> B = A('B', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 606, in __call__
    return cls._create_(
           ^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 770, in _create_
    _, first_enum = cls._get_mixins_(class_name, bases)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 899, in _get_mixins_
    raise TypeError('Cannot extend enumerations')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Cannot extend enumerations
```

I propose to use the first error message in this case as well. Moreover, this behavior is not covered with tests:

```
» ag 'Cannot extend enumerations'
Lib/enum.py
899:            raise TypeError('Cannot extend enumerations')
```

I will add tests for this edge case.
msg410603 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2022-01-14 22:18
New changeset e674e48ddc2712f28cc7ecdc66a6c328066694b0 by Nikita Sobolev in branch 'main':
bpo-46242: [Enum] better error message for extending `Enum` with members (GH-30357)
https://github.com/python/cpython/commit/e674e48ddc2712f28cc7ecdc66a6c328066694b0
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90400
2022-01-14 23:58:17AlexWaygoodsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-01-14 22:18:22ethan.furmansetmessages: + msg410603
2022-01-08 10:16:45AlexWaygoodsetassignee: ethan.furman
2022-01-03 16:49:45AlexWaygoodsettitle: Improve error message when creating an enum with `__call__` -> Improve error message when attempting to extend an enum with `__call__`
2022-01-03 13:34:30sobolevnsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28571
2022-01-03 13:23:31sobolevncreate