From bb31f9b10e3fdf7fb8bacde0064b02358477dc07 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:54:35 +0000 Subject: [PATCH] ci(ruff): Enable PT012 for pytest.raises() blocks with several statements Existing cases get a noqa comment, so only new tests are checked. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com> --- pyproject.toml | 1 + tests/integrations/anthropic/test_anthropic.py | 18 +++++++++--------- tests/integrations/asgi/test_asgi.py | 12 ++++++------ tests/integrations/bottle/test_bottle.py | 2 +- tests/integrations/django/test_basic.py | 2 +- tests/integrations/flask/test_flask.py | 4 ++-- tests/integrations/langgraph/test_langgraph.py | 6 +++--- .../openai_agents/test_openai_agents.py | 6 +++--- tests/integrations/ray/test_ray.py | 8 ++++---- .../integrations/sqlalchemy/test_sqlalchemy.py | 8 ++++---- tests/integrations/stdlib/test_httplib.py | 4 ++-- tests/test_utils.py | 4 ++-- tests/tracing/test_integration_tests.py | 4 ++-- 13 files changed, 40 insertions(+), 39 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index cfac620006..b5930611d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -301,6 +301,7 @@ select = [ "I", # Note: B and N rules are NOT enabled by default in flake8 # They were only active through the plugins, which may not have been fully enabled + "PT012", # pytest.raises() block with more than one statement ] # Use ONLY the same ignores as the original flake8 config + compatibility for this codebase diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 2e6fe7c0dc..58ef6fb0fa 100644 --- a/tests/integrations/anthropic/test_anthropic.py +++ b/tests/integrations/anthropic/test_anthropic.py @@ -2074,7 +2074,7 @@ def test_streaming_create_message_api_error( if span_streaming: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -2121,7 +2121,7 @@ def test_streaming_create_message_api_error( elif stream_gen_ai_spans: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -2169,7 +2169,7 @@ def test_streaming_create_message_api_error( else: events = capture_events() - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -3617,7 +3617,7 @@ async def test_streaming_create_message_async_api_error( if span_streaming: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -3664,7 +3664,7 @@ async def test_streaming_create_message_async_api_error( elif stream_gen_ai_spans: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -3713,7 +3713,7 @@ async def test_streaming_create_message_async_api_error( else: events = capture_events() - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -4206,7 +4206,7 @@ async def test_stream_messages_async_api_error( if span_streaming: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -4253,7 +4253,7 @@ async def test_stream_messages_async_api_error( elif stream_gen_ai_spans: items = capture_items("transaction", "span") - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, @@ -4299,7 +4299,7 @@ async def test_stream_messages_async_api_error( else: events = capture_events() - with pytest.raises(APIStatusError), mock.patch.object( + with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012 client._client, "send", return_value=response, diff --git a/tests/integrations/asgi/test_asgi.py b/tests/integrations/asgi/test_asgi.py index cae3a24cb4..8458969871 100644 --- a/tests/integrations/asgi/test_asgi.py +++ b/tests/integrations/asgi/test_asgi.py @@ -357,7 +357,7 @@ async def test_has_trace_if_performance_enabled( ) app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg) - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 async with TestClient(app) as client: if span_streaming: items = capture_items("event", "span") @@ -418,7 +418,7 @@ async def test_has_trace_if_performance_disabled( sentry_init() app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg) - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 async with TestClient(app) as client: events = capture_events() await client.get("/") @@ -453,7 +453,7 @@ async def test_trace_from_headers_if_performance_enabled( trace_id = "582b43a4192642f0b136d5159a501701" sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1) - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 async with TestClient(app) as client: if span_streaming: items = capture_items("event", "span") @@ -513,7 +513,7 @@ async def test_trace_from_headers_if_performance_disabled( trace_id = "582b43a4192642f0b136d5159a501701" sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1) - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 async with TestClient(app) as client: events = capture_events() await client.get("/", headers={"sentry-trace": sentry_trace_header}) @@ -552,7 +552,7 @@ async def test_websocket( request_url = "/ws" - with pytest.raises(ValueError): + with pytest.raises(ValueError): # noqa: PT012 client = TestClient(asgi3_ws_app) if span_streaming: items = capture_items("event", "span") @@ -609,7 +609,7 @@ async def test_auto_session_tracking_with_aggregates( "endpoint": asgi3_app, "client": ("127.0.0.1", 60457), } - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 envelopes = capture_envelopes() async with TestClient(app, scope=scope) as client: scope["route"] = "/some/fine/url" diff --git a/tests/integrations/bottle/test_bottle.py b/tests/integrations/bottle/test_bottle.py index 7f8ad7fa3f..ce1413f003 100644 --- a/tests/integrations/bottle/test_bottle.py +++ b/tests/integrations/bottle/test_bottle.py @@ -396,7 +396,7 @@ def index(): client = get_client() - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 try: client.get("/") except ZeroDivisionError as e: diff --git a/tests/integrations/django/test_basic.py b/tests/integrations/django/test_basic.py index 59afe9be6b..8d7a10cc6a 100644 --- a/tests/integrations/django/test_basic.py +++ b/tests/integrations/django/test_basic.py @@ -796,7 +796,7 @@ def test_sql_psycopg2_placeholders( items = capture_items("event") sentry_sdk.get_isolation_scope().clear_breadcrumbs() - with pytest.raises(DataError): + with pytest.raises(DataError): # noqa: PT012 names = ["foo", "bar"] identifiers = [psycopg2.sql.Identifier(name) for name in names] placeholders = [ diff --git a/tests/integrations/flask/test_flask.py b/tests/integrations/flask/test_flask.py index 498f74d82e..ce2eb4b1e1 100644 --- a/tests/integrations/flask/test_flask.py +++ b/tests/integrations/flask/test_flask.py @@ -917,7 +917,7 @@ def test_tracing_error(sentry_init, capture_events, capture_items, app, span_str def error(): 1 / 0 - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 with app.test_client() as client: response = client.get("/error") assert response.status_code == 500 @@ -960,7 +960,7 @@ def test_error_has_trace_context_if_tracing_disabled(sentry_init, capture_events def error(): 1 / 0 - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 with app.test_client() as client: response = client.get("/error") assert response.status_code == 500 diff --git a/tests/integrations/langgraph/test_langgraph.py b/tests/integrations/langgraph/test_langgraph.py index 7b6a6cc3d9..c5246b8faa 100644 --- a/tests/integrations/langgraph/test_langgraph.py +++ b/tests/integrations/langgraph/test_langgraph.py @@ -614,7 +614,7 @@ def original_invoke(self, *args, **kwargs): if span_streaming or stream_gen_ai_spans: items = capture_items("span") - with start_transaction(), pytest.raises( + with start_transaction(), pytest.raises( # noqa: PT012 Exception, match="Graph execution failed" ): wrapped_invoke = _wrap_pregel_invoke(original_invoke) @@ -634,7 +634,7 @@ def original_invoke(self, *args, **kwargs): else: events = capture_events() - with start_transaction(), pytest.raises( + with start_transaction(), pytest.raises( # noqa: PT012 Exception, match="Graph execution failed" ): wrapped_invoke = _wrap_pregel_invoke(original_invoke) @@ -676,7 +676,7 @@ async def original_ainvoke(self, *args, **kwargs): raise Exception("Async graph execution failed") async def run_error_test(): - with start_transaction(), pytest.raises( + with start_transaction(), pytest.raises( # noqa: PT012 Exception, match="Async graph execution failed" ): wrapped_ainvoke = _wrap_pregel_ainvoke(original_ainvoke) diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py index b36436592b..c30d909fc4 100644 --- a/tests/integrations/openai_agents/test_openai_agents.py +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -4994,7 +4994,7 @@ async def test_run_streamed_error_handling( items = capture_items("event", "span") - with pytest.raises(Exception, match="Model Error"): + with pytest.raises(Exception, match="Model Error"): # noqa: PT012 result = agents.Runner.run_streamed( test_agent, "Test input", run_config=test_run_config ) @@ -5034,7 +5034,7 @@ async def test_run_streamed_error_handling( items = capture_items("event", "span", "transaction") - with pytest.raises(Exception, match="Model Error"): + with pytest.raises(Exception, match="Model Error"): # noqa: PT012 result = agents.Runner.run_streamed( test_agent, "Test input", run_config=test_run_config ) @@ -5074,7 +5074,7 @@ async def test_run_streamed_error_handling( ) events = capture_events() - with pytest.raises(Exception, match="Model Error"): + with pytest.raises(Exception, match="Model Error"): # noqa: PT012 result = agents.Runner.run_streamed( test_agent, "Test input", run_config=test_run_config ) diff --git a/tests/integrations/ray/test_ray.py b/tests/integrations/ray/test_ray.py index aa8b00dc00..70cc82489a 100644 --- a/tests/integrations/ray/test_ray.py +++ b/tests/integrations/ray/test_ray.py @@ -340,12 +340,12 @@ def example_task(): with sentry_sdk.traces.start_span( name="ray test parent", attributes={"sentry.op": "task"} ): - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 future = example_task.remote() ray.get(future) else: with sentry_sdk.start_transaction(op="task", name="ray test transaction"): - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 future = example_task.remote() ray.get(future) @@ -530,13 +530,13 @@ def increment(self): with sentry_sdk.traces.start_span( name="ray test parent", attributes={"sentry.op": "task"} ): - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 counter = Counter.remote() future = counter.increment.remote() ray.get(future) else: with sentry_sdk.start_transaction(op="task", name="ray test transaction"): - with pytest.raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): # noqa: PT012 counter = Counter.remote() future = counter.increment.remote() ray.get(future) diff --git a/tests/integrations/sqlalchemy/test_sqlalchemy.py b/tests/integrations/sqlalchemy/test_sqlalchemy.py index e08bb0a423..4807b66818 100644 --- a/tests/integrations/sqlalchemy/test_sqlalchemy.py +++ b/tests/integrations/sqlalchemy/test_sqlalchemy.py @@ -139,7 +139,7 @@ class Address(Base): session.query(Person).first() for _ in range(2): - with pytest.raises(IntegrityError): + with pytest.raises(IntegrityError): # noqa: PT012 with session.begin_nested(): session.add(Person(id=1, name="bob")) session.add(Person(id=1, name="bob")) @@ -185,7 +185,7 @@ class Address(Base): session.query(Person).first() for _ in range(2): - with pytest.raises(IntegrityError): + with pytest.raises(IntegrityError): # noqa: PT012 with session.begin_nested(): session.add(Person(id=1, name="bob")) session.add(Person(id=1, name="bob")) @@ -270,7 +270,7 @@ class Address(Base): session.query(Person).first() for _ in range(2): - with pytest.raises(IntegrityError): + with pytest.raises(IntegrityError): # noqa: PT012 with session.begin_nested(): session.add(Person(id=1, name="bob")) session.add(Person(id=1, name="bob")) @@ -297,7 +297,7 @@ class Address(Base): session.query(Person).first() for _ in range(2): - with pytest.raises(IntegrityError): + with pytest.raises(IntegrityError): # noqa: PT012 with session.begin_nested(): session.add(Person(id=1, name="bob")) session.add(Person(id=1, name="bob")) diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index 068714d4dc..cdceca7d03 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -1324,7 +1324,7 @@ def test_http_timeout( if span_streaming: items = capture_items("span") - with pytest.raises(TimeoutError): + with pytest.raises(TimeoutError): # noqa: PT012 with sentry_sdk.traces.start_span( name="name", attributes={ @@ -1344,7 +1344,7 @@ def test_http_timeout( else: envelopes = capture_envelopes() - with pytest.raises(TimeoutError): + with pytest.raises(TimeoutError): # noqa: PT012 with start_transaction(op="op", name="name"): conn = HTTPConnection("localhost", port=PORT) conn.request("GET", "/bla") diff --git a/tests/test_utils.py b/tests/test_utils.py index 0bea9a6705..35ece0f904 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -913,12 +913,12 @@ def test_is_sentry_url_no_client(): ], ) def test_get_error_message(error, expected_result): - with pytest.raises(BaseException) as exc_value: + with pytest.raises(BaseException) as exc_value: # noqa: PT012 exc_value.message = error raise Exception assert get_error_message(exc_value) == expected_result(exc_value) - with pytest.raises(BaseException) as exc_value: + with pytest.raises(BaseException) as exc_value: # noqa: PT012 exc_value.detail = error raise Exception assert get_error_message(exc_value) == expected_result(exc_value) diff --git a/tests/tracing/test_integration_tests.py b/tests/tracing/test_integration_tests.py index 3a3447a69f..8467cf7c80 100644 --- a/tests/tracing/test_integration_tests.py +++ b/tests/tracing/test_integration_tests.py @@ -721,7 +721,7 @@ def test_good_sysexit_doesnt_fail_transaction( with start_transaction(name="hi") as transaction: transaction.set_status(SPANSTATUS.OK) - with pytest.raises(SystemExit): + with pytest.raises(SystemExit): # noqa: PT012 with start_span(op="foo", name="foodesc"): if exception_value is not False: sys.exit(exception_value) @@ -746,7 +746,7 @@ def test_good_sysexit_doesnt_fail_segment_span_streaming( items = capture_items() with sentry_sdk.traces.start_span(name="hi"): - with pytest.raises(SystemExit): + with pytest.raises(SystemExit): # noqa: PT012 with sentry_sdk.traces.start_span(name="foo"): if exception_value is not False: sys.exit(exception_value)