Skip to content

Commit 5fbf497

Browse files
committed
clean up duplicated code
1 parent 06eb37d commit 5fbf497

2 files changed

Lines changed: 10 additions & 28 deletions

File tree

dash/mcp/primitives/tools/input_schemas/input_descriptions/description_pattern_matching.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@
77

88
from __future__ import annotations
99

10-
import json
11-
12-
from dash.dependencies import Wildcard
10+
from dash._layout_utils import _WILDCARD_VALUES, parse_wildcard_id
1311
from dash.mcp.types import MCPInput
1412

1513
from .base import InputDescriptionSource
1614

17-
_WILDCARD_VALUES = frozenset(w.value for w in Wildcard)
18-
1915

2016
class PatternMatchingDescription(InputDescriptionSource):
2117
"""Describe pattern-matching behavior for wildcard inputs."""
2218

2319
@classmethod
2420
def describe(cls, param: MCPInput) -> list[str]:
25-
dep_id = _parse_dep_id(param["component_id"])
21+
dep_id = parse_wildcard_id(param["component_id"])
2622
if dep_id is None:
2723
return []
2824

@@ -56,15 +52,6 @@ def describe(cls, param: MCPInput) -> list[str]:
5652
return [desc] if desc else []
5753

5854

59-
def _parse_dep_id(component_id: str) -> dict | None:
60-
if not component_id.startswith("{"):
61-
return None
62-
try:
63-
return json.loads(component_id)
64-
except (json.JSONDecodeError, ValueError):
65-
return None
66-
67-
6855
def _find_wildcard(dep_id: dict) -> tuple[str | None, str | None]:
6956
"""Return (key, wildcard_type) for the first wildcard found."""
7057
for key, value in dep_id.items():

dash/mcp/primitives/tools/input_schemas/schema_pattern_matching.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
from __future__ import annotations
1010

11-
import json
1211
from typing import Any
1312

14-
from dash._layout_utils import find_matching_components, _WILDCARD_VALUES
13+
from dash._layout_utils import (
14+
_WILDCARD_VALUES,
15+
find_matching_components,
16+
parse_wildcard_id,
17+
)
1518
from dash.mcp.types import MCPInput
1619

1720
from .base import InputSchemaSource
@@ -26,7 +29,7 @@ class PatternMatchingSchema(InputSchemaSource):
2629

2730
@classmethod
2831
def get_schema(cls, param: MCPInput) -> dict[str, Any] | None:
29-
dep_id = _parse_dep_id(param["component_id"])
32+
dep_id = parse_wildcard_id(param["component_id"])
3033
if dep_id is None:
3134
return None
3235

@@ -52,15 +55,6 @@ def get_schema(cls, param: MCPInput) -> dict[str, Any] | None:
5255
return {"type": "array", "items": item_schema}
5356

5457

55-
def _parse_dep_id(component_id: str) -> dict | None:
56-
if not component_id.startswith("{"):
57-
return None
58-
try:
59-
return json.loads(component_id)
60-
except (json.JSONDecodeError, ValueError):
61-
return None
62-
63-
6458
def _get_wildcard_type(dep_id: dict) -> str | None:
6559
"""Return the wildcard type (ALL, MATCH, ALLSMALLER) or None."""
6660
for value in dep_id.values():
@@ -72,10 +66,11 @@ def _get_wildcard_type(dep_id: dict) -> str | None:
7266

7367
def _infer_value_schema(param: MCPInput) -> dict[str, Any] | None:
7468
"""Infer the JSON Schema for the ``value`` field from a matching component."""
75-
matches = find_matching_components(_parse_dep_id(param["component_id"]))
69+
matches = find_matching_components(parse_wildcard_id(param["component_id"]))
7670
if not matches:
7771
return None
7872

73+
# pylint: disable-next=cyclic-import,import-outside-toplevel
7974
from . import get_input_schema
8075

8176
concrete_param: MCPInput = {

0 commit comments

Comments
 (0)