GitHub

API reference for Brand.

A checked constructor that brands an A as B (Effect's Brand.Constructor). Name both types when making one: Brand[float, Int].make(...). Calling it raises BrandError on failure; option, result and is_ report it instead.

Attributes

AttributeTypeDescription
filterstuple[Filter[A], ...] = ()

Functions

__init__

__init__(filters=()) -> None
ParameterTypeDescription
filterstuple[Filter[A], ...] = ()

nominal

nominal() -> Self

A constructor that checks nothing.

Returns Self

@classmethod
def nominal(cls) -> Self:
    """A constructor that checks nothing."""
    return cls()

make

make(predicate) -> Self

A constructor with one check, predicate.

ParameterTypeDescription
predicateCallable[[A], FilterOutput]

Returns Self

@classmethod
def make(cls, predicate: Callable[[A], FilterOutput]) -> Self:
    """A constructor with one check, ``predicate``."""
    return cls((Filter(predicate),))

check

check(first, *rest) -> Self

A constructor running the filters in order.

ParameterTypeDescription
firstFilter[A]
restFilter[A] = ()

Returns Self

@classmethod
def check(cls, first: Filter[A], /, *rest: Filter[A]) -> Self:
    """A constructor running the filters in order."""
    return cls((first, *rest))

all

all(first, *rest) -> Self

A constructor running the checks of every brand given, in order.

ParameterTypeDescription
firstBrand[A, object]
restBrand[A, object] = ()

Returns Self

@classmethod
def all(cls, first: Brand[A, object], /, *rest: Brand[A, object]) -> Self:
    """A constructor running the checks of every brand given, in order."""
    return cls(tuple(f for b in (first, *rest) for f in b.filters))

__call__

__call__(value) -> B
ParameterTypeDescription
valueA

Returns B

def __call__(self, value: A) -> B:
    match self.result(value):
        case Ok(branded):
            return branded
        case Err(error):
            raise error

result

result(value) -> Result[B, BrandError]
ParameterTypeDescription
valueA

Returns Result[B, BrandError]

def result(self, value: A) -> Result[B, BrandError]:
    failures: list[str] = []
    for f in self.filters:
        failure = _failure(f, value)
        if failure is not None:
            failures.append(failure)
            if f.aborted:
                break
    if failures:
        return Err(BrandError("\n".join(failures)))
    # A brand is a ``NewType``: the branded value is the value itself.
    return Ok(cast("B", value))

option

option(value) -> Option[B]

Some(branded), or None when value fails.

ParameterTypeDescription
valueA

Returns Option[B]

def option(self, value: A) -> Option[B]:
    """``Some(branded)``, or ``None`` when ``value`` fails."""
    match self.result(value):
        case Ok(branded):
            return Some(branded)
        case Err():
            return None

is_

is_(value) -> TypeGuard[B]

value passes the checks (and so is already a B at run time).

ParameterTypeDescription
valueA

Returns TypeGuard[B]

def is_(self, value: A) -> TypeGuard[B]:
    """``value`` passes the checks (and so is already a ``B`` at run time)."""
    return isinstance(self.result(value), Ok)