From 36e8ca43a868852260dddff9527ad777ae727a3d Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:05:43 -0700 Subject: [PATCH] test(guardrails): type the masking guardrail test helpers --- .../test_anthropic_guardrail_handler.py | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py b/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py index 4e85ac18096..2436efa8a2f 100644 --- a/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py +++ b/tests/unit/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py @@ -561,11 +561,18 @@ class TestAnthropicMessagesHandlerStreamingOutputProcessing: @staticmethod def _python_masking_guardrail() -> CustomGuardrail: class MaskPython(CustomGuardrail): - async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None): + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict[str, object], + input_type: Literal["request", "response"], + logging_obj: Optional[LiteLLMLoggingObj] = None, + ) -> GenericGuardrailAPIInputs: for tool_call in inputs.get("tool_calls", []): - tool_call.function.arguments = re.sub( - "python", "[MASKED]", tool_call.function.arguments, flags=re.I - ) + if isinstance(tool_call, ChatCompletionMessageToolCall): + tool_call.function.arguments = re.sub( + "python", "[MASKED]", tool_call.function.arguments, flags=re.I + ) return { **inputs, "texts": [re.sub("python", "[MASKED]", text, flags=re.I) for text in inputs.get("texts", [])], @@ -576,7 +583,13 @@ class TestAnthropicMessagesHandlerStreamingOutputProcessing: @staticmethod def _python_texts_masking_guardrail() -> CustomGuardrail: class MaskPythonTexts(CustomGuardrail): - async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None): + async def apply_guardrail( + self, + inputs: GenericGuardrailAPIInputs, + request_data: dict[str, object], + input_type: Literal["request", "response"], + logging_obj: Optional[LiteLLMLoggingObj] = None, + ) -> GenericGuardrailAPIInputs: return { **inputs, "texts": [re.sub("python", "[MASKED]", text, flags=re.I) for text in inputs.get("texts", [])],