From ed1adab02ff10fb2916d535202ba3af11940c5ef Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 1 Jul 2025 16:54:28 +0200 Subject: [PATCH] Revert generator logic and fix isolation scope creation The main yield has to be outside the `use_isolation_scope` blocks because otherwise stuff in the test logic will use the pytest Client and report errors and exceptions too causing a regression. --- pytest_sentry/helpers.py | 8 ++++---- pytest_sentry/hooks.py | 14 +++++++++++++- tests/test_scope.py | 5 ----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pytest_sentry/helpers.py b/pytest_sentry/helpers.py index a902b15..7454dda 100644 --- a/pytest_sentry/helpers.py +++ b/pytest_sentry/helpers.py @@ -41,25 +41,25 @@ def _resolve_scope_marker_value_uncached(marker_value): if isinstance(marker_value, str): # If a DSN string is provided, create a new client and use that - scope = sentry_sdk.get_isolation_scope() + scope = sentry_sdk.Scope(ty=ScopeType.ISOLATION) scope.set_client(Client(marker_value)) return scope if isinstance(marker_value, dict): # If a dict is provided, create a new client using the dict as Client options - scope = sentry_sdk.get_isolation_scope() + scope = sentry_sdk.Scope(ty=ScopeType.ISOLATION) scope.set_client(Client(**marker_value)) return scope if isinstance(marker_value, Client): # If a Client instance is provided, use that - scope = sentry_sdk.get_isolation_scope() + scope = sentry_sdk.Scope(ty=ScopeType.ISOLATION) scope.set_client(marker_value) return scope if isinstance(marker_value, sentry_sdk.Scope): # If a Scope instance is provided, use the client from it - scope = sentry_sdk.get_isolation_scope() + scope = sentry_sdk.Scope(ty=ScopeType.ISOLATION) scope.set_client(marker_value.client) return marker_value diff --git a/pytest_sentry/hooks.py b/pytest_sentry/hooks.py index 80926c2..484d392 100644 --- a/pytest_sentry/hooks.py +++ b/pytest_sentry/hooks.py @@ -36,7 +36,19 @@ def _with_isolation_scope(wrapped, instance, args, kwargs): else: with sentry_sdk.use_isolation_scope(isolation_scope): gen = wrapped(*args, **kwargs) - yield from gen + + while True: + try: + with sentry_sdk.use_isolation_scope(isolation_scope): + chunk = next(gen) + + y = yield chunk + + with sentry_sdk.use_isolation_scope(isolation_scope): + gen.send(y) + + except StopIteration: + break def inner(f): return pytest.hookimpl(hookwrapper=True, **kwargs)(_with_isolation_scope(f)) diff --git a/tests/test_scope.py b/tests/test_scope.py index e0dfbda..3e82dbd 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -25,11 +25,6 @@ def test_basic(): _assert_right_scopes() -def test_correct_span(): - # Ensure that we are within a root span (started by the pytest_runtest_call hook) - assert sentry_sdk.get_current_scope().span is not None - - class TestSimpleClass(object): def setup_method(self): _assert_right_scopes()