You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I encountered some issues with indexing on enum classes that use multiple inheritance. I'm using the following test script:
fromenumimportEnumfromtypingimportTYPE_CHECKINGifnotTYPE_CHECKING:
reveal_type=printclassI(int, Enum):
a=1reveal_type(I['a']) # E: Revealed type is '__main__.I'classS(str, Enum):
a='1'reveal_type(S['a']) # E: Revealed type is '__main__.S'classF(float, Enum):
a=1.0reveal_type(F['a']) # E: Revealed type is '__main__.F'
All of this works at runtime; it prints I.a, S.a, F.a.
With current mypy master (incorporating most recent typeshed changes) I get:
$ mypy bin/enumindex.py
bin/enumindex.py:7: error: Definition of "__new__" in base class "int" is incompatible with definition in base class "Enum"
bin/enumindex.py:9: error: Revealed type is 'Any'
bin/enumindex.py:9: error: Value of type Type[I] is not indexable
bin/enumindex.py:11: error: Definition of "__new__" in base class "str" is incompatible with definition in base class "Enum"
bin/enumindex.py:13: error: Revealed type is 'Any'
bin/enumindex.py:13: error: Value of type Type[S] is not indexable
bin/enumindex.py:17: error: Revealed type is 'enumindex.F'
With the most recent mypy release it's better:
$ mypy bin/enumindex.py
bin/enumindex.py:9: error: Revealed type is 'enumindex.I'
bin/enumindex.py:13: error: Revealed type is 'Any'
bin/enumindex.py:13: error: Value of type "S" is not indexable
bin/enumindex.py:17: error: Revealed type is 'enumindex.F'
So there's a regression, which is caused by #1352 (cc @sproshev). Perhaps we should revert that PR? It fixed a relatively obscure case, and multiple inheritance from Enum is explicitly mentioned in the docs (https://docs.python.org/3/library/enum.html#others).
But the previous behavior was buggy too: somehow indexing worked on multiple inheritance from float or int, but not str. I'm not sure how to explain that yet. It doesn't reproduce in a mypy test, so presumably it's due to something in the typeshed stubs.
Activity
gvanrossum commentedon Jul 4, 2017
I never did like #1352 much, so fine to revert if there's nothing better.