mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
feat(any-gate): commit per-file Any baseline and guard deep-type recursion
Generate any-discipline-budget.json, the grandfathering snapshot the per-file gate reads: each file's current count of Any-tainted values, scanned across the whole litellm tree (1527 files carry Any today). A file may then hold up to baseline + ceil(baseline * 0.50) before the gate trips. Also cap the recursion in contains_any. The seen-set stops cycles between identical type objects, but a genuinely deep nesting (or a recursive type whose get_proper_type yields a fresh object each step) still overflowed the stack on a handful of real litellm modules. Bounding the walk depth keeps it from crashing; real Any-bearing types are shallow, so nothing actionable is missed.
This commit is contained in:
parent
715ec2d7a2
commit
0ca7318e8e
3 changed files with 1560 additions and 4 deletions
1529
any-discipline-budget.json
Normal file
1529
any-discipline-budget.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -142,6 +142,12 @@ _HARMLESS_ANY = frozenset(
|
|||
if kind is not None
|
||||
)
|
||||
|
||||
# Depth cap for the recursive "contains Any" walk. `seen` stops cycles between
|
||||
# identical type objects, but a genuinely deep nesting (or a recursive type whose
|
||||
# `get_proper_type` yields a fresh object each step) can still blow Python's stack.
|
||||
# Real Any-bearing types are shallow, so treat anything past this as Any-free.
|
||||
_MAX_TYPE_DEPTH = 100
|
||||
|
||||
# AST attributes that point OUTSIDE the syntactic subtree (a RefExpr's resolved
|
||||
# definition, a node's TypeInfo). Skipping exactly these two makes a generic
|
||||
# child-walk equivalent to mypy's TraverserVisitor -- validated to the node
|
||||
|
|
@ -170,8 +176,10 @@ def cap_for(baseline: int) -> int:
|
|||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def contains_any(t: Type, _seen: set[int] | None = None) -> bool:
|
||||
def contains_any(t: Type, _seen: set[int] | None = None, _depth: int = 0) -> bool:
|
||||
"""True if a *value* of type ``t`` carries `Any` anywhere meaningful."""
|
||||
if _depth > _MAX_TYPE_DEPTH:
|
||||
return False
|
||||
seen = _seen if _seen is not None else set()
|
||||
p = get_proper_type(t)
|
||||
if id(p) in seen:
|
||||
|
|
@ -185,11 +193,11 @@ def contains_any(t: Type, _seen: set[int] | None = None) -> bool:
|
|||
if isinstance(p, AnyType):
|
||||
return p.type_of_any not in _HARMLESS_ANY
|
||||
if isinstance(p, UnionType):
|
||||
return any(contains_any(item, seen) for item in p.items)
|
||||
return any(contains_any(item, seen, _depth + 1) for item in p.items)
|
||||
if isinstance(p, Instance):
|
||||
return any(contains_any(arg, seen) for arg in p.args)
|
||||
return any(contains_any(arg, seen, _depth + 1) for arg in p.args)
|
||||
if isinstance(p, TupleType):
|
||||
return any(contains_any(item, seen) for item in p.items)
|
||||
return any(contains_any(item, seen, _depth + 1) for item in p.items)
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -81,3 +81,22 @@ def test_new_file_with_any_breaches_against_a_zero_budget():
|
|||
|
||||
def test_clean_file_never_breaches():
|
||||
assert mod.budget_breaches({}, {"litellm/a.py": 100}) == []
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# contains_any: the depth cap keeps a pathologically deep type from blowing the
|
||||
# stack (some litellm types recurse past Python's default limit otherwise)
|
||||
# --------------------------------------------------------------------------- #
|
||||
|
||||
|
||||
def test_depth_cap_bails_out_past_the_limit():
|
||||
any_t = mod.AnyType(mod.TypeOfAny.explicit)
|
||||
assert mod.contains_any(any_t) is True
|
||||
assert mod.contains_any(any_t, _depth=mod._MAX_TYPE_DEPTH + 1) is False
|
||||
|
||||
|
||||
def test_any_nested_in_a_union_is_still_found():
|
||||
from mypy.types import NoneType
|
||||
|
||||
union = mod.UnionType([NoneType(), mod.AnyType(mod.TypeOfAny.explicit)])
|
||||
assert mod.contains_any(union) is True
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue