Skip to content

Commit 9db1b7e

Browse files
authored
fix: change scoped export, add capturing param (#257)
* change export, add capturing param * capturing -> capture_exceptions
1 parent 01751d1 commit 9db1b7e

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 4.8.0 - 2025-06-10
2+
3+
- fix: export scoped, rather than tracked, decorator
4+
- feat: allow use of contexts without error tracking
5+
16
## 4.7.0 - 2025-06-10
27

38
- feat: add support for parse endpoint in responses API (no longer beta)

posthog/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
tag = tag
1616
get_tags = get_tags
1717
clear_tags = clear_tags
18-
tracked = scoped
18+
scoped = scoped
1919

2020
"""Settings."""
2121
api_key = None # type: Optional[str]

posthog/scopes.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,34 @@ def _get_current_context() -> Dict[str, Any]:
1212

1313

1414
@contextmanager
15-
def new_context(fresh=False):
15+
def new_context(fresh=False, capture_exceptions=True):
1616
"""
17-
Create a new context scope that will be active for the duration of the with block.
18-
Any tags set within this scope will be isolated to this context. Any exceptions raised
17+
Create a new context scope that will be active for the duration of the with block.
18+
Any tags set within this scope will be isolated to this context. Any exceptions raised
1919
or events captured within the context will be tagged with the context tags.
2020
21-
Args:
22-
fresh: Whether to start with a fresh context (default: False).
23-
If False, inherits tags from parent context.
24-
If True, starts with no tags.
25-
26-
Examples:
27-
# Inherit parent context tags
28-
with posthog.new_context():
29-
posthog.tag("request_id", "123")
30-
# Both this event and the exception will be tagged with the context tags
31-
posthog.capture("event_name", {"property": "value"})
32-
raise ValueError("Something went wrong")
33-
34-
# Start with fresh context (no inherited tags)
35-
with posthog.new_context(fresh=True):
36-
posthog.tag("request_id", "123")
37-
# Both this event and the exception will be tagged with the context tags
38-
posthog.capture("event_name", {"property": "value"})
39-
raise ValueError("Something went wrong")
21+
Args:
22+
fresh: Whether to start with a fresh context (default: False).
23+
If False, inherits tags from parent context.
24+
If True, starts with no tags.
25+
capture_exceptions: Whether to capture exceptions raised within the context (default: True).
26+
If True, captures exceptions and tags them with the context tags before propagating them.
27+
If False, exceptions will propagate without being tagged or captured.
28+
29+
Examples:
30+
# Inherit parent context tags
31+
with posthog.new_context():
32+
posthog.tag("request_id", "123")
33+
# Both this event and the exception will be tagged with the context tags
34+
posthog.capture("event_name", {"property": "value"})
35+
raise ValueError("Something went wrong")
36+
37+
# Start with fresh context (no inherited tags)
38+
with posthog.new_context(fresh=True):
39+
posthog.tag("request_id", "123")
40+
# Both this event and the exception will be tagged with the context tags
41+
posthog.capture("event_name", {"property": "value"})
42+
raise ValueError("Something went wrong")
4043
4144
"""
4245
from posthog import capture_exception
@@ -49,7 +52,8 @@ def new_context(fresh=False):
4952
try:
5053
yield
5154
except Exception as e:
52-
capture_exception(e)
55+
if capture_exceptions:
56+
capture_exception(e)
5357
raise
5458
finally:
5559
_context_stack.reset(token)
@@ -88,13 +92,14 @@ def clear_tags() -> None:
8892
F = TypeVar("F", bound=Callable[..., Any])
8993

9094

91-
def scoped(fresh=False):
95+
def scoped(fresh=False, capture_exceptions=True):
9296
"""
9397
Decorator that creates a new context for the function. Simply wraps
9498
the function in a with posthog.new_context(): block.
9599
96100
Args:
97101
fresh: Whether to start with a fresh context (default: False)
102+
capture_exceptions: Whether to capture and track exceptions with posthog error tracking (default: True)
98103
99104
Example:
100105
@posthog.scoped()
@@ -114,7 +119,7 @@ def decorator(func: F) -> F:
114119

115120
@wraps(func)
116121
def wrapper(*args, **kwargs):
117-
with new_context(fresh=fresh):
122+
with new_context(fresh=fresh, capture_exceptions=capture_exceptions):
118123
return func(*args, **kwargs)
119124

120125
return cast(F, wrapper)

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "4.7.0"
1+
VERSION = "4.8.0"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)