From 970022aca3310d7498fe643277ee33a997310bc8 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:13:43 -0800 Subject: [PATCH] fix(typing): count only the probe errors in the Router stub guard and flag Final type variables everywhere --- scripts/check_type_discipline.py | 29 ++++++++++++------- .../check_router_constructor_stub.py | 5 ++-- tests/unit/test_check_type_discipline.py | 8 +++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/scripts/check_type_discipline.py b/scripts/check_type_discipline.py index 2bf9d209e0d..ea86c19b4de 100644 --- a/scripts/check_type_discipline.py +++ b/scripts/check_type_discipline.py @@ -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), diff --git a/tests/code_coverage_tests/check_router_constructor_stub.py b/tests/code_coverage_tests/check_router_constructor_stub.py index 85e5a43c0e7..8ab8b82e695 100644 --- a/tests/code_coverage_tests/check_router_constructor_stub.py +++ b/tests/code_coverage_tests/check_router_constructor_stub.py @@ -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 diff --git a/tests/unit/test_check_type_discipline.py b/tests/unit/test_check_type_discipline.py index 78f5bde5249..e4240d8c210 100644 --- a/tests/unit/test_check_type_discipline.py +++ b/tests/unit/test_check_type_discipline.py @@ -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")