From fb4e688df9ac21f2343dc153bbf5351ad4520b76 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 5 Sep 2026 13:50:19 +0000 Subject: [PATCH] Remove PostHog context capture_exceptions workaround. posthog-python now inherits enable_exception_autocapture for new_context, so per-call capture_exceptions=False is no longer required. Keep assigning default_client so module-level APIs use the configured instance. Co-authored-by: Michael Bianco --- app/configuration/posthog.py | 4 ++-- app/routes/dependencies/posthog.py | 5 ++--- tests/unit/posthog_test.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/unit/posthog_test.py diff --git a/app/configuration/posthog.py b/app/configuration/posthog.py index 7990e723..170ce5cd 100644 --- a/app/configuration/posthog.py +++ b/app/configuration/posthog.py @@ -23,8 +23,8 @@ def posthog_error_handler(error, batch): if not is_production(): posthog_client.disabled = True -# https://github.com/PostHog/posthog-python/issues/353 -# without this, a newly-created client will be used for things like catching context exceptions +# Module-level APIs (`posthog.new_context`, `posthog.capture`, …) use this client. +# Without it, contexts fall back to a separately constructed default instance. posthog.default_client = posthog_client diff --git a/app/routes/dependencies/posthog.py b/app/routes/dependencies/posthog.py index 16438b50..c710c1f1 100644 --- a/app/routes/dependencies/posthog.py +++ b/app/routes/dependencies/posthog.py @@ -22,7 +22,7 @@ async def inject_posthog_identity(request: Request): > If two users have the same distinct ID, their data is merged and they are considered one user in PostHog. """ - with posthog.new_context(capture_exceptions=False): + with posthog.new_context(): # by using clerk_id, we can easily use the same UUID on frontend events posthog.identify_context(request.state.user.clerk_id) yield @@ -42,8 +42,7 @@ async def inject_posthog_tags(request: Request): """ # by default, there is not a context, so we must define one - # `capture_exceptions=False` https://github.com/PostHog/posthog-python/issues/353 - with posthog.new_context(capture_exceptions=False): + with posthog.new_context(): # intentionally overwrite any other session or distinct id set previously # it's up to the user to add dependencies in the correct order if distinct_id := request.headers.get("X-Posthog-Distinct-Id"): diff --git a/tests/unit/posthog_test.py b/tests/unit/posthog_test.py new file mode 100644 index 00000000..2b18f55e --- /dev/null +++ b/tests/unit/posthog_test.py @@ -0,0 +1,19 @@ +import posthog +import pytest + +from app.configuration.posthog import posthog_client + + +def test_default_client_is_the_configured_instance(): + assert posthog.default_client is posthog_client + assert posthog_client.enable_exception_autocapture is False + + +def test_new_context_does_not_capture_exceptions_when_autocapture_is_disabled(mocker): + capture = mocker.patch("posthog.capture_exception") + + with pytest.raises(RuntimeError, match="boom"): + with posthog.new_context(): + raise RuntimeError("boom") + + capture.assert_not_called()