def _concat_lists_safe(A, B):
    """ Safely concatenate two lists if one or both may be None """

    if A is None and B is None:
        return None
    elif isinstance(A, list) and B is None:
        return A
    elif isinstance(B, list) and A is None:
        return B
    elif isinstance(A, list) and isinstance(B, list):
        return A + B
    else:
        raise TypeError("A and B must be lists or None")