# Buggy code
def _prepare_argument(ctxt, bld, inp, tyinp, where="input operand"):
    """returns an instance of the appropriate Helper (either
    _ScalarHelper or _ArrayHelper) class to handle the argument.
    using the polymorphic interface of the Helper classes, scalar
    and array cases can be handled with the same code"""
    if isinstance(tyinp, types.ArrayCompatible):
        ary = ctxt.make_array(tyinp)(ctxt, bld, inp)
        shape = cgutils.unpack_tuple(bld, ary.shape, tyinp.ndim)
        strides = cgutils.unpack_tuple(bld, ary.strides, tyinp.ndim)
        return _ArrayHelper(
            ctxt,
            bld,
            shape,
            strides,
            ary.data,
            tyinp.layout,
            tyinp.dtype,
            tyinp.ndim,
            inp,
        )
    elif types.unliteral(tyinp) in types.number_domain | set([types.boolean]):
        return _ScalarHelper(ctxt, bld, inp, tyinp)
    else:
        raise NotImplementedError(
            "unsupported type for {0}: {1}".format(where, str(tyinp))
        )


# Traceback (numba.errors.TypingError)
@njit((float_[:], optional(float_[:])))
... def f(x, y):
...     if y is None:
...         return x
...     return x + y
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/decorators.py", line 198, in wrapper
disp.compile(sig)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/dispatcher.py", line 655, in compile
cres = self._compiler.compile(args, return_type)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/dispatcher.py", line 82, in compile
pipeline_class=self.pipeline_class)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 926, in compile_extra
return pipeline.compile_extra(func)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 374, in compile_extra
return self._compile_bytecode()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 857, in _compile_bytecode
return self._compile_core()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 844, in _compile_core
res = pm.run(self.status)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 255, in run
raise patched_exception
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 246, in run
stage()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 717, in stage_nopython_backend
self._backend(lowerfn, objectmode=False)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 666, in _backend
lowered = lowerfn()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 653, in backend_nopython_mode
self.metadata)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 1048, in native_lowering_stage
lower.lower()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 178, in lower
self.lower_normal_function(self.fndesc)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 219, in lower_normal_function
entry_block_tail = self.lower_function_body()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 244, in lower_function_body
self.lower_block(block)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 259, in lower_block
self.lower_inst(inst)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 308, in lower_inst
val = self.lower_assign(ty, inst)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 454, in lower_assign
return self.lower_expr(ty, value)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/lowering.py", line 1045, in lower_expr
res = self.context.special_ops[expr.op](self, expr)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/npyufunc/array_exprs.py", line 393, in _lower_array_expr
caching=False)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/targets/base.py", line 843, in compile_subroutine
flags=flags)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/targets/base.py", line 815, in _compile_subroutine_no_cache
locals=locals)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 951, in compile_internal
return pipeline.compile_extra(func)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 374, in compile_extra
return self._compile_bytecode()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 857, in _compile_bytecode
return self._compile_core()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 844, in _compile_core
res = pm.run(self.status)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
return func(*args, **kwargs)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 255, in run
raise patched_exception
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 246, in run
stage()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 485, in stage_nopython_frontend
self.locals)
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/compiler.py", line 1029, in type_inference_stage
infer.propagate()
File "/Users/lizli/anaconda3/lib/python3.7/site-packages/numba/typeinfer.py", line 860, in propagate
raise errors[0]
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython mode backend)
Failed in nopython mode pipeline (step: nopython frontend)
No conversion from array(float32, 1d, C) to float32 for '$0.4', defined at None

File "<stdin>", line 3:
<source missing, REPL/exec in use?>

[1] During: typing of assignment at <stdin> (3)

File "<stdin>", line 3:
<source missing, REPL/exec in use?>

[1] During: lowering "$12.3 = arrayexpr(expr=(<built-in function add>, [Var(x, <stdin> (3)), Var(y, <stdin> (3))]), ty=array(float32, 1d, C))" at <stdin> (5)

# Fixed code
def _prepare_argument(ctxt, bld, inp, tyinp, where="input operand"):
    """returns an instance of the appropriate Helper (either
    _ScalarHelper or _ArrayHelper) class to handle the argument.
    using the polymorphic interface of the Helper classes, scalar
    and array cases can be handled with the same code"""

    # first un-Optional Optionals
    if isinstance(tyinp, types.Optional):
        oty = tyinp
        tyinp = tyinp.type
        inp = ctxt.cast(bld, inp, oty, tyinp)

    # then prepare the arg for a concrete instance
    if isinstance(tyinp, types.ArrayCompatible):
        ary = ctxt.make_array(tyinp)(ctxt, bld, inp)
        shape = cgutils.unpack_tuple(bld, ary.shape, tyinp.ndim)
        strides = cgutils.unpack_tuple(bld, ary.strides, tyinp.ndim)
        return _ArrayHelper(
            ctxt,
            bld,
            shape,
            strides,
            ary.data,
            tyinp.layout,
            tyinp.dtype,
            tyinp.ndim,
            inp,
        )
    elif types.unliteral(tyinp) in types.number_domain | set([types.boolean]):
        return _ScalarHelper(ctxt, bld, inp, tyinp)
    else:
        raise NotImplementedError(
            "unsupported type for {0}: {1}".format(where, str(tyinp))
        )