Skip to content
Sarj Code Standards

no-retained-feature-flag

Resolve feature flags once per unit of work instead of retaining them in process-lifetime state

Why

A flag provider refreshes its definitions while the process runs, so a changed flag is expected to reach the next request, call or job. A value read at import or process start, memoised, or copied to a module global, class attribute, long-lived instance or application state is frozen until the next deploy: the rollout or kill switch appears to work in the provider and does nothing in production.

Fix

Resolve one snapshot where the unit of work starts (request, call, job) and pass the snapshot, or the behaviour it selects, down as an argument. Keep only the flag source and the unresolved selector in long-lived objects. A deliberate one-time read keeps # sarj-noqa: SARJ594 -- <reason>.

Examples

Before — flagged A module constant fixes the flag when the process starts
.sarj-standards.toml
[python.no-retained-feature-flag]
modules = ["app.flags"]
snapshot_types = ["FlagSnapshot"]
resolver_functions = ["pinned_flags"]
resolver_methods = ["evaluate"]
selector_methods = ["for_"]
startup_functions = ["create_app"]
unit_of_work_types = ["RequestFlags"]
app/checkout.py
from app.flags import NEW_CHECKOUT, FlagSnapshot, Switch, pinned_flags
CHECKOUT = Switch(NEW_CHECKOUT, on=new, off=old).for_(pinned_flags())
After — preferred The handler resolves a snapshot and the service receives what it selects
.sarj-standards.toml
[python.no-retained-feature-flag]
modules = ["app.flags"]
snapshot_types = ["FlagSnapshot"]
resolver_functions = ["pinned_flags"]
resolver_methods = ["evaluate"]
selector_methods = ["for_"]
startup_functions = ["create_app"]
unit_of_work_types = ["RequestFlags"]
app/checkout.py
from app.flags import NEW_CHECKOUT, FlagSnapshot, Switch, pinned_flags
CHECKOUT = Switch(NEW_CHECKOUT, on=new, off=old)
class CheckoutService:
def __init__(self, source: FlagSource) -> None:
self.source = source
async def pay(self, order: Order) -> Receipt:
flags = await self.source.evaluate(order.subject)
return await charge(order, CHECKOUT.for_(flags))