Repository navigation
feat: user-scoped GrowthBookClient instances via create_scoped_instance - #141
vazarkevych wants to merge 4 commits into
Conversation
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge; no outstanding correctness, security, or repository-rule violations were identified. Reviews (4) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile |
6765784 to
576bf29
Compare
|
Maintainer decision needed: confirm the create_scoped_instance API and ownership rules. The handle binds UserContext by reference, its setters mutate that context, and a user callback overrides the client callback while None falls back to it. The README also limits scoped forced features to remote evaluation. These choices affect request isolation and tracking expectations; please record the accepted contract so implementation review can proceed. |
|
Recording the contract:
None of this is load-bearing — say the word on any of the four and I'll change |
Every evaluation on GrowthBookClient takes an explicit UserContext, so request-scoped code has to thread the same context through every layer and a mismatched one is a silent correctness bug. Adds GrowthBookClient.create_scoped_instance(user_context), returning a lightweight per-user UserScopedGrowthBook handle whose evaluation methods take no context: eval_feature, is_on, is_off, get_feature_value, run, log_event and preload_remote_eval. Calls delegate back to the shared client, so feature refresh, subscriptions, the feature-usage callback and the remote-eval cache are reused rather than forked. The handle owns per-user state on its bound context: set_attributes, update_attributes, set_url, set_forced_variations, set_forced_features, and per-user set_tracking_callback / set_feature_usage_callback that override the client-level Options.on_experiment_viewed / on_feature_usage for that user, falling back to them when unset. UserContext gains tracking_callback and feature_usage_callback for that, and _track / _feature_usage prefer them. Evaluation wires each callback when either a client-level or a per-user one exists — gating on the client-level option alone would mean a client without it never calls through, and the per-user callback would silently never fire. Both gates read the context with getattr: callers may pass a duck-typed context, and a gate must not be what breaks an evaluation. The context is bound by reference, not copied, so the setters write back to the caller's object and it stays usable with APIs that still take an explicit context. One handle per user: the bound context is mutable and also accumulates sticky bucket assignments. All changes are additive and default to existing behavior.
576bf29 to
e452edf
Compare
|
Rebased onto current main and resolved the test-file conflict (both sides had Also added One thing worth a look: evaluation wires these callbacks only when a consumer 1788 tests pass; mypy, pyright and the typing suite clean |
…ances-merge-main # Conflicts: # growthbook/common_types.py
|
@greptile review |
remote_group_ids shipped in 4.1.0 as UserContext's ninth field. The new tracking_callback / feature_usage_callback fields landed before it in the merge with main, so a ninth positional argument became tracking_callback and group targeting was lost. Append the callback fields after it. _track read user_context.tracking_callback directly, so a custom context without the field raised AttributeError before the callback's try block and broke the evaluation whenever Options.on_experiment_viewed was set. Use getattr like the other callback paths.
|
@greptile review |
|
@greptile review |
What and why
Every evaluation on
GrowthBookClienttakes an explicitUserContext(is_on(key, ctx),get_feature_value(key, fallback, ctx),run(experiment, ctx), …). In request-scoped backends the same context has to be threaded through every layer, and passing the wrong one is a silent bug: the wrong user gets the wrong variation.What changed
GrowthBookClient.create_scoped_instance(user_context=None)returns a per-userUserScopedGrowthBookhandle whose methods take no context:Evaluation:
eval_feature,is_on,is_off,get_feature_value,run(each with optionaltracking_buffer=),log_event,preload_remote_eval. All delegate to the shared client, so feature refresh, subscriptions and the remote-eval cache are reused. The handle holds no evaluation state and is cheap to create per request.Per-user state (writes to the bound context):
set_attributes,update_attributes(shallow merge,Noneis a no-op),set_url,set_forced_variations,set_forced_features,get_attributes()and theuser_contextproperty.Per-user callbacks:
set_tracking_callback/set_feature_usage_callbackoverrideOptions.on_experiment_viewed/Options.on_feature_usagefor this user and fall back to them whenNone. Backed by new optionalUserContext.tracking_callback/feature_usage_callbackfields, appended afterremote_group_idsso positional construction is unchanged. Read viagetattr, so custom contexts without them still work.UserScopedGrowthBookis exported fromgrowthbook. README section added.Decisions worth reviewing
user_contextcan be handed to plugins. A test pins this.set_forced_features()only applies in remote-eval mode. That is existing SDK behavior (local evaluation never readsforced_features). The docstring and README say so and point toset_forced_variations()instead.How to test
pytest tests/test_growthbook_client.py -k "scoped or per_user or predating or positional"14 new tests: evaluating without a context (matches the client with an explicit context), independent handles, every setter, binding by reference, per-user tracking and feature-usage callbacks (override, fallback, async),
tracking_buffer,log_eventandpreload_remote_evaldelegation, legacy contexts, and positionalUserContextorder.Full suite with current
mainmerged: 1955 passed. The 2cachefixture errors intest_growthbook_client.pyalso happen onmain.Size
Production code +221 / −10, tests +362, README +71.
Migration
None. All changes are additive.