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#
| Attribute | Type | Description |
|---|---|---|
filters | tuple[Filter[A], ...] = () |
Functions#
__init__#
__init__(filters=()) -> None| Parameter | Type | Description |
|---|---|---|
filters | tuple[Filter[A], ...] = () |
nominal#
nominal() -> SelfA constructor that checks nothing.
Returns Self
@classmethod
def nominal(cls) -> Self:
"""A constructor that checks nothing."""
return cls()make#
make(predicate) -> SelfA constructor with one check, predicate.
| Parameter | Type | Description |
|---|---|---|
predicate | Callable[[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) -> SelfA constructor running the filters in order.
| Parameter | Type | Description |
|---|---|---|
first | Filter[A] | |
rest | Filter[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) -> SelfA constructor running the checks of every brand given, in order.
| Parameter | Type | Description |
|---|---|---|
first | Brand[A, object] | |
rest | Brand[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| Parameter | Type | Description |
|---|---|---|
value | A |
Returns B
def __call__(self, value: A) -> B:
match self.result(value):
case Ok(branded):
return branded
case Err(error):
raise errorresult#
result(value) -> Result[B, BrandError]| Parameter | Type | Description |
|---|---|---|
value | A |
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.
| Parameter | Type | Description |
|---|---|---|
value | A |
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 Noneis_#
is_(value) -> TypeGuard[B]value passes the checks (and so is already a B at run time).
| Parameter | Type | Description |
|---|---|---|
value | A |
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)