Skip to content

Commit 8fa6d3d

Browse files
Revert "ref(flags): register LD hook in setup instead of init, and don't chec…" (#3900)
Mutating a class attribute on `__init__` violates encapsulation and will lead to strange errors. We need to rethink how we want to implement this before we merge any code. A simple reproduction of the issue: ```python >>> class X: ... y = 0 ... def __init__(self, z): ... self.__class__.y = z ... >>> a = X(1) >>> b = X(2) >>> X.y 2 >>> a.y 2 >>> b.y 2 ``` Reverts #3890 This reverts commit c3516db. Co-authored-by: Anton Pirker <[email protected]>
1 parent 7f73c9e commit 8fa6d3d

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

sentry_sdk/integrations/launchdarkly.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,27 @@
2020

2121
class LaunchDarklyIntegration(Integration):
2222
identifier = "launchdarkly"
23-
_ld_client = None # type: LDClient | None
2423

2524
def __init__(self, ld_client=None):
2625
# type: (LDClient | None) -> None
2726
"""
2827
:param client: An initialized LDClient instance. If a client is not provided, this
2928
integration will attempt to use the shared global instance.
3029
"""
31-
self.__class__._ld_client = ld_client
32-
33-
@staticmethod
34-
def setup_once():
35-
# type: () -> None
3630
try:
37-
client = LaunchDarklyIntegration._ld_client or ldclient.get()
31+
client = ld_client or ldclient.get()
3832
except Exception as exc:
3933
raise DidNotEnable("Error getting LaunchDarkly client. " + repr(exc))
4034

35+
if not client.is_initialized():
36+
raise DidNotEnable("LaunchDarkly client is not initialized.")
37+
4138
# Register the flag collection hook with the LD client.
4239
client.add_hook(LaunchDarklyHook())
4340

41+
@staticmethod
42+
def setup_once():
43+
# type: () -> None
4444
scope = sentry_sdk.get_current_scope()
4545
scope.add_error_processor(flag_error_processor)
4646

tests/integrations/launchdarkly/test_launchdarkly.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,21 +183,22 @@ async def runner():
183183
}
184184

185185

186-
def test_launchdarkly_integration_did_not_enable(sentry_init, uninstall_integration):
187-
"""
188-
Setup should fail when using global client and ldclient.set_config wasn't called.
189-
190-
We're accessing ldclient internals to set up this test, so it might break if launchdarkly's
191-
implementation changes.
192-
"""
193-
186+
def test_launchdarkly_integration_did_not_enable(monkeypatch):
187+
# Client is not passed in and set_config wasn't called.
188+
# TODO: Bad practice to access internals like this. We can skip this test, or remove this
189+
# case entirely (force user to pass in a client instance).
194190
ldclient._reset_client()
195191
try:
196192
ldclient.__lock.lock()
197193
ldclient.__config = None
198194
finally:
199195
ldclient.__lock.unlock()
200196

201-
uninstall_integration(LaunchDarklyIntegration.identifier)
202197
with pytest.raises(DidNotEnable):
203-
sentry_init(integrations=[LaunchDarklyIntegration()])
198+
LaunchDarklyIntegration()
199+
200+
# Client not initialized.
201+
client = LDClient(config=Config("sdk-key"))
202+
monkeypatch.setattr(client, "is_initialized", lambda: False)
203+
with pytest.raises(DidNotEnable):
204+
LaunchDarklyIntegration(ld_client=client)

0 commit comments

Comments
 (0)