From c41d366152cee4bfd57cc436e8ea93b42a27cc39 Mon Sep 17 00:00:00 2001 From: NiJingzhe <1448976235@qq.com> Date: Wed, 4 Nov 2026 05:01:45 +0800 Subject: [PATCH] catalog: a union of spellings of one value has that value's kind (Sequence[Edge] | ShapeSelector, Literal[...] | Enum, Solid | Sequence[Solid]); str | PathLike is a path Co-Authored-By: Claude Opus 5.5 --- src/simplecadapi/catalog.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/simplecadapi/catalog.py b/src/simplecadapi/catalog.py index a1d4fd6d..4bde9b81 100644 --- a/src/simplecadapi/catalog.py +++ b/src/simplecadapi/catalog.py @@ -13,7 +13,7 @@ A parameter's ``kind`` is one of: the meanings of :mod:`simplecadapi.types` (``l ``direction3``, ``vector3``, ``count``), ``number`` (a unitless ``float | Expr``), ``int``, ``bool``, ``text``, ``enum`` (``Literal[...]``, with ``choices``), a value type (``solid``, ``face``, ``wire``, ``edge``, ``vertex``, ``shell``, ``shape``, ``sketch``, ``part``, ``assembly``, ...), ``query`` (a ``ql`` selection), ``points`` / ``numbers`` (sequences), -or ``unknown`` when the signature says no more than ``object``. ``many`` marks a sequence of the kind, ``optional`` a +``path`` (a file path), or ``unknown`` when the signature says no more than ``object``. ``many`` marks a sequence of the kind, ``optional`` a ``None`` default. """ @@ -162,9 +162,35 @@ def classify(type_text: str) -> dict[str, Any]: if None not in kinds: kind = kinds.pop() if len(kinds) == 1 else "shape" if kinds <= {"solid", "face", "wire", "edge", "vertex", "shell", "compound", "shape"} else "|".join(sorted(kinds)) return {"kind": kind, "many": False, "optional": optional, "choices": ()} + if names in (["str", "PathLike[str]"], ["str", "PathLike"]): + return {"kind": "path", "many": False, "optional": optional, "choices": ()} + if len(names) > 1: + return _classify_union(names, optional) return {"kind": "unknown", "many": False, "optional": optional, "choices": ()} +def _classify_union(names: list[str], optional: bool) -> dict[str, Any]: + """A union of different spellings of one value: ``Literal[...] | SomeEnum`` (the choices), ``Sequence[Edge] | Query`` + (edges, or a ``ql`` query that finds them), ``Solid | Sequence[Solid]``; or of different kinds (``Wire | Direction3``).""" + parts = [classify(n) for n in names] + literal = next((p for p in parts if p["kind"] == "enum"), None) + if literal is not None: # the other members are enum classes of the same values + return {"kind": "enum", "many": False, "optional": optional, "choices": literal["choices"]} + if any(p["kind"] == "unknown" for p in parts): + return {"kind": "unknown", "many": False, "optional": optional, "choices": ()} + many = any(p["many"] for p in parts) + kinds = {p["kind"] for p in parts} + if many and len(kinds) > 1: + kinds.discard("query") + if len(kinds) == 1: + kind = kinds.pop() + elif kinds <= {"solid", "face", "wire", "edge", "vertex", "shell", "compound", "shape"}: + kind = "shape" + else: + kind = "|".join(sorted(kinds)) + return {"kind": kind, "many": many, "optional": optional, "choices": ()} + + def _returns(type_text: str) -> str: info = classify(type_text) if info["many"]: