The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(scripts): unwrap PEP 604 unions in LIT002 TypedDict detection
mateo-berri316732b3ae46
diff --git a/scripts/check_type_discipline.py b/scripts/check_type_discipline.pyindex e21693b2c9b..0706c8a7bd8 100644--- a/scripts/check_type_discipline.py+++ b/scripts/check_type_discipline.py@@ -30,7 +30,8 @@ LIT002 Mutable-collection *construction*: a list/dict/set literal or comprehensfields LIT012 keeps ReadOnly, not a growable accumulator, so it is exempt alongwith the dict literals nested in it (nested TypedDict fields); any otherconstruction inside still counts. Detection is name-based: Final/ClassVar/- Optional (and Annotated's first argument) unwrap, and any remaining named head+ Optional (and Annotated's first argument) unwrap, a PEP 604 union+ (`MyTD | None`) qualifies through either arm, and any remaining named headoutside the mutable collections and Mapping/Any/object is taken to be aTypedDict, since a dict literal assigned to any other named type would notsurvive basedpyright. Suppress with `# mutable-ok: <reason>`.@@ -514,10 +515,11 @@ def _is_typeddict_annotation(annotation: ast.expr) -> bool:"""True iff the annotation names a TypedDict, by the name-based heuristic.Final/ClassVar/Optional unwrap (as does Annotated's first argument, the only- one that is type syntax), string forward references are parsed, and whatever- named head remains counts as a TypedDict unless it is a mutable collection or- Mapping/Any/object -- the heads that can type a dict literal without being- one. Bare wrappers (`x: Final = ...`) name no type and never qualify.+ one that is type syntax), a PEP 604 union qualifies through either arm, string+ forward references are parsed, and whatever named head remains counts as a+ TypedDict unless it is a mutable collection or Mapping/Any/object -- the heads+ that can type a dict literal without being one. Bare wrappers+ (`x: Final = ...`) name no type and never qualify."""if isinstance(annotation, ast.Constant) and isinstance(annotation.value, str):try:@@ -525,6 +527,8 @@ def _is_typeddict_annotation(annotation: ast.expr) -> bool:except SyntaxError:return Falsereturn _is_typeddict_annotation(inner)+ if isinstance(annotation, ast.BinOp) and isinstance(annotation.op, ast.BitOr):+ return _is_typeddict_annotation(annotation.left) or _is_typeddict_annotation(annotation.right)if isinstance(annotation, ast.Subscript):head = _head_name(annotation.value)if head in TYPEDDICT_ANNOTATION_WRAPPERS:diff --git a/tests/test_litellm/test_check_type_discipline.py b/tests/test_litellm/test_check_type_discipline.pyindex 78268a6daa3..84dd547ad80 100644--- a/tests/test_litellm/test_check_type_discipline.py+++ b/tests/test_litellm/test_check_type_discipline.py@@ -218,6 +218,8 @@ def test_wrapped_typeddict_annotations_share_the_exemption(tmp_path):assert "LIT002" not in _codes(tmp_path, "from typing import ClassVar\nclass C:\n x: ClassVar[MyTD] = {'a': 1}\n")+ assert "LIT002" not in _codes(tmp_path, "from typing import Final\nx: Final[MyTD | None] = {'a': 1}\n")+ assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[dict[str, int] | None] = {'a': 1}\n")def test_bare_final_dict_literal_still_counts(tmp_path):@@ -235,8 +237,6 @@ def test_non_typeddict_annotations_do_not_exempt(tmp_path):def test_typeddict_exemption_covers_only_dict_literals(tmp_path):- # A TypedDict cannot be built from a comprehension (its keys are fixed literals),- # and `dict(...)` is the constructor call the rule targets, so neither is exempt.assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[MyTD] = dict(a=1)\n")assert "LIT002" in _codes(tmp_path, "from typing import Final\nx: Final[MyTD] = {k: 1 for k in ('a',)}\n")diff --git a/type-discipline-budget.json b/type-discipline-budget.jsonindex 03191c460c0..909afb0a9db 100644--- a/type-discipline-budget.json+++ b/type-discipline-budget.json@@ -3,7 +3,7 @@"limit": 23001},"LIT002": {- "limit": 26916+ "limit": 26912},"LIT003": {"limit": 269