def is_valid(stencil, max_neighborhood=None):
    """
    Tests if a nested sequence is a valid stencil i.e. all the inner sequences have the same length.
    If max_neighborhood is specified, it is also verified that the stencil does not contain any direction components
    with absolute value greater than the maximal neighborhood.

    Examples:
        >>> is_valid([(1, 0), (1, 0, 0)])  # stencil entries have different length
        False
        >>> is_valid([(2, 0), (1, 0)])
        True
        >>> is_valid([(2, 0), (1, 0)], max_neighborhood=1)
        False
    """
    expected_dim = len(stencil[0])
    for d in stencil:
        if len(d) != expected_dim:
            return False
        if max_neighborhood is not None:
            for d_i in d:
                if abs(d_i) > max_neighborhood:
                    return False
    return True
