Skip to content

Issues with enum indexing #1464

Closed
Closed

Description

@JelleZijlstra

I encountered some issues with indexing on enum classes that use multiple inheritance. I'm using the following test script:

from enum import Enum
from typing import TYPE_CHECKING

if not TYPE_CHECKING:
    reveal_type = print

class I(int, Enum):
    a = 1
reveal_type(I['a'])  # E: Revealed type is '__main__.I'

class S(str, Enum):
    a = '1'
reveal_type(S['a'])  # E: Revealed type is '__main__.S'

class F(float, Enum):
    a = 1.0
reveal_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

  1. gvanrossum commented on Jul 4, 2017

    @gvanrossum
    Member

    I never did like #1352 much, so fine to revert if there's nothing better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions