Skip to content

Commit 134ce8f

Browse files
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>
1 parent 7fe277b commit 134ce8f

13 files changed

Lines changed: 40 additions & 39 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ select = [
305305
"T20", # print() and pprint() calls
306306
"PLC0415", # Import not at the top level of the file
307307
"PT011", # pytest.raises() too broad
308+
"PT012", # pytest.raises() block with more than one statement
308309
]
309310

310311
# Use ONLY the same ignores as the original flake8 config + compatibility for this codebase

‎tests/integrations/anthropic/test_anthropic.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,7 +2074,7 @@ def test_streaming_create_message_api_error(
20742074
if span_streaming:
20752075
items = capture_items("transaction", "span")
20762076

2077-
with pytest.raises(APIStatusError), mock.patch.object(
2077+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
20782078
client._client,
20792079
"send",
20802080
return_value=response,
@@ -2121,7 +2121,7 @@ def test_streaming_create_message_api_error(
21212121
elif stream_gen_ai_spans:
21222122
items = capture_items("transaction", "span")
21232123

2124-
with pytest.raises(APIStatusError), mock.patch.object(
2124+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
21252125
client._client,
21262126
"send",
21272127
return_value=response,
@@ -2169,7 +2169,7 @@ def test_streaming_create_message_api_error(
21692169
else:
21702170
events = capture_events()
21712171

2172-
with pytest.raises(APIStatusError), mock.patch.object(
2172+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
21732173
client._client,
21742174
"send",
21752175
return_value=response,
@@ -3617,7 +3617,7 @@ async def test_streaming_create_message_async_api_error(
36173617
if span_streaming:
36183618
items = capture_items("transaction", "span")
36193619

3620-
with pytest.raises(APIStatusError), mock.patch.object(
3620+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
36213621
client._client,
36223622
"send",
36233623
return_value=response,
@@ -3664,7 +3664,7 @@ async def test_streaming_create_message_async_api_error(
36643664
elif stream_gen_ai_spans:
36653665
items = capture_items("transaction", "span")
36663666

3667-
with pytest.raises(APIStatusError), mock.patch.object(
3667+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
36683668
client._client,
36693669
"send",
36703670
return_value=response,
@@ -3713,7 +3713,7 @@ async def test_streaming_create_message_async_api_error(
37133713
else:
37143714
events = capture_events()
37153715

3716-
with pytest.raises(APIStatusError), mock.patch.object(
3716+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
37173717
client._client,
37183718
"send",
37193719
return_value=response,
@@ -4206,7 +4206,7 @@ async def test_stream_messages_async_api_error(
42064206
if span_streaming:
42074207
items = capture_items("transaction", "span")
42084208

4209-
with pytest.raises(APIStatusError), mock.patch.object(
4209+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
42104210
client._client,
42114211
"send",
42124212
return_value=response,
@@ -4253,7 +4253,7 @@ async def test_stream_messages_async_api_error(
42534253
elif stream_gen_ai_spans:
42544254
items = capture_items("transaction", "span")
42554255

4256-
with pytest.raises(APIStatusError), mock.patch.object(
4256+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
42574257
client._client,
42584258
"send",
42594259
return_value=response,
@@ -4299,7 +4299,7 @@ async def test_stream_messages_async_api_error(
42994299
else:
43004300
events = capture_events()
43014301

4302-
with pytest.raises(APIStatusError), mock.patch.object(
4302+
with pytest.raises(APIStatusError), mock.patch.object( # noqa: PT012
43034303
client._client,
43044304
"send",
43054305
return_value=response,

‎tests/integrations/asgi/test_asgi.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ async def test_has_trace_if_performance_enabled(
357357
)
358358
app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg)
359359

360-
with pytest.raises(ZeroDivisionError):
360+
with pytest.raises(ZeroDivisionError): # noqa: PT012
361361
async with TestClient(app) as client:
362362
if span_streaming:
363363
items = capture_items("event", "span")
@@ -418,7 +418,7 @@ async def test_has_trace_if_performance_disabled(
418418
sentry_init()
419419
app = SentryAsgiMiddleware(asgi3_app_with_error_and_msg)
420420

421-
with pytest.raises(ZeroDivisionError):
421+
with pytest.raises(ZeroDivisionError): # noqa: PT012
422422
async with TestClient(app) as client:
423423
events = capture_events()
424424
await client.get("/")
@@ -453,7 +453,7 @@ async def test_trace_from_headers_if_performance_enabled(
453453
trace_id = "582b43a4192642f0b136d5159a501701"
454454
sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1)
455455

456-
with pytest.raises(ZeroDivisionError):
456+
with pytest.raises(ZeroDivisionError): # noqa: PT012
457457
async with TestClient(app) as client:
458458
if span_streaming:
459459
items = capture_items("event", "span")
@@ -513,7 +513,7 @@ async def test_trace_from_headers_if_performance_disabled(
513513
trace_id = "582b43a4192642f0b136d5159a501701"
514514
sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1)
515515

516-
with pytest.raises(ZeroDivisionError):
516+
with pytest.raises(ZeroDivisionError): # noqa: PT012
517517
async with TestClient(app) as client:
518518
events = capture_events()
519519
await client.get("/", headers={"sentry-trace": sentry_trace_header})
@@ -552,7 +552,7 @@ async def test_websocket(
552552

553553
request_url = "/ws"
554554

555-
with pytest.raises(ValueError): # noqa: PT011
555+
with pytest.raises(ValueError): # noqa: PT011, PT012
556556
client = TestClient(asgi3_ws_app)
557557
if span_streaming:
558558
items = capture_items("event", "span")
@@ -609,7 +609,7 @@ async def test_auto_session_tracking_with_aggregates(
609609
"endpoint": asgi3_app,
610610
"client": ("127.0.0.1", 60457),
611611
}
612-
with pytest.raises(ZeroDivisionError):
612+
with pytest.raises(ZeroDivisionError): # noqa: PT012
613613
envelopes = capture_envelopes()
614614
async with TestClient(app, scope=scope) as client:
615615
scope["route"] = "/some/fine/url"

‎tests/integrations/bottle/test_bottle.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ def index():
396396

397397
client = get_client()
398398

399-
with pytest.raises(ZeroDivisionError):
399+
with pytest.raises(ZeroDivisionError): # noqa: PT012
400400
try:
401401
client.get("/")
402402
except ZeroDivisionError as e:

‎tests/integrations/django/test_basic.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def test_sql_psycopg2_placeholders(
796796
items = capture_items("event")
797797
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
798798

799-
with pytest.raises(DataError):
799+
with pytest.raises(DataError): # noqa: PT012
800800
names = ["foo", "bar"]
801801
identifiers = [psycopg2.sql.Identifier(name) for name in names]
802802
placeholders = [

‎tests/integrations/flask/test_flask.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def test_tracing_error(sentry_init, capture_events, capture_items, app, span_str
917917
def error():
918918
1 / 0
919919

920-
with pytest.raises(ZeroDivisionError):
920+
with pytest.raises(ZeroDivisionError): # noqa: PT012
921921
with app.test_client() as client:
922922
response = client.get("/error")
923923
assert response.status_code == 500
@@ -960,7 +960,7 @@ def test_error_has_trace_context_if_tracing_disabled(sentry_init, capture_events
960960
def error():
961961
1 / 0
962962

963-
with pytest.raises(ZeroDivisionError):
963+
with pytest.raises(ZeroDivisionError): # noqa: PT012
964964
with app.test_client() as client:
965965
response = client.get("/error")
966966
assert response.status_code == 500

‎tests/integrations/langgraph/test_langgraph.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def original_invoke(self, *args, **kwargs):
614614
if span_streaming or stream_gen_ai_spans:
615615
items = capture_items("span")
616616

617-
with start_transaction(), pytest.raises(
617+
with start_transaction(), pytest.raises( # noqa: PT012
618618
Exception, match="Graph execution failed"
619619
):
620620
wrapped_invoke = _wrap_pregel_invoke(original_invoke)
@@ -634,7 +634,7 @@ def original_invoke(self, *args, **kwargs):
634634
else:
635635
events = capture_events()
636636

637-
with start_transaction(), pytest.raises(
637+
with start_transaction(), pytest.raises( # noqa: PT012
638638
Exception, match="Graph execution failed"
639639
):
640640
wrapped_invoke = _wrap_pregel_invoke(original_invoke)
@@ -676,7 +676,7 @@ async def original_ainvoke(self, *args, **kwargs):
676676
raise Exception("Async graph execution failed")
677677

678678
async def run_error_test():
679-
with start_transaction(), pytest.raises(
679+
with start_transaction(), pytest.raises( # noqa: PT012
680680
Exception, match="Async graph execution failed"
681681
):
682682
wrapped_ainvoke = _wrap_pregel_ainvoke(original_ainvoke)

‎tests/integrations/openai_agents/test_openai_agents.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4994,7 +4994,7 @@ async def test_run_streamed_error_handling(
49944994

49954995
items = capture_items("event", "span")
49964996

4997-
with pytest.raises(Exception, match="Model Error"):
4997+
with pytest.raises(Exception, match="Model Error"): # noqa: PT012
49984998
result = agents.Runner.run_streamed(
49994999
test_agent, "Test input", run_config=test_run_config
50005000
)
@@ -5034,7 +5034,7 @@ async def test_run_streamed_error_handling(
50345034

50355035
items = capture_items("event", "span", "transaction")
50365036

5037-
with pytest.raises(Exception, match="Model Error"):
5037+
with pytest.raises(Exception, match="Model Error"): # noqa: PT012
50385038
result = agents.Runner.run_streamed(
50395039
test_agent, "Test input", run_config=test_run_config
50405040
)
@@ -5074,7 +5074,7 @@ async def test_run_streamed_error_handling(
50745074
)
50755075
events = capture_events()
50765076

5077-
with pytest.raises(Exception, match="Model Error"):
5077+
with pytest.raises(Exception, match="Model Error"): # noqa: PT012
50785078
result = agents.Runner.run_streamed(
50795079
test_agent, "Test input", run_config=test_run_config
50805080
)

‎tests/integrations/ray/test_ray.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,12 @@ def example_task():
340340
with sentry_sdk.traces.start_span(
341341
name="ray test parent", attributes={"sentry.op": "task"}
342342
):
343-
with pytest.raises(ZeroDivisionError):
343+
with pytest.raises(ZeroDivisionError): # noqa: PT012
344344
future = example_task.remote()
345345
ray.get(future)
346346
else:
347347
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
348-
with pytest.raises(ZeroDivisionError):
348+
with pytest.raises(ZeroDivisionError): # noqa: PT012
349349
future = example_task.remote()
350350
ray.get(future)
351351

@@ -530,13 +530,13 @@ def increment(self):
530530
with sentry_sdk.traces.start_span(
531531
name="ray test parent", attributes={"sentry.op": "task"}
532532
):
533-
with pytest.raises(ZeroDivisionError):
533+
with pytest.raises(ZeroDivisionError): # noqa: PT012
534534
counter = Counter.remote()
535535
future = counter.increment.remote()
536536
ray.get(future)
537537
else:
538538
with sentry_sdk.start_transaction(op="task", name="ray test transaction"):
539-
with pytest.raises(ZeroDivisionError):
539+
with pytest.raises(ZeroDivisionError): # noqa: PT012
540540
counter = Counter.remote()
541541
future = counter.increment.remote()
542542
ray.get(future)

‎tests/integrations/sqlalchemy/test_sqlalchemy.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class Address(Base):
139139
session.query(Person).first()
140140

141141
for _ in range(2):
142-
with pytest.raises(IntegrityError):
142+
with pytest.raises(IntegrityError): # noqa: PT012
143143
with session.begin_nested():
144144
session.add(Person(id=1, name="bob"))
145145
session.add(Person(id=1, name="bob"))
@@ -185,7 +185,7 @@ class Address(Base):
185185
session.query(Person).first()
186186

187187
for _ in range(2):
188-
with pytest.raises(IntegrityError):
188+
with pytest.raises(IntegrityError): # noqa: PT012
189189
with session.begin_nested():
190190
session.add(Person(id=1, name="bob"))
191191
session.add(Person(id=1, name="bob"))
@@ -270,7 +270,7 @@ class Address(Base):
270270
session.query(Person).first()
271271

272272
for _ in range(2):
273-
with pytest.raises(IntegrityError):
273+
with pytest.raises(IntegrityError): # noqa: PT012
274274
with session.begin_nested():
275275
session.add(Person(id=1, name="bob"))
276276
session.add(Person(id=1, name="bob"))
@@ -297,7 +297,7 @@ class Address(Base):
297297
session.query(Person).first()
298298

299299
for _ in range(2):
300-
with pytest.raises(IntegrityError):
300+
with pytest.raises(IntegrityError): # noqa: PT012
301301
with session.begin_nested():
302302
session.add(Person(id=1, name="bob"))
303303
session.add(Person(id=1, name="bob"))

0 commit comments

Comments
 (0)