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
Much of our internal Python code uses a custom Enum class as an optimization above the built-in Enum type. These should be treated the same as built-in enums in terms of the custom enum handling in mypy, but since mypy looks for enum.Enum as the base-class in order to trigger the is_enum behavior, we can't get this to trigger without monkey-patching mypy.
There's a variety of solutions that could help, a couple that could work:
Allow a list of additional Enum base classes to be passed in to mypy or specified in the .ini file, and use those in addition to enum.Enum.
Allow for an annotation on the custom Enum base class that opts that class and any subclasses into the is_enum behavior
We're happy to extend mypy with either of those options (or other suggested ones), but we wanted to get alignment here before starting work.
I don't understand enough about your problem to propose a solution. Can't you create a stub for it that pretends it derives from enum.Enum?
Alternatively, if we changed mypy so that instead of checking "is the type exactly enum.Enum (or a few others)" it checked "is this type's metaclass derived from enum.EnumMeta", then could you write a stub?
A stub with from enum import Enum works just fine for us. Thanks for the suggestion! Slightly embarrassed that none of us thought of that; guess I need to get used to thinking of stubs as a way to lie to mypy ;-)
Activity
gvanrossum commentedon Apr 22, 2017
Our own custom enums are so crusty that they don't work anyways. :-(
Maybe a better approach would be to use the EnumMeta class as a flag instead?
ambv commentedon Apr 24, 2017
Could we make this work with a C-based Enum implementation? The thing is, our optimized version is Cython, we wrote it for perf reasons.
gvanrossum commentedon Apr 24, 2017
I don't understand enough about your problem to propose a solution. Can't you create a stub for it that pretends it derives from enum.Enum?
Alternatively, if we changed mypy so that instead of checking "is the type exactly enum.Enum (or a few others)" it checked "is this type's metaclass derived from enum.EnumMeta", then could you write a stub?
carljm commentedon May 23, 2017
A stub with
from enum import Enumworks just fine for us. Thanks for the suggestion! Slightly embarrassed that none of us thought of that; guess I need to get used to thinking of stubs as a way to lie to mypy ;-)JukkaL commentedon May 23, 2017
@carljm Can we close this issue now that you have a workaround?
carljm commentedon May 24, 2017
@JukkaL Yes, fine to close as far as I'm concerned.
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums
Static Type Checking: Enums