mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-11 08:38:24 +00:00
fix(guardrails): remove new basedpyright unknown-type errors
This commit is contained in:
parent
a8cb6f9840
commit
f299dd191e
3 changed files with 16 additions and 12 deletions
|
|
@ -14,7 +14,7 @@ def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"
|
|||
_tealtiger_callback: Final = TealTigerGuardrail(
|
||||
policies=getattr(litellm_params, "policies", None),
|
||||
policy_mode=getattr(litellm_params, "policy_mode", "ENFORCE"),
|
||||
guardrail_name=guardrail.get("guardrail_name", ""),
|
||||
guardrail_name=guardrail["guardrail_name"],
|
||||
event_hook=litellm_params.mode,
|
||||
default_on=litellm_params.default_on,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -94,9 +94,13 @@ class TealEngine:
|
|||
scope: Final = self._pii_policy.get("patterns", "all")
|
||||
if scope == "all":
|
||||
return self.patterns
|
||||
entries: Final = (scope,) if isinstance(scope, str) else scope
|
||||
if not isinstance(entries, (list, tuple)):
|
||||
raise TypeError("TealTiger: pii policy `patterns` must be 'all' or a list of names")
|
||||
if isinstance(scope, str):
|
||||
return self._select_patterns((scope,))
|
||||
if isinstance(scope, (list, tuple)):
|
||||
return self._select_patterns(scope) # pyright: ignore[reportUnknownArgumentType] # isinstance on object yields list[Unknown]
|
||||
raise TypeError("TealTiger: pii policy `patterns` must be 'all' or a list of names")
|
||||
|
||||
def _select_patterns(self, entries: Sequence[object]) -> Mapping[str, re.Pattern[str]]:
|
||||
names: Final = tuple(chain.from_iterable(map(self._expand_scope_entry, entries)))
|
||||
return MappingProxyType({name: self.patterns[name] for name in names})
|
||||
|
||||
|
|
@ -145,12 +149,12 @@ class TealEngine:
|
|||
if not self._tool_policy:
|
||||
return True
|
||||
raw_blocklist: Final = self._tool_policy.get("blocklist")
|
||||
blocklist: Final = raw_blocklist if isinstance(raw_blocklist, (list, tuple)) else ()
|
||||
blocklist: Final = raw_blocklist if isinstance(raw_blocklist, (list, tuple)) else () # pyright: ignore[reportUnknownVariableType] # isinstance on object yields list[Unknown]
|
||||
if tool_name in blocklist:
|
||||
return False
|
||||
raw_allowlist: Final = self._tool_policy.get("allowlist")
|
||||
if raw_allowlist is not None:
|
||||
allowlist: Final = raw_allowlist if isinstance(raw_allowlist, (list, tuple)) else ()
|
||||
allowlist: Final = raw_allowlist if isinstance(raw_allowlist, (list, tuple)) else () # pyright: ignore[reportUnknownVariableType] # isinstance on object yields list[Unknown]
|
||||
return tool_name in allowlist
|
||||
return True
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ from typing import TYPE_CHECKING, Final, Literal
|
|||
from litellm._logging import verbose_proxy_logger
|
||||
from litellm.integrations.custom_guardrail import (
|
||||
CustomGuardrail,
|
||||
log_guardrail_information,
|
||||
log_guardrail_information, # pyright: ignore[reportUnknownVariableType] # upstream decorator is untyped
|
||||
)
|
||||
from litellm.types.guardrails import GuardrailEventHooks, Mode
|
||||
from litellm.types.utils import GenericGuardrailAPIInputs
|
||||
|
|
@ -29,8 +29,6 @@ from .engine import Action, PolicyMode, TealEngine
|
|||
if TYPE_CHECKING:
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
|
||||
|
||||
_EMPTY_MAPPING: Final = MappingProxyType({})
|
||||
|
||||
DEFAULT_POLICIES: Final = (
|
||||
MappingProxyType({"type": "pii", "action": "REDACT", "patterns": "all"}),
|
||||
MappingProxyType({"type": "cost", "action": "ENFORCE", "daily_limit_usd": 50.0}),
|
||||
|
|
@ -64,7 +62,9 @@ def _tool_call_name_from_mapping(tool_call: Mapping[str, object]) -> str | None:
|
|||
if isinstance(direct_name, str):
|
||||
return direct_name
|
||||
function: Final = tool_call.get("function")
|
||||
nested_name: Final = function.get("name") if isinstance(function, Mapping) else None
|
||||
if not isinstance(function, Mapping):
|
||||
return None
|
||||
nested_name: Final = function.get("name") # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType] # isinstance on object yields Mapping[Unknown, Unknown]
|
||||
return nested_name if isinstance(nested_name, str) else None
|
||||
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ def _tool_call_name(tool_call: object) -> str | None:
|
|||
(ChatCompletionMessageToolCall). Avoids chained .get() calls, since one
|
||||
branch of that union doesn't reliably support dict-style access."""
|
||||
if isinstance(tool_call, Mapping):
|
||||
return _tool_call_name_from_mapping(tool_call)
|
||||
return _tool_call_name_from_mapping(tool_call) # pyright: ignore[reportUnknownArgumentType] # Mapping[Unknown, Unknown] from isinstance
|
||||
return _tool_call_name_from_object(tool_call)
|
||||
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ class TealTigerGuardrail(CustomGuardrail):
|
|||
policies=policies or DEFAULT_POLICIES,
|
||||
mode=PolicyMode(policy_mode),
|
||||
)
|
||||
super().__init__(
|
||||
super().__init__( # pyright: ignore[reportUnknownMemberType] # upstream CustomGuardrail.__init__ takes untyped **kwargs
|
||||
guardrail_name=guardrail_name,
|
||||
event_hook=_to_event_hook(event_hook),
|
||||
default_on=default_on or False,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue