Skip to content

fix: Respect exception autocapture default for contexts#680

Merged
marandaneto merged 3 commits into
mainfrom
fix/context-exception-defaults-353
Jun 19, 2026
Merged

fix: Respect exception autocapture default for contexts#680
marandaneto merged 3 commits into
mainfrom
fix/context-exception-defaults-353

Conversation

@marandaneto

@marandaneto marandaneto commented Jun 19, 2026

Copy link
Copy Markdown
Member

💡 Motivation and Context

Fixes #353.

Contexts previously captured exceptions by default even when exception autocapture was disabled globally or on a custom client. This made users opt out with capture_exceptions=False for every new context.

💚 How did you test it?

  • uv run --extra test pytest -q posthog/test/test_contexts.py posthog/test/test_exception_capture.py
  • uv run pytest -q posthog/test/test_contexts.py posthog/test/test_exception_capture.py
  • uv run --extra dev ruff format --check posthog/contexts.py posthog/client.py posthog/__init__.py posthog/test/test_contexts.py
  • uv run --extra dev ruff check posthog/contexts.py posthog/client.py posthog/__init__.py posthog/test/test_contexts.py
  • uv run --extra dev python .github/scripts/test_check_public_api.py
  • uv run --extra dev make public_api_check

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Implemented with a delegated Pi worker agent in a dedicated worktree. The change keeps explicit capture_exceptions=True and capture_exceptions=False behavior intact while making omitted values derive from the relevant global or client exception autocapture setting. A patch changeset was added manually for pypi/posthog, and the public API snapshot was updated for the intentional signature default/type change.

@marandaneto marandaneto self-assigned this Jun 19, 2026
@marandaneto marandaneto force-pushed the fix/context-exception-defaults-353 branch from a083097 to e0d83ca Compare June 19, 2026 10:35
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/test/test_contexts.py:163-238
**New tests should be parameterised**

The three new tests that manipulate module globals (`test_new_context_defaults_to_global_exception_autocapture_disabled`, `test_new_context_explicit_true_captures_when_global_autocapture_disabled`, `test_new_context_explicit_false_skips_capture_when_global_autocapture_enabled`) share an identical save/restore pattern and differ only in `posthog.enable_exception_autocapture`, the value passed to `capture_exceptions`, and whether `mock_capture` is expected to be called. Following the project's stated preference for parameterised tests, these three could be collapsed into a single `@pytest.mark.parametrize` (or `subTest`) loop covering `(global_setting, explicit_arg, expect_captured)` tuples, reducing boilerplate and making the intention of each case clearer.

Reviews (1): Last reviewed commit: "fix: Respect exception autocapture defau..." | Re-trigger Greptile

Comment thread posthog/test/test_contexts.py Outdated
@github-actions

github-actions Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-19 14:38:59 UTC
Duration: 540108ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10006ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10006ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18019ms
Retry Behavior.Does Not Retry On 400 12004ms
Retry Behavior.Does Not Retry On 401 10005ms
Retry Behavior.Respects Retry After Header 16015ms
Retry Behavior.Implements Backoff 32019ms
Retry Behavior.Retries On 500 16009ms
Retry Behavior.Retries On 502 16007ms
Retry Behavior.Retries On 504 16013ms
Retry Behavior.Max Retries Respected 32027ms
Deduplication.Generates Unique Uuids 9992ms
Deduplication.Preserves Uuid On Retry 16016ms
Deduplication.Preserves Uuid And Timestamp On Retry 23018ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16004ms
Deduplication.No Duplicate Events In Batch 10003ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10006ms
Batch Format.Flush With No Events Sends Nothing 5005ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12009ms
Error Handling.Does Not Retry On 413 10006ms
Error Handling.Retries On 408 14015ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9501ms
Request Payload.Flags Request Uses V2 Query Param 10006ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10006ms
Request Payload.Token In Flags Body Matches Init 10007ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10007ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10006ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10007ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9511ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10002ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10509ms

@marandaneto marandaneto requested review from a team, ablaszkiewicz, cat-ph and hpouillot June 19, 2026 12:52
@marandaneto

Copy link
Copy Markdown
Member Author

@PostHog/team-error-tracking can you check this? it makes sense to me but it might be a change in behavior if the global is disabled and relies on the current default value of capture_exceptions

@marandaneto marandaneto marked this pull request as ready for review June 19, 2026 12:53
@marandaneto marandaneto requested a review from a team as a code owner June 19, 2026 12:53
@greptile-apps

greptile-apps Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
posthog/test/test_contexts.py:167-171
**Missing `(True, None, True)` subTest case**

The test verifies that the global autocapture setting is respected, but omits the case where `global_setting=True` and `capture_exceptions` is not passed — the scenario that confirms the default inherits an *enabled* setting. With only the three current cases, a regression that always resolves to `False` would pass this test. Adding `(True, None, True)` closes that gap and satisfies the team's rule of expressing every required idea.

### Issue 2 of 2
posthog/test/test_contexts.py:207-224
**Single-sided custom-client test**

`test_new_context_defaults_to_custom_client_exception_autocapture_disabled` only tests `enable_exception_autocapture=False`. Following the same pattern applied to the global-setting test, this would benefit from also covering `enable_exception_autocapture=True` — confirming that a client context with autocapture enabled does capture the exception by default. The test could be turned into a short parameterised loop over `(False, False)` / `(True, True)` to express both sides OnceAndOnlyOnce.

Reviews (2): Last reviewed commit: "address pr review feedback" | Re-trigger Greptile

@ablaszkiewicz ablaszkiewicz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks for finding and fixing this. I just don't think this is a patch

@marandaneto

Copy link
Copy Markdown
Member Author

Looks good. Thanks for finding and fixing this. I just don't think this is a patch

its not a new feat either, imo its like fixing a bug, what do you suggest?

@marandaneto marandaneto enabled auto-merge (squash) June 19, 2026 14:29
@marandaneto marandaneto merged commit b7dff95 into main Jun 19, 2026
30 checks passed
@marandaneto marandaneto deleted the fix/context-exception-defaults-353 branch June 19, 2026 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

capture_exceptions=False by default on new contexts if posthog is not capturing exceptions

3 participants