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
[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"]from app.flags import NEW_CHECKOUT, FlagSnapshot, Switch, pinned_flags
CHECKOUT = Switch(NEW_CHECKOUT, on=new, off=old).for_(pinned_flags())[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"]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))