GitHub

SchemaAsserts

API reference for SchemaAsserts.

Assertions for one schema; a schema needing services to decode (RD) or encode (RE) gets them from provide on each direction.

Attributes

AttributeTypeDescription
schemaCodec[object, object, RD_co, RE_co]

Functions

__init__

__init__(schema) -> None
ParameterTypeDescription
schemaCodec[object, object, RD_co, RE_co]

make

make(**options) -> SchemaCheck[RD_co]

Construction: the decoded side, with constructor defaults.

ParameterTypeDescription
optionsUnpack[ParseOptions] = {}

Returns SchemaCheck[RD_co]

def make(self, **options: Unpack[ParseOptions]) -> SchemaCheck[RD_co]:
    """Construction: the decoded side, with constructor defaults."""
    schema = self.schema.__codec__()

    def parse(input: object) -> Effect[object, SchemaError, RD_co]:
        return schema.make_effect(input, **options).map_error(SchemaError)

    return SchemaCheck(parse)

decoding

decoding(**options) -> SchemaCheck[RD_co]
ParameterTypeDescription
optionsUnpack[ParseOptions] = {}

Returns SchemaCheck[RD_co]

def decoding(self, **options: Unpack[ParseOptions]) -> SchemaCheck[RD_co]:
    return SchemaCheck(decode_unknown_effect(self.schema, **options))

encoding

encoding(**options) -> SchemaCheck[RE_co]
ParameterTypeDescription
optionsUnpack[ParseOptions] = {}

Returns SchemaCheck[RE_co]

def encoding(self, **options: Unpack[ParseOptions]) -> SchemaCheck[RE_co]:
    return SchemaCheck(encode_unknown_effect(self.schema, **options))

verify_round_trip

verify_round_trip(*, runs=100, seed=None) -> None

Each value to_strategy draws encodes, then decodes to a value the schema's equivalence calls equal (Effect's verifyRoundTrip; needs Hypothesis). A failing value is shrunk and reported.

ParameterTypeDescription
runsint = 100
seedint | None = None
def verify_round_trip(
    self: SchemaAsserts[Never, Never], *, runs: int = 100, seed: int | None = None
) -> None:
    """Each value ``to_strategy`` draws encodes, then decodes to a value
    the schema's equivalence calls equal (Effect's ``verifyRoundTrip``;
    needs Hypothesis). A failing value is shrunk and reported."""
    verify: Effect[None] = self.verify_round_trip_effect(runs=runs, seed=seed)
    run_blocking(verify)

verify_round_trip_effect

verify_round_trip_effect[R](*, runs=100, seed=None) -> Effect[None, Never, R]

verify_round_trip as an effect with the running fiber's services (Effect's verifyRoundTripEffect): a mismatch is an AssertionError defect; any other failure than a schema error ends the check and fails the effect with its cause. Hypothesis draws synchronously, so each value's round trip runs to its end in a nested synchronous run (no asyncio, no interruption midway).

ParameterTypeDescription
runsint = 100
seedint | None = None

Returns Effect[None, Never, R]

def verify_round_trip_effect[R](
    self: SchemaAsserts[R, R], *, runs: int = 100, seed: int | None = None
) -> Effect[None, Never, R]:
    """``verify_round_trip`` as an effect with the running fiber's
    services (Effect's ``verifyRoundTripEffect``): a mismatch is an
    ``AssertionError`` defect; any other failure than a schema error
    ends the check and fails the effect with its cause. Hypothesis
    draws synchronously, so each value's round trip runs to its end in
    a nested synchronous run (no asyncio, no interruption midway)."""
    try:
        from hypothesis import HealthCheck, given, settings
        from hypothesis import seed as hypothesis_seed
    except ImportError as error:
        raise missing_extra("verify_round_trip", "hypothesis", error) from error

    schema = self.schema

    # The services are the running fiber's: whatever ``R`` it was given.
    def verify(ctx: Context[Any]) -> Effect[None]:
        encode = encode_unknown_effect(schema)
        decode = decode_unknown_effect(schema)
        equivalent = to_equivalence(schema)

        @settings(
            max_examples=runs,
            database=None,
            deadline=None,
            suppress_health_check=[HealthCheck.too_slow],
        )
        @given(to_strategy(schema))
        def check(value: object) -> None:
            round_trip = _schema_result(encode(value).flat_map(decode))
            ex = run_blocking_exit(round_trip, context=ctx)
            if not isinstance(ex, Success):
                raise _Abort(ex.cause)
            match ex.value:
                case Ok(back) if equivalent(back, value):
                    return
                case result:
                    raise AssertionError(f"{value!r} did not round-trip: {result!r}")

        try:
            (check if seed is None else hypothesis_seed(seed)(check))()
        except _Abort as abort:
            return fail_cause(abort.cause)
        return void

    return context().flat_map(lambda ctx: suspend(lambda: verify(ctx)))

verify_generation

verify_generation(count=30) -> None

to_strategy draws count values, each one of the schema's (Effect's arbitrary().verifyGeneration(); needs Hypothesis).

ParameterTypeDescription
countint = 30
def verify_generation(self, count: int = 30) -> None:
    """``to_strategy`` draws ``count`` values, each one of the schema's
    (Effect's ``arbitrary().verifyGeneration()``; needs Hypothesis)."""
    try:
        from hypothesis import HealthCheck, given, settings
    except ImportError as error:
        raise missing_extra("verify_generation", "hypothesis", error) from error

    is_value = is_(self.schema)

    @settings(
        max_examples=count,
        database=None,
        deadline=None,
        suppress_health_check=[HealthCheck.too_slow],
    )
    @given(to_strategy(self.schema))
    def check(value: object) -> None:
        assert is_value(value), f"generated a value the schema refuses: {value!r}"

    check()