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

Check the amount of base types in Enum #11948

Closed
sobolevn opened this issue Jan 9, 2022 · 0 comments · Fixed by #12026
Closed

Check the amount of base types in Enum #11948

sobolevn opened this issue Jan 9, 2022 · 0 comments · Fixed by #12026
Labels
bug mypy got something wrong topic-enum

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 9, 2022

Right now this code does not produce any issues in mypy:

import enum

class First:
    def __new__(cls, val):
        return 1

class Second:
    def __new__(cls, val):
        return 2

class Some(First, Second, enum.Enum):  # ok
    x = 1

But, in runtime it fails:

>>> import enum
>>> 
>>> class First:
...     def __new__(cls, val):
...         return 1
... 
>>> class Second:
...     def __new__(cls, val):
...         return 2
... 
>>> class Some(First, Second, enum.Enum):
...     x = 1
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 403, in __prepare__
    member_type, first_enum = metacls._get_mixins_(cls, bases)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 893, in _get_mixins_
    member_type = _find_data_type(bases) or object
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 881, in _find_data_type
    raise TypeError('%r: too many data types: %r' % (class_name, data_types))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'Some': too many data types: {<class '__main__.Second'>, <class '__main__.First'>}

This happens because Enum can have only one extra subclass with __new__, because it is used to create new Enum members.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants