bito-code-review[bot] commented on code in PR #44573:
URL: https://github.com/apache/superset/pull/44573#discussion_r4090386851
##########
tests/unit_tests/mcp_service/test_mcp_tool_registration.py:
##########
@@ -249,6 +250,96 @@ def _run(coro):
return asyncio.run(coro)
+# Tools whose request model tells "field omitted" from "field set to null":
+# omitting leaves the stored value alone, an explicit null is deliberate input.
+# Their optional fields must not advertise a default, or a client that
+# materialises defaults sends nulls for everything the caller never named.
+OMITTED_MEANS_UNCHANGED_TOOLS = (
+ "update_chart",
+ "update_dashboard",
+ "update_dataset_metric",
+)
+
+
+def _request_model_schema(tool: Any) -> dict[str, Any]:
+ """Return the JSON Schema of a tool's ``request`` argument."""
+ schema = tool.parameters or {}
+ request = schema.get("properties", {}).get("request", {})
+ for candidate in (request, *request.get("allOf", [])):
+ if reference := candidate.get("$ref"):
+ name = reference.rpartition("/")[2]
+ for container in ("$defs", "definitions"):
+ if name in schema.get(container, {}):
+ return schema[container][name]
+ pytest.fail(
+ f"{tool.name}: cannot resolve request schema reference
{reference!r}"
+ )
+ if "properties" in request:
+ return request
+ pytest.fail(
+ f"{tool.name}: unrecognised request schema shape {sorted(request)}; "
+ "the null-default check below would silently pass"
+ )
+
+
+def _null_defaults(schema: dict[str, Any]) -> list[str]:
+ """Return the fields of one schema that advertise null as their default."""
+ return sorted(
+ field
+ for field, spec in schema.get("properties", {}).items()
+ if "default" in spec and spec["default"] is None
+ )
+
+
+def _omitted_means_unchanged_models() -> list[type[OmittedMeansUnchanged]]:
+ """Return every model built on ``OmittedMeansUnchanged``."""
+ models: list[type[OmittedMeansUnchanged]] = []
+ pending = [OmittedMeansUnchanged]
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Untyped local pending</b></div>
<div id="fix">
`_omitted_means_unchanged_models` leaves the `pending` work-list
unannotated. BITO.md rule 13153 requires explicit annotations for all locals in
test files, even when inferable; the sibling `models` list on line 296 is
annotated, so this is inconsistent. Suggest `pending:
list[type[OmittedMeansUnchanged]] = [OmittedMeansUnchanged]`.
</div>
</div>
<small><i>Code Review Run #04c8d2</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
##########
tests/unit_tests/mcp_service/test_mcp_tool_registration.py:
##########
@@ -249,6 +250,96 @@ def _run(coro):
return asyncio.run(coro)
+# Tools whose request model tells "field omitted" from "field set to null":
+# omitting leaves the stored value alone, an explicit null is deliberate input.
+# Their optional fields must not advertise a default, or a client that
+# materialises defaults sends nulls for everything the caller never named.
+OMITTED_MEANS_UNCHANGED_TOOLS = (
+ "update_chart",
+ "update_dashboard",
+ "update_dataset_metric",
+)
+
+
+def _request_model_schema(tool: Any) -> dict[str, Any]:
+ """Return the JSON Schema of a tool's ``request`` argument."""
+ schema = tool.parameters or {}
+ request = schema.get("properties", {}).get("request", {})
+ for candidate in (request, *request.get("allOf", [])):
+ if reference := candidate.get("$ref"):
+ name = reference.rpartition("/")[2]
+ for container in ("$defs", "definitions"):
+ if name in schema.get(container, {}):
+ return schema[container][name]
+ pytest.fail(
+ f"{tool.name}: cannot resolve request schema reference
{reference!r}"
+ )
+ if "properties" in request:
+ return request
+ pytest.fail(
+ f"{tool.name}: unrecognised request schema shape {sorted(request)}; "
+ "the null-default check below would silently pass"
+ )
+
+
+def _null_defaults(schema: dict[str, Any]) -> list[str]:
+ """Return the fields of one schema that advertise null as their default."""
+ return sorted(
+ field
+ for field, spec in schema.get("properties", {}).items()
+ if "default" in spec and spec["default"] is None
+ )
+
+
+def _omitted_means_unchanged_models() -> list[type[OmittedMeansUnchanged]]:
+ """Return every model built on ``OmittedMeansUnchanged``."""
+ models: list[type[OmittedMeansUnchanged]] = []
+ pending = [OmittedMeansUnchanged]
+ while pending:
+ for subclass in pending.pop().__subclasses__():
+ if subclass not in models:
+ models.append(subclass)
+ pending.append(subclass)
+ return models
+
+
+def test_partial_update_tools_advertise_no_null_default() -> None:
+ """No optional field of a partial-update tool offers null as its
default."""
+ registered = {tool.name: tool for tool in _run(mcp.list_tools())}
+ advertised = {}
+ for name in OMITTED_MEANS_UNCHANGED_TOOLS:
+ tool = registered.get(name)
Review Comment:
<div>
<div id="suggestion">
<div id="issue"><b>Untyped local tool</b></div>
<div id="fix">
`tool = registered.get(name)` is an unannotated local on a changed line.
BITO.md rule 13153 asks for explicit annotations on all locals in test files
even when inferable. `Any` is already imported, so `tool: Any =
registered.get(name)` keeps the new fail-loud guard while satisfying the repo
typing rule.
</div>
</div>
<small><i>Code Review Run #04c8d2</i></small>
</div>
---
Should Bito avoid suggestions like this for future reviews? (<a
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
- [ ] Yes, avoid them
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]