gabriel / musehub public
test_template_response_call_shape.py python
45 lines 1.8 KB
Raw
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ breaking 1 day ago
1 """Every ``TemplateResponse`` call passes ``request`` first (musehub#119).
2
3 Starlette's ``TemplateResponse(request, name, context)`` takes the request positionally. The old
4 ``TemplateResponse(name, context)`` order put the template name in the request slot and the context
5 dict in the name slot, which surfaced as ``TypeError: unhashable type: 'dict'`` deep inside Jinja's
6 loader cache and made every non-empty ``/explore/search`` query return 500.
7 """
8
9 from __future__ import annotations
10
11 import ast
12 from pathlib import Path
13
14 import pytest
15
16 pytestmark = pytest.mark.no_db
17
18 ROOT = Path(__file__).resolve().parents[1] / "musehub"
19
20
21 def bad_template_response_calls(source: str) -> list[int]:
22 """Line numbers of ``*.TemplateResponse(...)`` calls whose first positional argument is not a Name."""
23 bad = []
24 for node in ast.walk(ast.parse(source)):
25 if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == "TemplateResponse":
26 first = node.args[0] if node.args else next((k.value for k in node.keywords if k.arg == "request"), None)
27 if not isinstance(first, ast.Name):
28 bad.append(node.lineno)
29 return bad
30
31
32 def test_every_template_response_call_passes_request_first() -> None:
33 offenders = {
34 str(path.relative_to(ROOT)): lines
35 for path in sorted(ROOT.rglob("*.py"))
36 if (lines := bad_template_response_calls(path.read_text()))
37 }
38 assert offenders == {}
39
40
41 def test_the_guard_flags_the_old_argument_order() -> None:
42 broken = 'templates.TemplateResponse("musehub/fragments/x.html", ctx)\n'
43 fixed = 'templates.TemplateResponse(request, "musehub/fragments/x.html", ctx)\n'
44 assert bad_template_response_calls(broken) == [1]
45 assert bad_template_response_calls(fixed) == []
File History 3 commits
sha256:f19d5e4a89766fc91b926cdf801255e12dcd968041c0ece8adf6627760788d22 merge: next build — MCP (#291 #292 #295 #324), profiles/sit… Opus 5.5 minor ⚠ 1 day ago
sha256:7a1f4dfd571e937cb1032574d5c0965ba04ad520adcf523dec123d7a34746236 merge: rc26 — profiles/site wave (#119 #301 #303 #307 #314;… Opus 5.5 minor ⚠ 1 day ago
sha256:83400c1ee7368a7d56cf68a7e1e53fe4ea616683c97afaa9960ce2b69e1d5cc4 fix(explore): /explore/search renders instead of 500 (#119) Opus 5.5 patch 2 days ago