bito-code-review[bot] commented on code in PR #44573:
URL: https://github.com/apache/superset/pull/44573#discussion_r4101465325


##########
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:
   <!-- Bito Reply -->
   The suggestion is correct and improves the code by providing an explicit 
type annotation for the `pending` local variable, which aligns with the 
project's coding standards for test files. Applying this change ensures 
consistency with the nearby `models` list and satisfies the requirement for 
explicit annotations.
   
   **tests/unit_tests/mcp_service/test_mcp_tool_registration.py**
   ```
   models: list[type[OmittedMeansUnchanged]] = []
       pending: list[type[OmittedMeansUnchanged]] = [OmittedMeansUnchanged]
   ```



##########
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:
   <!-- Bito Reply -->
   The suggestion to explicitly annotate the local variable `tool` is 
appropriate. It aligns with the repository's typing standards for test files, 
ensuring consistency and clarity even when the type could be inferred.
   
   **tests/unit_tests/mcp_service/test_mcp_tool_registration.py**
   ```
   for name in OMITTED_MEANS_UNCHANGED_TOOLS:
           tool: Any = registered.get(name)
   ```



-- 
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]

Reply via email to