Skip to content

Commit b1803a8

Browse files
ci(mypy): Enable redundant-expr error code
Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent ddd4948 commit b1803a8

25 files changed

Lines changed: 49 additions & 45 deletions

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ disallow_incomplete_defs = true
146146
disallow_subclassing_any = true
147147
disallow_untyped_decorators = true
148148
disallow_untyped_defs = true
149+
enable_error_code = ["redundant-expr"]
149150
no_implicit_optional = true
150151
python_version = "3.11"
151152
strict_equality = true

‎sentry_sdk/ai/_openai_completions_api.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
def _is_system_instruction(
2020
message: "ChatCompletionMessageParam",
2121
) -> "TypeGuard[ChatCompletionSystemMessageParam]":
22-
return isinstance(message, dict) and message.get("role") == "system"
22+
return isinstance(message, dict) and message.get("role") == "system" # type: ignore[redundant-expr] # user input may not match the annotation
2323

2424

2525
def _get_system_instructions(
@@ -42,7 +42,7 @@ def _get_text_items(
4242

4343
text_items: "list[str]" = []
4444
for part in content:
45-
if isinstance(part, dict) and part.get("type") == "text":
45+
if isinstance(part, dict) and part.get("type") == "text": # type: ignore[redundant-expr] # user input may not match the annotation
4646
text = cast("ChatCompletionContentPartTextParam", part).get("text", None)
4747
if text is not None:
4848
text_items.append(text)
@@ -82,7 +82,7 @@ def _transform_tool_definitions(
8282

8383
tool_definitions = []
8484
for tool in tools:
85-
if not isinstance(tool, dict) or "type" not in tool:
85+
if not isinstance(tool, dict) or "type" not in tool: # type: ignore[redundant-expr] # user input may not match the annotation
8686
continue
8787

8888
if tool["type"] == "function":

‎sentry_sdk/ai/_openai_responses_api.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
def _is_system_instruction(
1919
message: "ResponseInputItemParam",
2020
) -> "TypeGuard[Union[EasyInputMessageParam, Message]]":
21-
if not isinstance(message, dict) or not message.get("role") == "system":
21+
if not isinstance(message, dict) or not message.get("role") == "system": # type: ignore[redundant-expr] # user input may not match the annotation
2222
return False
2323

2424
return "type" not in message or message["type"] == "message"
@@ -54,7 +54,7 @@ def _transform_system_instructions(
5454
continue
5555

5656
for part in content:
57-
if not isinstance(part, dict) or part.get("type") != "input_text":
57+
if not isinstance(part, dict) or part.get("type") != "input_text": # type: ignore[redundant-expr] # user input may not match the annotation
5858
continue
5959

6060
text = cast("ResponseInputTextParam", part).get("text", None)
@@ -74,7 +74,7 @@ def _transform_tool_definitions(tools: "Iterable[ToolParam]") -> "list[ToolDefin
7474

7575
tool_definitions = []
7676
for tool in tools:
77-
if not isinstance(tool, dict) or "type" not in tool:
77+
if not isinstance(tool, dict) or "type" not in tool: # type: ignore[redundant-expr] # user input may not match the annotation
7878
continue
7979

8080
if tool["type"] == "function":
@@ -88,7 +88,7 @@ def _transform_tool_definitions(tools: "Iterable[ToolParam]") -> "list[ToolDefin
8888
if "description" in tool and tool["description"] is not None:
8989
tool_definition["description"] = tool["description"]
9090

91-
if "parameters" in tool and tool["parameters"] is not None:
91+
if "parameters" in tool and tool["parameters"] is not None: # type: ignore[redundant-expr] # user input may not match the annotation
9292
tool_definition["parameters"] = tool["parameters"]
9393

9494
tool_definitions.append(tool_definition)

‎sentry_sdk/ai/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def _truncate_single_message_content_if_present(
552552
Truncate a message's content to at most `max_chars` characters and append an
553553
ellipsis if truncation occurs.
554554
"""
555-
if not isinstance(message, dict) or "content" not in message:
555+
if not isinstance(message, dict) or "content" not in message: # type: ignore[redundant-expr] # message may not be a dict at runtime
556556
return message
557557
content = message["content"]
558558

‎sentry_sdk/client.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def _prepare_event(
949949
before_send = self.options["before_send"]
950950
if (
951951
before_send is not None
952-
and event is not None
952+
and event is not None # type: ignore[redundant-expr] # serialize() can return None on internal errors
953953
and event.get("type") != "transaction"
954954
):
955955
new_event = None
@@ -1230,7 +1230,6 @@ def capture_event(
12301230
converted_gen_ai_spans = [
12311231
_serialized_v1_span_to_serialized_v2_span(span, event_opt)
12321232
for span in gen_ai_spans
1233-
if isinstance(span, dict)
12341233
]
12351234

12361235
envelope.add_item(

‎sentry_sdk/data_collection.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _apply_key_value_collection_filtering(
141141

142142
# denylist behaviour
143143
for key, value in items.items():
144-
if isinstance(key, str) and _is_sensitive_key(
144+
if isinstance(key, str) and _is_sensitive_key( # type: ignore[redundant-expr] # items may have non-str keys at runtime
145145
key=key, extra_terms=behaviour.get("terms", [])
146146
):
147147
result[key] = substitute

‎sentry_sdk/integrations/_wsgi_common.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _filter_headers(
234234
)
235235

236236
for key in filtered:
237-
if isinstance(key, str) and key.lower() in ("cookie", "set-cookie"):
237+
if isinstance(key, str) and key.lower() in ("cookie", "set-cookie"): # type: ignore[redundant-expr] # headers may have non-str keys at runtime
238238
filtered[key] = SENSITIVE_DATA_SUBSTITUTE
239239

240240
return filtered

‎sentry_sdk/integrations/anthropic.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,14 +499,14 @@ def _transform_system_instructions(
499499
"content": instruction["text"],
500500
}
501501
for instruction in system_instructions
502-
if isinstance(instruction, dict) and "text" in instruction
502+
if isinstance(instruction, dict) and "text" in instruction # type: ignore[redundant-expr] # user input may not match the annotation
503503
]
504504

505505

506506
def _set_common_input_data(
507507
span: "Union[Span, StreamedSpan]",
508508
integration: "AnthropicIntegration",
509-
max_tokens: "int",
509+
max_tokens: "Optional[int]",
510510
messages: "Iterable[MessageParam]",
511511
model: "Optional[ModelParam]",
512512
system: "Optional[Union[str, Iterable[TextBlockParam]]]",
@@ -570,7 +570,7 @@ def _set_common_input_data(
570570
for message in messages:
571571
if (
572572
message.get("role") == GEN_AI_ALLOWED_MESSAGE_ROLES.USER
573-
and "content" in message
573+
and "content" in message # type: ignore[redundant-expr] # user input may not match the annotation
574574
and isinstance(message["content"], (list, tuple))
575575
):
576576
transformed_content = []
@@ -644,7 +644,7 @@ def _set_create_input_data(
644644
_set_common_input_data(
645645
span=span,
646646
integration=integration,
647-
max_tokens=kwargs.get("max_tokens"), # type: ignore
647+
max_tokens=kwargs.get("max_tokens"),
648648
messages=kwargs.get("messages"), # type: ignore
649649
model=kwargs.get("model"),
650650
system=kwargs.get("system"),

‎sentry_sdk/integrations/asgi.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ async def _sentry_wrapped_send(
412412
finally:
413413
if isinstance(span, StreamedSpan):
414414
already_set = (
415-
span is not None
416-
and span.name != _DEFAULT_TRANSACTION_NAME
415+
span.name != _DEFAULT_TRANSACTION_NAME
417416
and span.get_attributes().get(
418417
"sentry.segment.name.source"
419418
)

‎sentry_sdk/integrations/aws_lambda.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def sentry_init_error(*args: "Any", **kwargs: "Any") -> "Any":
7575
sentry_sdk.get_isolation_scope().clear_breadcrumbs()
7676

7777
exc_info = sys.exc_info()
78-
if exc_info and all(exc_info):
78+
if all(exc_info):
7979
sentry_event, hint = event_from_exception(
8080
exc_info,
8181
client_options=client.options,

0 commit comments

Comments
 (0)