import six
import ast


def to_name(str_or_name, ctx=None):
    """ Cast a string to a Name AST node.
    """
    if isinstance(str_or_name, six.string_types):
        id = str_or_name
    elif isinstance(str_or_name, ast.Name):
        id = str_or_name.id
    else:
        raise TypeError("Argument must be a string or a Name AST node")
    return ast.Name(id, ctx or ast.Load())