예제 #1
0
def container_of(inner, arg, *, type, min_length=0, flatten=False, **kwargs):
    if not is_iterable(arg):
        raise IbisTypeError('Argument must be a sequence')

    if len(arg) < min_length:
        raise IbisTypeError(
            f'Arg must have at least {min_length} number of elements')

    if flatten:
        arg = flatten_iterable(arg)

    return type(inner(item, **kwargs) for item in arg)
예제 #2
0
def one_of(inners, arg, **kwargs):
    """At least one of the inner validators must pass"""
    for inner in inners:
        with suppress(IbisTypeError, ValueError):
            return inner(arg, **kwargs)

    raise IbisTypeError("argument passes none of the following rules: "
                        f"{', '.join(map(repr, inners))}")
예제 #3
0
파일: core.py 프로젝트: ibis-project/ibis
def cast(source: str | DataType, target: str | DataType, **kwargs) -> DataType:
    """Attempts to implicitly cast from source dtype to target dtype"""
    source, target = dtype(source), dtype(target)

    if not castable(source, target, **kwargs):
        raise IbisTypeError(
            f'Datatype {source} cannot be implicitly casted to {target}'
        )
    return target
예제 #4
0
파일: core.py 프로젝트: ibis-project/ibis
def higher_precedence(left: DataType, right: DataType) -> DataType:
    if castable(left, right, upcast=True):
        return right
    elif castable(right, left, upcast=True):
        return left

    raise IbisTypeError(
        f'Cannot compute precedence for {left} and {right} types'
    )
예제 #5
0
파일: core.py 프로젝트: ibis-project/ibis
def from_string(value: str) -> DataType:
    try:
        return parse(value)
    except SyntaxError:
        raise IbisTypeError(f'{value!r} cannot be parsed as a datatype')
예제 #6
0
파일: core.py 프로젝트: ibis-project/ibis
def default(value, **kwargs) -> DataType:
    raise IbisTypeError(f'Value {value!r} is not a valid datatype')
예제 #7
0
def instance_of(klasses, arg, **kwargs):
    """Require that a value has a particular Python type."""
    if not isinstance(arg, klasses):
        raise IbisTypeError(f'Given argument with type {type(arg)} '
                            f'is not an instance of {klasses}')
    return arg