IntEnum from int

Numba has enum support, including IntEnum

However, I’m having trouble converting an int value into the corresponding enum in a jit func. The below works fine without jitting

from enum import IntEnum
from numba import njit

class Bool(IntEnum):
    FALSE = 1
    TRUE = 2

# @njit()
def func(val):
    return Bool(val)


but fails with this error when jitted.

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of IntEnum class(Bool) with parameters (int64)
No type info available for IntEnum class(Bool) as a callable.
During: resolving callee type: IntEnum class(Bool)

I’m sure I’m doing something silly, but I didn’t find an example that did this in either the tests or generally poking about on the net.

This appears to work fine on current master (b1258c82c0ed2845fce52508f439e29f729384a9) – which version of Numba are you using?

Hej :slight_smile:

@esc For me the latest version on master is not working. Did you maybe forget to uncomment the njit decorator when testing?

I have been working on improving Enum support but ran into a bit of a dead end. I will try to make a PR this weekend to show what I got so far, maybe someone can help me to finish things up or tell me it’s hopeless :stuck_out_tongue:

It should be said that the Enums in CPython are a bit weird if you look into the details of how they are implemented.
IntEnums are even weirder because their members are more like monkey-patched integers. (similar for StrEnums etc.)

One of the issues here might be that numba does not lower all the information about an Enum, only the value field iirc. So that might be part of why it cannot be reconstructed.

This issue is what got me interested in Enums: Feature Request: __hash__ for Enum and IntEnum · Issue #5911 · numba/numba · GitHub
It’s not directly related but maybe it helps someone to jump into the related code.

If I am reading the implementations correctly, the behaviour @nelson2005 is looking for, is not currently implemented, see numba/ at master · numba/numba · GitHub

@esc For me the latest version on master is not working. Did you maybe forget to uncomment the njit decorator when testing?

Indeed, sorry about that, yes it is broken on master for me too.

Thanks for the responses… I was hoping I was doing something silly. :slightly_smiling_face:


I managed to make a PR for what I had been working on. At this point I don’t really think it should get merged given the increase in complexity for little gain. But maybe there is something that can sow inspiration :slight_smile: