Skip to content

Commit 443d83d

Browse files
ci(mypy): Enable warn_unreachable
Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent 7a45522 commit 443d83d

32 files changed

Lines changed: 137 additions & 102 deletions

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ python_version = "3.11"
152152
strict_equality = true
153153
strict_optional = true
154154
warn_redundant_casts = true
155+
warn_unreachable = true
155156
warn_unused_configs = true
156157
warn_unused_ignores = true
157158

‎sentry_sdk/ai/_openai_completions_api.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _get_system_instructions(
2626
messages: "Iterable[ChatCompletionMessageParam]",
2727
) -> "list[ChatCompletionSystemMessageParam]":
2828
if not isinstance(messages, Iterable):
29-
return []
29+
return [] # type: ignore[unreachable] # defensive check on user input
3030

3131
return [message for message in messages if _is_system_instruction(message)]
3232

@@ -38,7 +38,7 @@ def _get_text_items(
3838
return [content]
3939

4040
if not isinstance(content, Iterable):
41-
return []
41+
return [] # type: ignore[unreachable] # defensive check on user input
4242

4343
text_items: "list[str]" = []
4444
for part in content:
@@ -57,11 +57,11 @@ def _transform_system_instructions(
5757

5858
for instruction in system_instructions:
5959
if not isinstance(instruction, dict):
60-
continue
60+
continue # type: ignore[unreachable] # defensive check on user input
6161

6262
content = instruction.get("content")
6363
if content is None:
64-
continue
64+
continue # type: ignore[unreachable] # defensive check on user input
6565

6666
text_parts: "list[TextPart]" = [
6767
{"type": "text", "content": text} for text in _get_text_items(content)
@@ -78,20 +78,20 @@ def _transform_tool_definitions(
7878
Transform tool definitions to the schema used by the "gen_ai.tool.definitions" attribute.
7979
"""
8080
if not isinstance(tools, Iterable):
81-
return []
81+
return [] # type: ignore[unreachable] # defensive check on user input
8282

8383
tool_definitions = []
8484
for tool in tools:
8585
if not isinstance(tool, dict) or "type" not in tool:
86-
continue
86+
continue # type: ignore[unreachable] # defensive check on user input
8787

8888
if tool["type"] == "function":
8989
tool_definition: "ToolDefinition" = {
9090
"type": "function",
9191
}
9292

9393
if "function" not in tool:
94-
tool_definitions.append(tool_definition)
94+
tool_definitions.append(tool_definition) # type: ignore[unreachable] # defensive check on user input
9595
continue
9696

9797
if "name" in tool["function"]:
@@ -112,7 +112,7 @@ def _transform_tool_definitions(
112112
}
113113

114114
if "custom" not in tool:
115-
tool_definitions.append(tool_definition)
115+
tool_definitions.append(tool_definition) # type: ignore[unreachable] # defensive check on user input
116116
continue
117117

118118
if "name" in tool["custom"]:

‎sentry_sdk/ai/_openai_responses_api.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ def _transform_system_instructions(
4040

4141
for instruction in system_instructions:
4242
if not isinstance(instruction, dict):
43-
continue
43+
continue # type: ignore[unreachable] # defensive check on user input
4444

4545
content = instruction.get("content")
4646
if content is None:
47-
continue
47+
continue # type: ignore[unreachable] # defensive check on user input
4848

4949
if isinstance(content, str):
5050
instruction_text_parts.append({"type": "text", "content": content})
5151
continue
5252

5353
if not isinstance(content, Iterable):
54-
continue
54+
continue # type: ignore[unreachable] # defensive check on user input
5555

5656
for part in content:
5757
if not isinstance(part, dict) or part.get("type") != "input_text":
@@ -70,12 +70,12 @@ def _transform_tool_definitions(tools: "Iterable[ToolParam]") -> "list[ToolDefin
7070
Includes special handling for tools where the type includes a name, description or parameters.
7171
"""
7272
if not isinstance(tools, Iterable):
73-
return []
73+
return [] # type: ignore[unreachable] # defensive check on user input
7474

7575
tool_definitions = []
7676
for tool in tools:
7777
if not isinstance(tool, dict) or "type" not in tool:
78-
continue
78+
continue # type: ignore[unreachable] # defensive check on user input
7979

8080
if tool["type"] == "function":
8181
tool_definition: "ToolDefinition" = {

‎sentry_sdk/ai/utils.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def transform_openai_content_part(
130130
is not OpenAI image_url format or transformation fails.
131131
"""
132132
if not isinstance(content_part, dict):
133-
return None
133+
return None # type: ignore[unreachable] # defensive check on SDK input
134134

135135
block_type = content_part.get("type")
136136

@@ -203,7 +203,7 @@ def transform_anthropic_content_part(
203203
is not Anthropic format or transformation fails.
204204
"""
205205
if not isinstance(content_part, dict):
206-
return None
206+
return None # type: ignore[unreachable] # defensive check on SDK input
207207

208208
block_type = content_part.get("type")
209209

@@ -271,7 +271,7 @@ def transform_google_content_part(
271271
is not Google format or transformation fails.
272272
"""
273273
if not isinstance(content_part, dict):
274-
return None
274+
return None # type: ignore[unreachable] # defensive check on SDK input
275275

276276
# Handle Google inline_data format
277277
if "inline_data" in content_part:
@@ -330,7 +330,7 @@ def transform_generic_content_part(
330330
is not generic format or transformation fails.
331331
"""
332332
if not isinstance(content_part, dict):
333-
return None
333+
return None # type: ignore[unreachable] # defensive check on SDK input
334334

335335
block_type = content_part.get("type")
336336

@@ -405,7 +405,7 @@ def transform_content_part(
405405
is unrecognized or transformation fails.
406406
"""
407407
if not isinstance(content_part, dict):
408-
return None
408+
return None # type: ignore[unreachable] # defensive check on SDK input
409409

410410
# Try OpenAI format first (most common, clear indicator)
411411
result = transform_openai_content_part(content_part)
@@ -526,7 +526,7 @@ def normalize_message_roles(messages: "list[dict[str, Any]]") -> "list[dict[str,
526526
normalized_messages = []
527527
for message in messages:
528528
if not isinstance(message, dict):
529-
normalized_messages.append(message)
529+
normalized_messages.append(message) # type: ignore[unreachable] # defensive check on SDK input
530530
continue
531531
normalized_message = message.copy()
532532
if "role" in message:
@@ -661,7 +661,7 @@ def redact_blob_message_parts(
661661
has_blobs = False
662662
for message in messages:
663663
if not isinstance(message, dict):
664-
continue
664+
continue # type: ignore[unreachable] # defensive check on SDK input
665665
content = message.get("content")
666666
if isinstance(content, list):
667667
for item in content:
@@ -683,7 +683,7 @@ def redact_blob_message_parts(
683683
# Second pass: redact blob content in the copy
684684
for message in messages_copy:
685685
if not isinstance(message, dict):
686-
continue
686+
continue # type: ignore[unreachable] # defensive check on SDK input
687687

688688
content = message.get("content")
689689
if isinstance(content, list):

‎sentry_sdk/client.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,8 @@ def _update_session_from_event(
11251125
if exceptions:
11261126
errored = True
11271127
for error in exceptions:
1128-
if isinstance(error, AnnotatedValue):
1129-
error = error.value or {}
1128+
if isinstance(error, AnnotatedValue): # type: ignore[unreachable] # the event may already contain annotated values
1129+
error = error.value or {} # type: ignore[unreachable]
11301130
mechanism = error.get("mechanism")
11311131
if isinstance(mechanism, Mapping) and mechanism.get("handled") is False:
11321132
crashed = True
@@ -1475,14 +1475,14 @@ async def close_async(
14751475
semantics as :py:meth:`Client.flush_async`.
14761476
"""
14771477
if self.transport is not None:
1478-
if not self._has_async_transport():
1478+
if not isinstance(self.transport, AsyncHttpTransport):
14791479
logger.debug(
14801480
"close_async() used with non-async transport, aborting. Please use close() instead."
14811481
)
14821482
return
14831483
await self.flush_async(timeout=timeout, callback=callback)
14841484
self._close_components()
1485-
kill_task = self.transport.kill() # type: ignore
1485+
kill_task = self.transport.kill()
14861486
if kill_task is not None:
14871487
await kill_task
14881488
self.transport = None
@@ -1525,15 +1525,15 @@ async def flush_async(
15251525
:param callback: Is invoked with the number of pending events and the configured timeout.
15261526
"""
15271527
if self.transport is not None:
1528-
if not self._has_async_transport():
1528+
if not isinstance(self.transport, AsyncHttpTransport):
15291529
logger.debug(
15301530
"flush_async() used with non-async transport, aborting. Please use flush() instead."
15311531
)
15321532
return
15331533
if timeout is None:
15341534
timeout = self.options["shutdown_timeout"]
15351535
self._flush_components()
1536-
flush_task = self.transport.flush(timeout=timeout, callback=callback) # type: ignore
1536+
flush_task = self.transport.flush(timeout=timeout, callback=callback)
15371537
if flush_task is not None:
15381538
await flush_task
15391539

‎sentry_sdk/feature_flags.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get(self) -> "list[FlagData]":
4141
]
4242

4343
def set(self, flag: str, result: bool) -> None:
44-
if isinstance(result, FlagBuffer):
44+
if isinstance(result, FlagBuffer): # type: ignore[unreachable] # guard against misuse
4545
# If someone were to insert `self` into `self` this would create a circular dependency
4646
# on the lock. This is of course a deadlock. However, this is far outside the expected
4747
# usage of this class. We guard against it here for completeness and to document this

‎sentry_sdk/integrations/aiohttp.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,9 @@ async def on_request_start(
492492
# `baggage` with Sentry members (e.g. merged by the boto3 integration
493493
# before signing): skip so Sentry members are not duplicated.
494494
with capture_internal_exceptions():
495-
baggage = params.headers.get(BAGGAGE_HEADER_NAME)
495+
baggage: "Optional[Union[str, bytes]]" = params.headers.get(
496+
BAGGAGE_HEADER_NAME
497+
)
496498
if baggage:
497499
if isinstance(baggage, bytes):
498500
baggage = baggage.decode("latin-1")
@@ -503,7 +505,9 @@ async def on_request_start(
503505
headers_to_skip.add(BAGGAGE_HEADER_NAME)
504506

505507
with capture_internal_exceptions():
506-
authorization = params.headers.get("Authorization")
508+
authorization: "Optional[Union[str, bytes]]" = params.headers.get(
509+
"Authorization"
510+
)
507511
if authorization:
508512
if isinstance(authorization, bytes):
509513
authorization = authorization.decode("latin-1")

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class _PatchedMessageStreamManager(MessageStreamManager):
138138
_integration: "AnthropicIntegration"
139139

140140
_max_tokens: int
141-
_messages: Iterable[MessageParam]
141+
_messages: Optional[Iterable[MessageParam]]
142142
_model: Optional[ModelParam]
143143
_system: Optional[Union[str, Iterable[TextBlockParam]]]
144144
_temperature: Optional[float]
@@ -151,7 +151,7 @@ class _PatchedAsyncMessageStreamManager(AsyncMessageStreamManager[Any]):
151151
_integration: "AnthropicIntegration"
152152

153153
_max_tokens: int
154-
_messages: Iterable[MessageParam]
154+
_messages: Optional[Iterable[MessageParam]]
155155
_model: Optional[ModelParam]
156156
_system: Optional[Union[str, Iterable[TextBlockParam]]]
157157
_temperature: Optional[float]

‎sentry_sdk/integrations/aws_lambda.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
def _get_user_from_event(aws_event: "dict[str, Any]") -> "dict[str, Any]":
4949
if not isinstance(aws_event, dict):
50-
return {}
50+
return {} # type: ignore[unreachable] # the event can be any JSON value
5151

5252
identity = aws_event.get("requestContext", {}).get("identity")
5353
if identity is None:

‎sentry_sdk/integrations/boto3/_services/bedrock.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ def _transform_tool_definitions(
583583
result: "list[ToolDefinition]" = []
584584
for tool in tools:
585585
if not isinstance(tool, dict):
586-
continue
586+
continue # type: ignore[unreachable] # defensive check on user input
587587

588588
if "toolSpec" in tool:
589589
tool_definition: "ToolDefinition" = {"type": "function"}

0 commit comments

Comments
 (0)