mirror of
https://github.com/hanzoai/python-sdk.git
synced 2026-10-23 20:23:00 +00:00
Flags were natively supported in Rust (the hanzo-flags crate — the evaluation core, pub-exported for Rust callers and FFI-exported for the rest), Go (in-process via CGO to that core), and TypeScript (@hanzo/flags over /v1/flags). Python was the gap. This closes it. hanzo-flags POSTs an evaluation context to cloud /v1/flags — the same PostHog-compatible endpoint every other client speaks — and answers is_enabled / variant / payload from the response. It mirrors @hanzo/flags exactly: a HanzoFlags client bound to one host, load(distinct_id, person_properties=, groups=), the same three-field result, an async twin, and a one-shot evaluate(). Two invariants match the family: fail-open (a transport error yields the last good or empty result with errors_while_computing set, never a raise on the hot path) and cache-by-context+TTL. Zero runtime dependencies — stdlib urllib only, so a flag check never drags httpx/pydantic into a service. Tests stand up a local /v1/flags stub and prove the request shape is PostHog-compatible, the accessors resolve booleans/variants/payloads, the TTL cache holds, and both a 5xx and an unreachable host fail open. 7/7 green.
30 lines
850 B
Python
30 lines
850 B
Python
# Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
"""hanzo-flags — feature flags + A/B for the Hanzo native flags engine.
|
|
|
|
The Python client for cloud ``/v1/flags`` (the PostHog-compatible evaluation
|
|
endpoint the Rust core, Go, and @hanzo/flags all speak). Zero runtime deps.
|
|
|
|
from hanzo_flags import HanzoFlags
|
|
|
|
flags = HanzoFlags("https://api.hanzo.ai", token=tok)
|
|
flags.load("user-123", person_properties={"plan": "pro"})
|
|
if flags.is_enabled("checkout-exp"):
|
|
...
|
|
variant = flags.variant("pricing-test") # "control" | "b" | None
|
|
"""
|
|
|
|
from .async_client import AsyncHanzoFlags
|
|
from .client import HanzoFlags, evaluate
|
|
from .models import EvalContext, EvalResult, Group
|
|
|
|
__all__ = [
|
|
"HanzoFlags",
|
|
"AsyncHanzoFlags",
|
|
"evaluate",
|
|
"EvalContext",
|
|
"EvalResult",
|
|
"Group",
|
|
]
|
|
|
|
__version__ = "0.1.0"
|