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.

Author sobolevn
Recipients ethan.furman, pablogsal, sobolevn
Date 2022-01-03.13:23:31
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1641216211.34.0.269752308501.issue46242@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2022-01-03 13:23:31sobolevnsetrecipients: + sobolevn, ethan.furman, pablogsal
2022-01-03 13:23:31sobolevnsetmessageid: <1641216211.34.0.269752308501.issue46242@roundup.psfhosted.org>
2022-01-03 13:23:31sobolevnlinkissue46242 messages
2022-01-03 13:23:31sobolevncreate