Skip to main content

Verdict — Python

The Python implementation of Verdict — a small, zero-dependency, async-native rule-evaluation engine.

Install

pip install verdict-rules
from verdict import Rule, FunctionRule, AndRule, OrRule, NotRule, RuleResult, RulesEngine

A first rule

FunctionRule wraps a plain async predicate — a reusable one can build several named rules from the same code, each checking a different field against its own floor:

import asyncio
from verdict import AndRule, FunctionRule, PredicateOutcome, RulesEngine

def at_least(name: str, field: str, floor: float) -> FunctionRule:
    async def predicate(context: dict) -> PredicateOutcome:
        value = context[field]
        return PredicateOutcome(passed=value >= floor, detail=f"{value} vs {floor}")
    return FunctionRule(name, predicate)

async def main() -> None:
    eligible = AndRule("eligible", [
        at_least("age_ok", "age", 18),
        at_least("score_ok", "score", 60),
    ])
    engine = RulesEngine([eligible])
    verdict = await engine.run_named("eligible", {"age": 21, "score": 55})
    print(verdict.passed)                          # False
    failed = verdict.failing_leaves[0]
    print(failed.rule_name, "|", failed.detail)    # score_ok | 55 vs 60

asyncio.run(main())

A predicate reports a PredicateOutcome; the FunctionRule wrapping it owns the name and builds the RuleResult. A composite's own detail is empty — read failing_leaves for which leaf check refused, and key an audit trail on that leaf's rule_name rather than the composite's.

If it has the shape, it is a rule

Rule is a Protocol, and Python's typing is structural — so any object with the right attributes and an evaluate coroutine already is a Rule. No inheritance, no registration:

class IsBusinessHours:
    name = "is_business_hours"
    group = None

    async def evaluate(self, context: dict) -> RuleResult:
        hour = context["hour"]
        return RuleResult(rule_name="is_business_hours", passed=9 <= hour < 17)

await AndRule("open", [IsBusinessHours()]).evaluate({"hour": 21})

Most rules need no class at all either: FunctionRule wraps a plain async function.

What it guarantees

  • Sequential evaluation, never concurrent. Composites use a plain for loop with await, never asyncio.gather. Short-circuiting only means something if later work never starts — and because the returned boolean is identical either way, getting this wrong is silent.
  • Vacuous truth has a polarity. AndRule([]) passes, OrRule([]) fails. Deliberately asymmetric.
  • Emptiness is not absence. An empty composite folds to its identity; an unknown rule name or group raises KeyError. A group exists only because some rule declared it, so a lookup matching nothing can only be a mistake — and a misspelled group silently approving is the worst failure an eligibility check can have. Use rule_names / group_names to check membership, or try_run_named / try_run_group where your own domain has an answer for absence — both return None instead of raising.
  • RuleResult.data is opaque — never read or written by this package; it carries whatever a predicate attached, unchanged.
  • A composite's children live in sub_results, and are exactly what it evaluated: never padded to the full sub-rule list, never flattened into the parent. leaves/failing_leaves walk that tree for you, and decided_by names which sub-results explain a result's own verdict.
  • Zero runtime dependencies.

Where to go next

Doc For
docs/quickstart.md The quickstart — core concepts and a full worked example
docs/architecture/ Why it's shaped this way, in depth — type structure, the execution model
docs/extending/ Building on top of it from your own code, with no changes here
docs/maintenance/ Changing this package itself
docs/testing/ How the test suite is organized, and what a change needs to prove
fixtures/ The two worked scenarios — each one's problem, design, and the cross-language data contract every port reproduces
examples/ Full, tested mini-projects behind the fixtures — real code, real tests, real docs

Metadata

Release files for verdict-rules 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for verdict-rules 0.4.0
File Size Uploaded Provenance Actions
verdict_rules-0.4.0.tar.gz 14.2 kB With provenance Details for verdict_rules-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for verdict-rules 0.4.0
File Interpreter ABI Platform Actions
verdict_rules-0.4.0-py3-none-any.whl Python 3 none any Details for verdict_rules-0.4.0-py3-none-any.whl

Total release size: 29.8 kB

Release files / verdict_rules-0.4.0.tar.gz

Download URL verdict_rules-0.4.0.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3abe3a0f2022067fda9f63a6aabc601dfcec7ff27cc56f40bb291f044251ccb7
BLAKE2b-256 checksum
How to use checksums
29301a640db0ea478a4c251d577941c2c3d851560695fa57db866f167740e31d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / verdict_rules-0.4.0-py3-none-any.whl

Download URL verdict_rules-0.4.0-py3-none-any.whl
Size 15.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
304249cb8cbd1f66b19fa0b71fcec82798404499390237bb732344e5515f9053
BLAKE2b-256 checksum
How to use checksums
266585084a5525400eddde9956fd859a0178937eb4071ed2276f4d341fb45f5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Security for verdict-rules 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for verdict_rules-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page