Skip to content

Commit f0d4a0d

Browse files
aapelivclaude
andcommitted
Backend: Add _growthbook attribute to CouchersContext
Declare _growthbook directly on CouchersContext so _get_growthbook no longer needs type: ignore comments. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 73648df commit f0d4a0d

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

app/backend/src/couchers/context.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from typing import NoReturn, cast
1+
from typing import TYPE_CHECKING, NoReturn, cast
22

33
import grpc
44

55
from couchers.i18n import LocalizationContext
66

7+
if TYPE_CHECKING:
8+
from growthbook import GrowthBook
9+
710

811
class NonInteractiveContextException(Exception):
912
"""If this exception is raised, it is a programming error"""
@@ -79,6 +82,7 @@ def __init__(
7982
self.__is_interactive = is_interactive
8083
self.__logged_in = self._user_id is not None
8184
self.__cookies: list[str] = []
85+
self._growthbook: GrowthBook | None = None
8286

8387
if self.__is_interactive:
8488
if not self._grpc_context:

app/backend/src/couchers/experimentation.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ def _get_growthbook(context: CouchersContext) -> GrowthBook:
153153
`client_key` keeps the GrowthBook a pure evaluator: no callback
154154
registration on the library's process-wide singleton.
155155
"""
156-
if not hasattr(context, "_growthbook"):
156+
gb = context._growthbook
157+
if gb is None:
157158
with _state_lock:
158159
features = _state["features"]
159160
saved_groups = _state["savedGroups"]
@@ -169,8 +170,8 @@ def on_experiment_viewed(experiment: Experiment, result: Result, **kwargs: Any)
169170
savedGroups=saved_groups,
170171
on_experiment_viewed=on_experiment_viewed,
171172
)
172-
context._growthbook = gb # type: ignore[attr-defined]
173-
return context._growthbook # type: ignore[attr-defined, no-any-return]
173+
context._growthbook = gb
174+
return gb
174175

175176

176177
def check_gate(context: CouchersContext, gate_name: str) -> bool:

0 commit comments

Comments
 (0)