How to avoid multiple overload functions for the same config variable

I have a config variable, which is a Numba Boolean Literal. I want to type switch based on the value of the variable. In post 2619, one of the comments suggested using overload, which worked. But here is the issue: my program looks like

@numba.njit
def aNumbaFunc(configFlag1):
    if configFlag1:
        do something_1 # Initialize a variable to int
    else:
        do something_1 else #Initialize a variable to string

    some operations in between
 
     if configFlag1:
        do something_2 
    else:
        do something_2 else
   

    some operations in between

And the above goes on for more if and else statements.

The above can’t be compiled in Numba if I used the typical if-else statements.

Using overload, the program will look like

def do_something_1(configFlag1):
    pass

@numba.extending.overload(do_something_1)
def ol_do_something_1(configFlag1):
    if configFlag1.literal_value:
        def do_something(configFlag1):
            do_something_1
            return something
        return do_something
    else:
         def do_something_else(configFlag1):
            do_something_1_else
            return something_else
        return do_something_else

@numba.njit
def aNumbaFunc(configFlag1):
    variable1 = do_something_1(configFlag1)

    some operations in between

I have to create one overload function for every if statement of a config variable. There are multiple config variables with various if statements; this could potentially become very large.

This should be a standard issue that Python users might face since Python can have multiple types for the same variable. Do you have any suggestions on what I should do in this case? Is there any feature of Numba that can tackle this that I might not be aware of?

I think you nailed it. This should be a quite fundamental feature request. IMHO, if numba wants to support this, it has to implement a powerful dataflow and control flow analysis to let type inference happy. SSA should be able to do this, just need more analysis information.

A somewhat close solution. So, there is this dead branch pruning in numba

@numba.njit
def aNumaFunc(flag):
    if flag is None:
        variable1 = None
    else
        variable1 = "a different dataype"
    return variable1

The above code will work but,

@numba.njit
def aNumaFunc(flag):
    if flag is None:
        variable1 = 1
    else
        variable1 = "a different dataype"
    return variable1

This would not.

Hey @Sumit112192

Numba has an “Optional” data type which is used to represent a type that can either be a value of a specific type or None. That’s why your 1st example works. It will create an optional type of unicode or None.
If you need type based dispatch or literal value based dispatch you may use “overload” if possible.