fix(typing): count only the probe errors in the Router stub guard and flag Final type variables everywhere
Some checks failed
Cost map pins recheck / rerun-cost-map-guard (push) Waiting to run
LiteLLM Rust DB / db-queries (push) Waiting to run
LiteLLM Rust / rust-lint (push) Waiting to run
LiteLLM Rust / rust-test (push) Waiting to run
LiteLLM Rust / rust-wheel (push) Waiting to run
Terraform Provider / gofmt, vet, build, test (push) Has been cancelled
Terraform Provider / Provider endpoints vs proxy OpenAPI schema (push) Has been cancelled

This commit is contained in:
mateo-berri 2026-11-06 19:13:43 -08:00
parent 995045d0cf
commit 970022aca3
3 changed files with 30 additions and 12 deletions

View file

@ -583,7 +583,7 @@ def _node_bindings(node: ast.AST, in_loop: bool) -> Iterator[Binding]:
yield from (Binding(n, line, "unpack", in_loop) for n, line in _bound_names(target))
case ast.AnnAssign(target=ast.Name(id=name, lineno=line), annotation=annotation, value=value):
if _declares_constant(annotation):
yield Binding(name, line, "final_type_variable" if _creates_type_variable(value) else "declared", in_loop)
yield Binding(name, line, "declared", in_loop)
elif value is not None:
yield Binding(name, line, "annassign", in_loop)
case ast.AugAssign(target=ast.Name(id=name, lineno=line)):
@ -660,15 +660,6 @@ def iter_final_violations(path: Path, tree: ast.AST) -> Iterator[Violation]:
declared = frozenset(b.name for b in bindings if b.form == "declared")
first = _first_binding_index(bindings)
for i, b in enumerate(bindings):
if b.form == "final_type_variable":
yield Violation(
path,
b.line,
"LIT010",
f"`{b.name}` is a type variable declared Final: mypy then reads it as a plain "
f"variable and every signature using it as Any, so remove the Final",
)
continue
if b.name in declared or b.name in params or b.in_loop:
continue
if _exempt_final_name(b.name):
@ -696,6 +687,23 @@ def iter_final_violations(path: Path, tree: ast.AST) -> Iterator[Violation]:
)
def iter_final_type_variable_violations(path: Path, tree: ast.AST) -> Iterator[Violation]:
for node in ast.walk(tree):
match node:
case ast.AnnAssign(target=ast.Name(id=name, lineno=line), annotation=annotation, value=value) if (
_declares_constant(annotation) and _creates_type_variable(value)
):
yield Violation(
path,
line,
"LIT010",
f"`{name}` is a type variable declared Final: mypy then reads it as a plain "
f"variable and every signature using it as Any, so remove the Final",
)
case _:
pass
def _mutation_sites(scope: ast.AST) -> Iterator[tuple[str, int]]:
for node, _in_loop in _walk_scope(scope):
match node:
@ -1167,6 +1175,7 @@ def check_file(path: Path) -> tuple[Violation, ...]:
*iter_cast_violations(path, tree),
*iter_guard_violations(path, tree),
*iter_final_violations(path, tree),
*iter_final_type_variable_violations(path, tree),
*iter_param_violations(path, tree),
*iter_typeddict_violations(path, tree),
*iter_pydantic_violations(path, tree),

View file

@ -46,10 +46,11 @@ def main() -> int:
probe: Final = Path(directory) / "router_constructor_probe.py"
probe.write_text(PROBE)
errors: Final = mypy_errors(probe)
if len(errors) == 1 and EXPECTED_ERROR in errors[0]:
probe_errors: Final = tuple(error for error in errors if Path(error.partition(":")[0]).name == probe.name)
if len(probe_errors) == 1 and EXPECTED_ERROR in probe_errors[0]:
print("mypy reads the Router constructor stub")
return 0
print(f"expected only {EXPECTED_ERROR!r} from mypy, got: {errors}")
print(f"expected only {EXPECTED_ERROR!r} from the probe, got: {errors}")
return 1

View file

@ -313,6 +313,14 @@ def test_a_type_variable_stays_bare_and_a_final_one_trips(tmp_path, declaration)
assert "LIT010" in _codes(tmp_path, f"_T: Final[object] = {declaration}\n")
def test_a_final_type_variable_trips_in_a_class_body(tmp_path):
assert "LIT010" in _codes(tmp_path, "class Box:\n _T: Final = TypeVar('_T')\n")
def test_a_final_type_variable_trips_on_the_config_surface(tmp_path):
assert "LIT010" in _codes_at(tmp_path, ("litellm", "__init__.py"), "_T: Final = TypeVar('_T')\n")
def test_rebinding_a_bare_type_variable_still_trips(tmp_path):
assert "LIT010" in _codes(tmp_path, "_T = TypeVar('_T')\n_T = 1\n")