def _replace_arg(arg, tvars, args):
 if (tvars is None):
  tvars = []
 if hasattr(arg, u'_subs_tree'):
  return arg._subs_tree(tvars, args)
 if isinstance(arg, TypeVar):
  for (i, tvar) in enumerate(tvars):
   if (arg == tvar):
    return args[i]
 return arg

def _replace_arg ( arg , tvars , args ) : if ( tvars is None ) : tvars = [ ] if hasattr ( arg , u'_subs_tree' ) : return arg . _subs_tree ( tvars , args ) if isinstance ( arg , TypeVar ) : for ( i , tvar ) in enumerate ( tvars ) : if ( arg == tvar ) : return args [ i ] return arg
an internal helper function: replace arg if it is a type variable found in tvars with corresponding substitution from args or with corresponding substitution sub-tree if arg is a generic type .