Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ select = [
# 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
"T20", # print() and pprint() calls
"PT011", # pytest.raises() too broad
]

# Use ONLY the same ignores as the original flake8 config + compatibility for this codebase
Expand Down
12 changes: 5 additions & 7 deletions tests/integrations/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,12 @@ async def app(scope, receive, send):


def test_invalid_transaction_style(asgi3_app):
with pytest.raises(ValueError) as exp:
with pytest.raises(
ValueError,
match=r"Invalid value for transaction_style: URL \(must be in \('endpoint', 'url'\)\)",
):
SentryAsgiMiddleware(asgi3_app, transaction_style="URL")

assert (
str(exp.value)
== "Invalid value for transaction_style: URL (must be in ('endpoint', 'url'))"
)


@pytest.mark.asyncio
@pytest.mark.parametrize(
Expand Down Expand Up @@ -552,7 +550,7 @@ async def test_websocket(

request_url = "/ws"

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Oh no"):
client = TestClient(asgi3_ws_app)
if span_streaming:
items = capture_items("event", "span")
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/boto3/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def fail_attribute_extraction(exception):
)

def invoke_failing_client_method():
with pytest.raises(ValueError) as exc_info:
with pytest.raises(ValueError, match="parameter processing failed") as exc_info:
client.head_object(Bucket="bucket", Key="foo")
assert exc_info.value is original_exception

Expand Down Expand Up @@ -750,7 +750,7 @@ def respond(request, **kwargs):

def invoke_client_method_and_read_body():
body = client.get_object(Bucket="bucket", Key="foo")["Body"]
with pytest.raises(OSError) as exc_info:
with pytest.raises(OSError, match="stream read failed") as exc_info:
body.read()
assert exc_info.value is original_exception

Expand Down
3 changes: 1 addition & 2 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ def dummy_task(self):
# Curious: Cannot use delay() here or py2.7-celery-4.2 crashes
res = dummy_task.apply_async()

with pytest.raises(Exception): # noqa: B017
# Celery 4.1 raises a gibberish exception
with pytest.raises(ZeroDivisionError):
res.wait()

# if this is nonempty, the worker never really forked
Expand Down
3 changes: 1 addition & 2 deletions tests/integrations/chalice/test_chalice.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ def every_hour(event):
"id": "event-id",
"resources": ["arn:aws:events:us-west-1:120987654312:rule/my-schedule"],
}
with pytest.raises(Exception) as exc_info:
with pytest.raises(Exception, match="schedule event!"):
every_hour(lambda_event, context=context)
assert str(exc_info.value) == "schedule event!"


@pytest.mark.skipif(
Expand Down
18 changes: 12 additions & 6 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3623,14 +3623,18 @@ def _llm_type(self) -> str:
if span_streaming or stream_gen_ai_spans:
items = capture_items("event")

with start_transaction(), pytest.raises(ValueError):
with start_transaction(), pytest.raises(
ValueError, match="API rate limit error"
):
list(agent_executor.stream({"input": "How many letters in the word eudca"}))

(error,) = (item.payload for item in items)
else:
events = capture_events()

with start_transaction(), pytest.raises(ValueError):
with start_transaction(), pytest.raises(
ValueError, match="API rate limit error"
):
list(agent_executor.stream({"input": "How many letters in the word eudca"}))

error = events[0]
Expand Down Expand Up @@ -3698,7 +3702,7 @@ def _llm_type(self) -> str:
agent=agent, tools=[get_word_length], verbose=True
)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="API rate limit error"):
list(
agent_executor.stream(
{"input": "How many letters in the word eudca"}
Expand Down Expand Up @@ -3737,7 +3741,7 @@ def _llm_type(self) -> str:
agent=agent, tools=[get_word_length], verbose=True
)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="API rate limit error"):
list(
agent_executor.stream(
{"input": "How many letters in the word eudca"}
Expand Down Expand Up @@ -3777,7 +3781,7 @@ def _llm_type(self) -> str:
agent=agent, tools=[get_word_length], verbose=True
)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="API rate limit error"):
list(
agent_executor.stream(
{"input": "How many letters in the word eudca"}
Expand Down Expand Up @@ -3857,7 +3861,9 @@ def test_langchain_tool_error(

with patch.object(
llm.client._client._client, "send", side_effect=[tool_response]
), start_transaction(name="tx"), pytest.raises(ValueError):
), start_transaction(name="tx"), pytest.raises(
ValueError, match="Tool execution failed"
):
agent.invoke({"messages": [HumanMessage(content="hi")]})

error_events = [event for event in events if event.get("level") == "error"]
Expand Down
4 changes: 2 additions & 2 deletions tests/integrations/stdlib/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import socket
import time
from http.client import HTTPConnection, HTTPSConnection
from http.client import CannotSendRequest, HTTPConnection, HTTPSConnection
from http.server import BaseHTTPRequestHandler, HTTPServer
from socket import SocketIO
from threading import Thread
Expand Down Expand Up @@ -356,7 +356,7 @@ def test_httplib_misuse(sentry_init, capture_events, request):

conn.request("GET", "/200")

with pytest.raises(Exception): # noqa: B017
with pytest.raises(CannotSendRequest):
# This raises an exception, because we didn't call `getresponse` for
# the previous request yet.
#
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/wsgi/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def dogpark(environ, start_response):
else:
events = capture_events()

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Fetch aborted"):
client.get("http://dogs.are.great/sit/stay/rollover/")

sentry_sdk.flush()
Expand Down Expand Up @@ -300,7 +300,7 @@ def dogpark(environ, start_response):
else:
events = capture_events()

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Fetch aborted"):
client.get("http://dogs.are.great/sit/stay/rollover/")

sentry_sdk.flush()
Expand Down Expand Up @@ -359,7 +359,7 @@ def dogpark(environ, start_response):
client = Client(app)
events = capture_events()

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Fetch aborted"):
client.get("http://dogs.are.great/sit/stay/rollover/")

msg_event, error_event = events
Expand Down Expand Up @@ -397,7 +397,7 @@ def dogpark(environ, start_response):
trace_id = "582b43a4192642f0b136d5159a501701"
sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Fetch aborted"):
client.get(
"http://dogs.are.great/sit/stay/rollover/",
headers={"sentry-trace": sentry_trace_header},
Expand Down Expand Up @@ -444,7 +444,7 @@ def dogpark(environ, start_response):
trace_id = "582b43a4192642f0b136d5159a501701"
sentry_trace_header = "{}-{}-{}".format(trace_id, "6e8f22c393e68f19", 1)

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Fetch aborted"):
client.get(
"http://dogs.are.great/sit/stay/rollover/",
headers={"sentry-trace": sentry_trace_header},
Expand Down
2 changes: 1 addition & 1 deletion tests/profiler/test_continuous_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def client_options(
],
)
def test_continuous_profiler_invalid_mode(mode, make_options, teardown_profiling):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Unknown continuous profiler mode: foo"):
setup_continuous_profiler(
make_options(mode=mode),
mock_sdk_info,
Expand Down
2 changes: 1 addition & 1 deletion tests/profiler/test_transaction_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def experimental_options(mode=None, sample_rate=None):
],
)
def test_profiler_invalid_mode(mode, make_options, teardown_profiling):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Unknown profiler mode: foo"):
setup_profiler(make_options(mode))


Expand Down
6 changes: 3 additions & 3 deletions tests/test_data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@


def test_kvcb_invalid_mode(sentry_init):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Invalid collection mode 'nope'"):
sentry_init(data_collection={"cookies": {"mode": "nope"}}) # type: ignore Purposely ignoring to test invalid option


def test_stack_frame_variables_invalid_mode(sentry_init):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Invalid collection mode 'nope'"):
sentry_init(data_collection={"stack_frame_variables": {"mode": "nope"}})


Expand All @@ -28,7 +28,7 @@ def test_stack_frame_variables_invalid_mode(sentry_init):
],
)
def test_frame_context_lines_invalid_value(sentry_init, value):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Invalid `frame_context_lines` value"):
sentry_init(data_collection={"frame_context_lines": value})


Expand Down
16 changes: 7 additions & 9 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,14 +913,12 @@ def test_is_sentry_url_no_client():
],
)
def test_get_error_message(error, expected_result):
with pytest.raises(BaseException) as exc_value:
exc_value.message = error
raise Exception
exc_value = Exception()
exc_value.message = error
assert get_error_message(exc_value) == expected_result(exc_value)

with pytest.raises(BaseException) as exc_value:
exc_value.detail = error
raise Exception
exc_value = Exception()
exc_value.detail = error
assert get_error_message(exc_value) == expected_result(exc_value)


Expand Down Expand Up @@ -1296,11 +1294,11 @@ def test_exc_info_from_error_dont_get_an_exc():
class NotAnException:
pass

with pytest.raises(ValueError) as exc:
with pytest.raises(
ValueError, match="Expected Exception object to report, got <class"
):
exc_info_from_error(NotAnException())

assert "Expected Exception object to report, got <class" in str(exc.value)


def test_get_lines_from_file_handle_linecache_errors():
expected_result = ([], None, [])
Expand Down
2 changes: 1 addition & 1 deletion tests/tracing/test_span_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ def test_set_span_status_on_error(sentry_init, capture_items):

items = capture_items("span")

with pytest.raises(ValueError):
with pytest.raises(ValueError, match="oh no!"):
with sentry_sdk.traces.start_span(name="span") as span:
raise ValueError("oh no!")

Expand Down
Loading