ProvenPin Install

voluptuous config schemas with Coerce, All, Range and Required: working versions (tested 2026-10-16)

Validate and coerce stringly-typed config into a typed dict with voluptuous, combining Coerce, All, Range, In and Required, and testing the failure paths offline.

passed6 passed · 0 failed2026-10-16 09:58 UTC
Status
passed
Last passed
2026-10-16 09:58 UTC
Tests
6 / 6
Language
Python
Runtime
Python 3.13.16
pinned versions2
voluptuous@0.16.0pytest@9.1.1
run it
uv venv -q .venv && uv pip install -q --python .venv/bin/python -r requirements.txt
.venv/bin/python -m pytest -q
test environment
Python 3.13.16
linux 6.17.0-1022-azure
x64
runner: github-actions 38883899161

The code

Exactly the files that passed. Your agent can pull them with get_recipe("voluptuous-config-schema-coerce") (install), or see them on GitHub.

requirements.txt
voluptuous==0.16.0
pytest==9.1.1
config.py
from voluptuous import (
    Schema, Required, Optional, All, Coerce, Range, In, Length,
    MultipleInvalid, REMOVE_EXTRA,
)

def to_bool(value):
    if isinstance(value, bool):
        return value
    text = str(value).strip().lower()
    if text in ("1", "true", "yes", "on"):
        return True
    if text in ("0", "false", "no", "off"):
        return False
    raise ValueError(f"cannot coerce {value!r} to bool")

NonEmptyStr = All(Coerce(str), Length(min=1))
Port = All(Coerce(int), Range(min=1, max=65535))
LogLevel = All(Coerce(str), In(["debug", "info", "warning", "error"]))

CONFIG_SCHEMA = Schema(
    {
        Required("host"): NonEmptyStr,
        Required("workers"): All(Coerce(int), Range(min=1, max=64)),
        Optional("port", default="8080"): Port,
        Optional("debug", default=False): Coerce(to_bool),
        Optional("log_level", default="info"): LogLevel,
    },
    extra=REMOVE_EXTRA,
)

def load_config(data):
    return CONFIG_SCHEMA(data)
tests/test_config.py
import pytest
from voluptuous import All, Coerce, MultipleInvalid, Range, Schema
from config import load_config, to_bool

def test_coerces_env_style_strings():
    cfg = load_config({"host": "api.internal", "workers": "4", "port": "443"})
    assert cfg["workers"] == 4 and cfg["port"] == 443
    assert isinstance(cfg["port"], int)

def test_defaults_are_coerced_too():
    cfg = load_config({"host": "api.internal", "workers": 2})
    assert cfg["port"] == 8080 and isinstance(cfg["port"], int)
    assert cfg["debug"] is False and cfg["log_level"] == "info"

def test_all_runs_stages_in_order():
    schema = Schema({"port": All(Coerce(int), Range(min=1, max=65535))})
    assert schema({"port": "443"})["port"] == 443
    reversed_order = Schema({"port": All(Range(min=1), Coerce(int))})
    with pytest.raises(MultipleInvalid, match="partial ordering"):
        reversed_order({"port": "443"})

def test_naive_coerce_bool_is_a_trap():
    assert Coerce(bool)("false") is True
    assert to_bool("false") is False and to_bool("on") is True

def test_required_and_bounds_are_reported():
    with pytest.raises(MultipleInvalid) as err:
        load_config({"port": "70000"})
    paths = {tuple(e.path) for e in err.value.errors}
    assert ("host",) in paths and ("workers",) in paths and ("port",) in paths

def test_unknown_keys_are_removed():
    cfg = load_config({"host": "h", "workers": 1, "bogus": "x"})
    assert cfg == {"host": "h", "workers": 1, "port": 8080, "debug": False, "log_level": "info"}
pytest.ini
[pytest]
pythonpath = .

Connect to ProvenPin

Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.

terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp

Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.