Skip to content

Commit 431778c

Browse files
DeusDataahundt
andcommitted
fix(extract): a decorator with arguments but no path is not a route
Distilled from DeusData#1245 by Andrew Hundt (a slice of d15071c), with a different rule than upstream chose. Python's unittest.mock is used as a decorator: @patch("subprocess.run") def test_runs(self, run): ... decorator_method_name maps a bare `patch` to the HTTP method PATCH, and try_route_from_decorator_call accepts a receiver-less decorator call. When extract_route_path_from_args then finds nothing path-shaped, the function falls back to "/". So every mock-decorated test function became a Route handler, PATCH "/", and every one of those was wrong. Upstream fixed it with a has_receiver heuristic. That still admits @mock.patch("os.getcwd") (it has a receiver) and would reject the bare framework decorators Litestar and BlackSheep use, @get("/x"). The rule here is about the arguments, not the callee: a decorator call that HAS arguments but none of them path-shaped is not a route. The "/" default survives only for a zero-argument call such as @app.route(), which is the one case where "/" is what the framework means. No test on main relied on the default for a call with non-path arguments; checked before changing it. RED before the fix: extract_python_mock_patch_is_not_route FAIL tests/test_extraction.c:4023: mocked->route_path is not NULL extract_python_bare_decorator_route_rules FAIL tests/test_extraction.c:4063: mocked->route_path is not NULL The second test also pins the two shapes that must keep working: bare @get("/health") still yields a GET route, and zero-argument @app.route() still yields "/" ANY -- those assertions passed before the fix and after. GREEN after: extraction 351 passed; extraction pipeline edge_types_probe route_canon cross_repo infrascan lang_contract 754 passed, with the CALLS-breadth contract at 54 languages / 0 failures. The single remaining red in both runs is extract_spill_round_trip_keeps_every_field refusing to spill onto a disk below its 10 GB floor -- this host, not this change. Co-authored-by: Andrew Hundt <ATHundt@gmail.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 66d9c2b commit 431778c

2 files changed

Lines changed: 81 additions & 1 deletion

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,11 @@ static bool try_drf_action_decorator(CBMArena *a, TSNode dchild, const char *sou
16051605
}
16061606

16071607
// Try to extract a route from a single decorator call node.
1608-
// Returns true if a route method was found (even with fallback path "/").
1608+
// Returns true if a route method was found. The fallback path "/" applies only
1609+
// to a zero-argument call (`@app.route()`): a call that HAS arguments but none
1610+
// path-shaped is not a route -- unittest.mock's `@patch("subprocess.run")`
1611+
// shares its name with the HTTP verb and used to mint a PATCH "/" handler for
1612+
// every mocked test function (distilled from PR #1245).
16091613
static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char *source,
16101614
const char **out_path, const char **out_method) {
16111615
TSNode fn = ts_node_child_by_field_name(dchild, TS_FIELD("function"));
@@ -1630,6 +1634,9 @@ static bool try_route_from_decorator_call(CBMArena *a, TSNode dchild, const char
16301634
*out_method = method;
16311635
return true;
16321636
}
1637+
if (ts_node_named_child_count(args) > 0) {
1638+
return false;
1639+
}
16331640
}
16341641
*out_path = "/";
16351642
*out_method = method;

‎tests/test_extraction.c‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4303,6 +4303,77 @@ TEST(extract_java_method_annotations_issue382) {
43034303
PASS();
43044304
}
43054305

4306+
/* Distilled from PR #1245 (Andrew Hundt): unittest.mock's `@patch("x")` is a
4307+
* bare decorator whose name collides with the HTTP verb, and its only argument
4308+
* is a dotted target, not a path. The "/" fallback then minted a Route handler
4309+
* PATCH "/" for every mocked test function. Rule: a decorator call WITH
4310+
* arguments but no path-shaped one is not a route. */
4311+
TEST(extract_python_mock_patch_is_not_route) {
4312+
CBMFileResult *r = extract("from unittest.mock import patch\n\n"
4313+
"@patch(\"subprocess.run\")\n"
4314+
"def test_cmd(mock_run):\n"
4315+
" pass\n\n"
4316+
"@app.patch(\"/items/{id}\")\n"
4317+
"def update_item():\n"
4318+
" pass\n",
4319+
CBM_LANG_PYTHON, "t", "test_routes.py");
4320+
ASSERT_NOT_NULL(r);
4321+
ASSERT_FALSE(r->has_error);
4322+
4323+
const CBMDefinition *mocked = find_def_by_name(r, "test_cmd");
4324+
ASSERT_NOT_NULL(mocked);
4325+
ASSERT_NULL(mocked->route_path);
4326+
ASSERT_NULL(mocked->route_method);
4327+
4328+
const CBMDefinition *route = find_def_by_name(r, "update_item");
4329+
ASSERT_NOT_NULL(route);
4330+
ASSERT_STR_EQ(route->route_path, "/items/{id}");
4331+
ASSERT_STR_EQ(route->route_method, "PATCH");
4332+
4333+
cbm_free_result(r);
4334+
PASS();
4335+
}
4336+
4337+
/* Companion to the mock-patch case: the narrowing must not cost real routes.
4338+
* A receiver-less framework decorator with a path-shaped argument (Litestar /
4339+
* BlackSheep `@get("/x")`) stays a route, `@mock.patch("x")` (receiver, but
4340+
* no path) is not one, and a zero-argument `@app.route()` keeps the "/"
4341+
* default main emits today. */
4342+
TEST(extract_python_bare_decorator_route_rules) {
4343+
CBMFileResult *r = extract("from litestar import get\n"
4344+
"from unittest import mock\n\n"
4345+
"@get(\"/health\")\n"
4346+
"def health():\n"
4347+
" pass\n\n"
4348+
"@mock.patch(\"os.getcwd\")\n"
4349+
"def test_cwd(mock_cwd):\n"
4350+
" pass\n\n"
4351+
"@app.route()\n"
4352+
"def index():\n"
4353+
" pass\n",
4354+
CBM_LANG_PYTHON, "t", "routes.py");
4355+
ASSERT_NOT_NULL(r);
4356+
ASSERT_FALSE(r->has_error);
4357+
4358+
const CBMDefinition *health = find_def_by_name(r, "health");
4359+
ASSERT_NOT_NULL(health);
4360+
ASSERT_STR_EQ(health->route_path, "/health");
4361+
ASSERT_STR_EQ(health->route_method, "GET");
4362+
4363+
const CBMDefinition *mocked = find_def_by_name(r, "test_cwd");
4364+
ASSERT_NOT_NULL(mocked);
4365+
ASSERT_NULL(mocked->route_path);
4366+
ASSERT_NULL(mocked->route_method);
4367+
4368+
const CBMDefinition *index = find_def_by_name(r, "index");
4369+
ASSERT_NOT_NULL(index);
4370+
ASSERT_STR_EQ(index->route_path, "/");
4371+
ASSERT_STR_EQ(index->route_method, "ANY");
4372+
4373+
cbm_free_result(r);
4374+
PASS();
4375+
}
4376+
43064377
/* ── ArkTS (HarmonyOS .ets) ─────────────────────────────────────── */
43074378

43084379
TEST(arkts_component_struct) {
@@ -8813,6 +8884,8 @@ SUITE(extraction) {
88138884
RUN_TEST(js_index_module_qn_not_collide_with_folder);
88148885
RUN_TEST(python_regular_module_qn_unchanged);
88158886
RUN_TEST(extract_java_method_annotations_issue382);
8887+
RUN_TEST(extract_python_mock_patch_is_not_route);
8888+
RUN_TEST(extract_python_bare_decorator_route_rules);
88168889
RUN_TEST(arkts_component_struct);
88178890
RUN_TEST(arkts_exported_struct_decorators);
88188891
RUN_TEST(arkts_member_decorators);

0 commit comments

Comments
 (0)