@lower_cast(types.Optional, types.Optional)
def optional_to_optional(context, builder, fromty, toty, val):
 optval = context.make_helper(builder, fromty, value=val)
 validbit = cgutils.as_bool_bit(builder, optval.valid)
 outoptval = context.make_helper(builder, toty)
 with builder.if_else(validbit) as (is_valid, is_not_valid):
  with is_valid:
   outoptval.valid = cgutils.true_bit
   outoptval.data = context.cast(builder, optval.data, fromty.type, toty.type)
  with is_not_valid:
   outoptval.valid = cgutils.false_bit
   outoptval.data = cgutils.get_null_value(outoptval.data.type)
 return outoptval._getvalue()

@ lower_cast ( types . Optional , types . Optional ) def optional_to_optional ( context , builder , fromty , toty , val ) : optval = context . make_helper ( builder , fromty , value = val ) validbit = cgutils . as_bool_bit ( builder , optval . valid ) outoptval = context . make_helper ( builder , toty ) with builder . if_else ( validbit ) as ( is_valid , is_not_valid ) : with is_valid : outoptval . valid = cgutils . true_bit outoptval . data = context . cast ( builder , optval . data , fromty . type , toty . type ) with is_not_valid : outoptval . valid = cgutils . false_bit outoptval . data = cgutils . get_null_value ( outoptval . data . type ) return outoptval . _getvalue ( )
the handling of optional->optional cast must be special cased for correct propagation of none value .