[New-bugs-announce] [issue46242] Improve error message when creating an enum with `__call__`
Nikita Sobolev
report at bugs.python.org
Mon Jan 3 08:23:31 EST 2022
New submission from Nikita Sobolev <mail at sobolevn.me>:
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.
----------
components: Library (Lib)
messages: 409583
nosy: ethan.furman, pablogsal, sobolevn
priority: normal
severity: normal
status: open
title: Improve error message when creating an enum with `__call__`
type: behavior
versions: Python 3.10, Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46242>
_______________________________________
More information about the New-bugs-announce
mailing list