diff --git a/pyegeria/view/analytic_registry.py b/pyegeria/view/analytic_registry.py index c868562b..8982552f 100644 --- a/pyegeria/view/analytic_registry.py +++ b/pyegeria/view/analytic_registry.py @@ -45,13 +45,24 @@ from typing import Any, Dict, List, Literal, Optional, Tuple, Union from loguru import logger -from pydantic import BaseModel, Field, field_validator, model_validator +from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator __all__ = [ 'AnalyticParam', 'AnalyticFunctionSpec', 'AnalyticActionSpec', 'AnalyticDeclaration', + 'RoutineDeclaration', + 'ParamDeclaration', + 'Binding', + 'TileSafety', + 'Admission', + 'ROUTINE_KINDS', + 'BINDING_KINDS', + 'PARAM_CATEGORIES', + 'INPUT_ROLES', + 'get_routine_registry', + 'routine_declaration_errors', 'MeasureDeclaration', 'BasisDeclaration', 'AbsenceSentences', @@ -94,6 +105,8 @@ class AnalyticParam(BaseModel): required: bool = False default: Optional[Any] = None description: str = "" + #: What the parameter changes (PARAM_CATEGORIES): content, shape or performance (RS-W2-2). + category: Literal["content", "shape", "performance"] = "content" class AnalyticActionSpec(BaseModel): @@ -375,15 +388,32 @@ class AnalyticDeclaration(BaseModel): """What an analytic function declares about its results: the minimum is `result_kind`, `measures` (required for measure/series/distribution/graph) and `absence`. - `out` lists the result's fields (for a list or series, the fields of one row). `categories` - belongs to a distribution or graph and `listing` to a list, as in trellis-analytics; this model - accepts a declaration without them, and the built-ins all carry them.""" + `out` says the result's fields (for a list or series, the fields of one row) in one of the + library's three forms: a list of fields (every pyegeria built-in), a registered shape's name, or + an inline shape (`{"fields": [...], "extends": ...}`, kept as data); None or [] when not declared. Read it through + `out_fields()`, never by iterating `out`, so a library declaration reads the same way (RS-W2-1a + read-back F2). `categories` belongs to a distribution or graph and `listing` to a list, as in + trellis-analytics; this model accepts a declaration without them, and the built-ins all carry + them.""" result_kind: str measures: List[MeasureDeclaration] = Field(default_factory=list) absence: AbsenceSentences categories: Optional[CategoriesDeclaration] = None listing: Optional[ListDeclaration] = None - out: List[FieldDeclaration] = Field(default_factory=list) + out: Optional[Union[List[FieldDeclaration], str, Dict[str, Any]]] = Field(default_factory=list) + + def out_fields(self) -> List[FieldDeclaration]: + """The result's fields, whichever form `out` takes. A shape name pyegeria does not hold in + ROW_SHAPES (one only the library registers) and an inline shape's reused fields (`extends`, + `include`) are not resolved here, so they read as []; the resolver (RS-W2-2 PR-B) says why.""" + if self.out is None: # the library's word for "not declared" + return [] + if isinstance(self.out, list): + return list(self.out) + if isinstance(self.out, str): + return list(ROW_SHAPES.get(self.out, [])) + return [FieldDeclaration.model_validate(f) for f in self.out.get("fields", []) + if isinstance(f, dict) and "include" not in f] @field_validator("result_kind") @classmethod @@ -399,6 +429,177 @@ def _measured_kinds_declare_measures(self) -> "AnalyticDeclaration": return self +# ── Routine declaration and bindings (RS-W2-2 PR-A; note §2b), mirrored from trellis-analytics 0.4 ── +# A registry entry is a declaration (data: what the routine is, its inputs, parameters, constants and +# results) plus one or more bindings (how it is reached). The library authors the schema in +# `trellis_analytics.routine` (owner decision D2); pyegeria keeps the same names, words and rules +# without importing it, and tests/micro-tests/test_routine_declarations_match_library.py loads every +# built-in here in the library when it is installed. `AnalyticFunctionSpec` below is the compatibility +# view: its `function` is the one python binding. + +#: What a declaration declares: a stage of a micro-flow (producer, processor, routine) or a step (a +#: routine deployed to an engine host). +ROUTINE_KINDS = ("producer", "processor", "routine", "step") + +#: How a routine is reached. `python`: an importable `module.attr` path; `egeria_query`: a saved Egeria +#: query by name; `sql`: a named query a reader owns; `step`: a step's registered name. +BINDING_KINDS = ("python", "egeria_query", "sql", "step") + +#: What a parameter changes: what is counted (`content`), how the result is laid out (`shape`), or +#: only how fast or how much is read (`performance`, which never changes a value). +PARAM_CATEGORIES = ("content", "shape", "performance") + +#: The role words a routine's `inputs` use. `egeria_client` and `reader` take a qualifier. +INPUT_ROLES = ("egeria_client", "reader", "viewer", "subject") + +_INPUT_ROLE = re.compile(r"^(?:egeria_client:[A-Za-z_][A-Za-z0-9_]*|reader:[a-z_][a-z0-9_]*|viewer|subject)$") +_PYTHON_TARGET = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)+$") + + +class _StrictModel(BaseModel): + # An unknown key is refused, so a misspelt field is named rather than silently dropped. + model_config = ConfigDict(extra="forbid") + + +class Binding(_StrictModel): + """How a routine is reached: one of BINDING_KINDS and its target. A python target is a dotted + import path; the module allow-list (`format_set_executor.python_binding_allowed`) applies to it, + and only to it: a report spec names routines, never targets (ISSUE-173).""" + kind: Literal["python", "egeria_query", "sql", "step"] + target: str + + @model_validator(mode="after") + def _target(self) -> "Binding": + if not _words(self.target): + raise ValueError(f"a {self.kind} binding names no target") + if self.kind == "python" and not _PYTHON_TARGET.fullmatch(self.target): + raise ValueError(f"python binding target '{self.target}' is not a dotted import path (module.attr)") + return self + + +class ParamDeclaration(_StrictModel): + """One parameter a caller may set. Either it has a default or it is required, never both.""" + name: str + type: str = "str" + default: Any = None + required: bool = False + category: Literal["content", "shape", "performance"] = "content" + description: str = "" + + @model_validator(mode="after") + def _shape(self) -> "ParamDeclaration": + if not re.fullmatch(_FIELD_NAME, self.name or ""): + raise ValueError(f"parameter name '{self.name}' is not an identifier") + if self.required and self.default is not None: + raise ValueError(f"parameter '{self.name}' is required and has a default: a required parameter has none") + return self + + +class TileSafety(_StrictModel): + """May the routine run on a tile's request path? `safe` with no `cap` is a yes at any size; a + `cap` (param -> value) is a yes only within it. The reason is the evidence and is always said.""" + safe: bool + cap: Dict[str, Any] = Field(default_factory=dict) + reason: str + + @model_validator(mode="after") + def _reason(self) -> "TileSafety": + if not _words(self.reason): + raise ValueError("tile_safe declares no reason: say the evidence (a timing, a cost class)") + if self.cap and not self.safe: + raise ValueError("tile_safe declares a cap and safe false: a cap says within what it IS safe") + return self + + +class Admission(_StrictModel): + """Where the routine may be offered: the resource types it is about ([] = any), and whether only + in tests.""" + resource_types: List[str] = Field(default_factory=list) + test_only: bool = False + + +#: A processor's input shape (`in`): a list of fields, a registered shape's name, or an inline shape. +ShapeRef = Union[List[FieldDeclaration], str, Dict[str, Any]] + + +class RoutineDeclaration(_StrictModel): + """What a routine says about how it is called and what it returns (note §2b). + + Only `name` is required. `declaration` is the results (`AnalyticDeclaration`: result kind, + measures, absence sentences, `out`); `out` lives there, not here, so a result's fields are + declared once. `in` (attribute `in_`) is the shape a processor reads. `constants` are values + fixed for every call; a constant is never also a parameter. At most one python binding. + + A shape name in `in` is not checked against ROW_SHAPES here (the library may register shapes + pyegeria does not hold); the resolver checks it (RS-W2-2 PR-B, check 3).""" + name: str + kind: Literal["producer", "processor", "routine", "step"] = "routine" + description: str = "" + version: str = "" + source: str = "" + family: str = "" + tags: List[str] = Field(default_factory=list) + generic: bool = False + binding_note: str = "" + inputs: List[str] = Field(default_factory=list) + params: List[ParamDeclaration] = Field(default_factory=list) + constants: Dict[str, Any] = Field(default_factory=dict) + in_: Optional[ShapeRef] = Field(default=None, alias="in") + declaration: Optional[AnalyticDeclaration] = None + pushdown: List[str] = Field(default_factory=list) + tile_safe: Optional[TileSafety] = None + admission: Admission = Field(default_factory=Admission) + bindings: List[Binding] = Field(default_factory=list) + annotation_type: str = "" + + model_config = ConfigDict(extra="forbid", populate_by_name=True) + + @model_validator(mode="after") + def _whole(self) -> "RoutineDeclaration": + if not re.fullmatch(_FIELD_NAME, self.name or ""): + raise ValueError(f"routine name '{self.name}' is not an identifier") + label = f"routine '{self.name}'" + bad = [i for i in self.inputs if not _INPUT_ROLE.fullmatch(i or "")] + if bad: + raise ValueError(f"{label} input {', '.join(repr(b) for b in bad)} is not a role word " + "(egeria_client:, reader:, viewer, subject)") + if len(set(self.inputs)) != len(self.inputs): + raise ValueError(f"{label} declares an input more than once") + names = [p.name for p in self.params] + twice = sorted({n for n in names if names.count(n) > 1}) + if twice: + raise ValueError(f"{label} declares parameter {', '.join(twice)} more than once") + clash = sorted(set(names) & set(self.constants)) + if clash: + raise ValueError(f"{label} declares {', '.join(clash)} as a parameter and a constant: a constant is " + "fixed for every call, so it is not a parameter") + if sum(1 for b in self.bindings if b.kind == "python") > 1: + raise ValueError(f"{label} declares more than one python binding: one routine, one function") + if self.in_ is not None and self.kind != "processor": + raise ValueError(f"{label} declares in and is a '{self.kind}': only a processor reads a previous " + "stage's shape; a routine or producer names its inputs") + if self.generic and _words(self.binding_note): + raise ValueError(f"{label} is generic and declares a binding_note: the note names what is fixed, " + "and a generic routine fixes nothing") + return self + + def python_target(self) -> str: + """The python binding's target, or "" when it has none.""" + return next((b.target for b in self.bindings if b.kind == "python"), "") + + def out_fields(self) -> List[FieldDeclaration]: + """The result's fields (`declaration.out`); [] when not declared.""" + return self.declaration.out_fields() if self.declaration is not None else [] + + +def _as_analytic_declaration(decl: Any) -> Optional[AnalyticDeclaration]: + """A declaration as pyegeria's model. A subclass's richer declaration (trellis-analytics' + `Declaration`) is read through its dump; the blocks pyegeria does not model are left out.""" + if decl is None or isinstance(decl, AnalyticDeclaration): + return decl + return AnalyticDeclaration.model_validate(decl.model_dump() if isinstance(decl, BaseModel) else decl) + + class AnalyticFunctionSpec(BaseModel): """One entry in the analytic function registry. @@ -422,7 +623,10 @@ class AnalyticFunctionSpec(BaseModel): existing consumer that only reads `.function` -- including every other entry in this registry -- keeps working completely unchanged.""" name: str - function: str # dotted import path, e.g. "pyegeria.view.overview_metrics.growth_series" + # The one python binding's target (a dotted import path, e.g. + # "pyegeria.view.overview_metrics.growth_series"). "" only for an entry whose bindings has no + # python binding; this pyegeria runs python bindings only, and refuses such an entry by name. + function: str = "" description: str = "" returns: str = "" # human-readable result shape, e.g. "scalar (int)", "list[dict] (time series)" generic: bool = False @@ -433,7 +637,8 @@ class AnalyticFunctionSpec(BaseModel): # Manifest fields (ISSUE-149), all optional so every existing entry is unchanged. # trellis-analytics' RoutineManifest/StepManifest subclass this class and used to # carry these themselves. - kind: Literal["routine", "step"] = "routine" # routine: runs in the caller; step: a survey-style step + # routine: runs in the caller; step: a survey-style step; producer/processor: a stage (RS-W2-2) + kind: Literal["producer", "processor", "routine", "step"] = "routine" source: str = "" # where the data comes from; known values in KNOWN_ANALYTIC_SOURCES version: str = "" # the producer's version, stamped on every result envelope: bool = False # the result carries a state envelope (measured / nothing_found / ...) @@ -445,6 +650,48 @@ class AnalyticFunctionSpec(BaseModel): # sentences. Optional; trellis-analytics' StepManifest narrows it to its fuller Declaration. declaration: Optional[AnalyticDeclaration] = None + # The routine declaration's own fields (RS-W2-2 PR-A), so this view carries a RoutineDeclaration + # whole: `routine()` and `from_routine()` round-trip. All optional; no existing entry sets them. + bindings: List[Binding] = Field(default_factory=list) # every binding; the python one is `function` + inputs: List[str] = Field(default_factory=list) # role words (INPUT_ROLES) + constants: Dict[str, Any] = Field(default_factory=dict) + in_: Optional[ShapeRef] = Field(default=None, alias="in") + pushdown: List[str] = Field(default_factory=list) + tile_safe: Optional[TileSafety] = None + test_only: bool = False # with resource_types, the routine's Admission + + model_config = ConfigDict(populate_by_name=True) + + @model_validator(mode="before") + @classmethod + def _function_is_the_python_binding(cls, data: Any) -> Any: + """`function` and the python binding are one value: either fills the other; two that differ, + two python bindings, or no binding at all are refused.""" + if not isinstance(data, dict): + return data + data = dict(data) + label = f"analytic function '{data.get('name', '')}'" + function = data.get("function") or "" + bindings = list(data.get("bindings") or []) + + def _get(b: Any, key: str) -> Any: + return b.get(key) if isinstance(b, dict) else getattr(b, key, None) + + python = [_get(b, "target") for b in bindings if _get(b, "kind") == "python"] + if len(python) > 1: + raise ValueError(f"{label} declares more than one python binding: one routine, one function") + if function and not python: + bindings.insert(0, {"kind": "python", "target": function}) + elif python and not function: + data["function"] = python[0] + elif python and python[0] != function: + raise ValueError(f"{label} names function '{function}' and python binding '{python[0]}': they are " + "one value, so name it once") + if not bindings: + raise ValueError(f"{label} declares no binding: name its function (a python binding) or a binding") + data["bindings"] = bindings + return data + @field_validator("source") @classmethod def _suggest_known_source(cls, value: str) -> str: @@ -454,6 +701,48 @@ def _suggest_known_source(cls, value: str) -> str: f"{sorted(KNOWN_ANALYTIC_SOURCES)}; keeping it.") return value + def python_target(self) -> str: + """The python binding's target ("" when the entry has none).""" + return next((b.target for b in self.bindings if b.kind == "python"), "") + + def routine(self) -> RoutineDeclaration: + """This entry as a RoutineDeclaration (the declaration + bindings it is a view of). Raises + ValueError, in words, when the entry breaks a declaration rule; `get_routine_registry` + records that instead of raising.""" + return RoutineDeclaration( + name=self.name, kind=self.kind, description=self.description, version=self.version, + source=self.source, family=self.family, tags=list(self.tags), generic=self.generic, + binding_note=self.binding_note, inputs=list(self.inputs), + params=[ParamDeclaration(name=p.name, type=p.type, default=p.default, required=p.required, + category=p.category, description=p.description) for p in self.params], + constants=dict(self.constants), in_=self.in_, + declaration=_as_analytic_declaration(self.declaration), pushdown=list(self.pushdown), + tile_safe=self.tile_safe, + admission=Admission(resource_types=list(self.resource_types), test_only=self.test_only), + bindings=list(self.bindings), + annotation_type=self.annotation_type if self.kind == "step" else "", + ) + + @classmethod + def from_routine(cls, routine: RoutineDeclaration, *, returns: str = "", + action: Optional[AnalyticActionSpec] = None, envelope: bool = False, + **extra: Any) -> "AnalyticFunctionSpec": + """The compatibility view of a RoutineDeclaration. `returns`, `action` and `envelope` are + the view's own fields, which a declaration does not carry (its results are in + `declaration`). `extra` is passed on, for a subclass's own fields.""" + r = routine + return cls( + name=r.name, function=r.python_target(), description=r.description, returns=returns, + generic=r.generic, binding_note=r.binding_note, + params=[AnalyticParam(name=p.name, type=p.type, required=p.required, default=p.default, + description=p.description, category=p.category) for p in r.params], + action=action, kind=r.kind, source=r.source, version=r.version, envelope=envelope, + annotation_type=r.annotation_type, resource_types=list(r.admission.resource_types), + family=r.family, tags=list(r.tags), declaration=r.declaration, bindings=list(r.bindings), + inputs=list(r.inputs), constants=dict(r.constants), in_=r.in_, pushdown=list(r.pushdown), + tile_safe=r.tile_safe, test_only=r.admission.test_only, **extra, + ) + class AnalyticFunctionDict(Dict[str, AnalyticFunctionSpec]): """Name -> AnalyticFunctionSpec, mirroring FormatSetDict/DashboardSheetDict.""" @@ -1028,8 +1317,9 @@ def _always_applies(what: str) -> str: "(a Vega-Lite line chart) is proven against today. " "Cost: one count per series per snapshot (four series by default), seconds to tens of seconds.", returns="list[dict] (time series: {label, date, : count, ...})", + # Generic: what it counts is type_map. (Its binding_note used to repeat type_map's own + # description; a generic routine fixes nothing, so it declares no note -- RS-W2-2.) generic=True, - binding_note="type_map defaults to assets/terms/governed/products but is fully overridable.", params=[ AnalyticParam(name="window", type="str", default="6mo", description="8h|1d|3d|7d|30d|90d|6mo|1y -- sets span + default point count."), @@ -1779,6 +2069,33 @@ def register_analytic_functions(new_functions: Union[AnalyticFunctionDict, dict] _RUNTIME_ANALYTIC_FUNCTIONS[k] = v +def get_routine_registry() -> Dict[str, RoutineDeclaration]: + """Every registry entry (built-in, config, installed, runtime) as a RoutineDeclaration: the + declaration + bindings each `AnalyticFunctionSpec` is a view of. An entry that breaks a + declaration rule is left out and named, with the rule's words, by `routine_declaration_errors()`; + it still runs as before (RS-W2-2 PR-A changes no behaviour).""" + routines, _ = _routines_and_errors() + return routines + + +def routine_declaration_errors() -> Dict[str, str]: + """Registry entries that cannot be read as a RoutineDeclaration, each with a sentence naming the + entry and the rule it breaks (empty when every entry reads).""" + _, errors = _routines_and_errors() + return errors + + +def _routines_and_errors() -> Tuple[Dict[str, RoutineDeclaration], Dict[str, str]]: + routines: Dict[str, RoutineDeclaration] = {} + errors: Dict[str, str] = {} + for name, spec in get_analytic_registry().items(): + try: + routines[name] = spec.routine() + except ValueError as exc: # pydantic's ValidationError is a ValueError + errors[name] = f"analytic function '{name}' is not a valid routine declaration: {exc}" + return routines, errors + + def unregister_analytic_function(name: str) -> bool: return bool(_RUNTIME_ANALYTIC_FUNCTIONS.pop(name, None)) diff --git a/pyegeria/view/format_set_executor.py b/pyegeria/view/format_set_executor.py index f31bf271..4e60213d 100644 --- a/pyegeria/view/format_set_executor.py +++ b/pyegeria/view/format_set_executor.py @@ -111,6 +111,25 @@ def _module_allowed(module_path: str) -> bool: return any(module_path == p or module_path.startswith(p + ".") for p in analytic_module_prefixes()) +def python_binding_allowed(target: str) -> bool: + """Is a python binding's target under an allowed module prefix? The allow-list applies to + binding targets only: a report spec names routines by registered name and never carries a + target (ISSUE-173, RS-W2-2).""" + return isinstance(target, str) and "." in target and _module_allowed(target.rsplit(".", 1)[0]) + + +def _python_binding_of(spec) -> str: + """A registered entry's python binding target, or AnalyticFunctionRefused naming the bindings + it has instead (this pyegeria runs python bindings only).""" + target = spec.python_target() if hasattr(spec, "python_target") else spec.function + if not target: + kinds = ", ".join(f"{b.kind} '{b.target}'" for b in getattr(spec, "bindings", [])) or "none" + raise AnalyticFunctionRefused( + f"Analytic function '{spec.name}' has no python binding (its bindings: {kinds}); this pyegeria " + f"runs python bindings only.") + return target + + def _import_callable(func_decl: str): module_path, func_name = func_decl.rsplit(".", 1) module = importlib.import_module(module_path) @@ -131,11 +150,15 @@ def _resolve_analytic_function(func_decl: str): class to resolve here, just the function itself. A report spec is data, so it may not import arbitrary code (ISSUE-169): - 1. A registered name (analytic_registry) resolves to that entry's function. + 1. A registered name (analytic_registry) resolves to that entry's python + binding; an entry with none is refused by name. 2. A dotted path some registered entry declares is trusted as-is. 3. Any other dotted path must sit under an allowed module prefix - (`analytic_module_prefixes()`); anything else raises + (`python_binding_allowed`); anything else raises AnalyticFunctionRefused before any import happens. + Cases 2 and 3 are binding targets (an action's fetch/analytic step, a binder + factory): a report spec never reaches them, because + `resolve_report_spec_function` accepts registered names only (ISSUE-173). """ if not isinstance(func_decl, str) or not func_decl.strip(): raise ValueError(f"Invalid analytic_function declaration: {func_decl!r}") @@ -144,14 +167,13 @@ class to resolve here, just the function itself. registry = get_analytic_registry() spec = registry.get(func_decl) if spec is not None: - return _import_callable(spec.function) + return _import_callable(_python_binding_of(spec)) if "." not in func_decl: raise AnalyticFunctionRefused( f"Analytic function '{func_decl}' is not a registered name, and is not a dotted path.") - if any(s.function == func_decl for s in registry.values()): + if any(s.function and s.function == func_decl for s in registry.values()): return _import_callable(func_decl) - module_path = func_decl.rsplit(".", 1)[0] - if not _module_allowed(module_path): + if not python_binding_allowed(func_decl): raise AnalyticFunctionRefused( f"Analytic function '{func_decl}' is not registered and its module is not under an allowed " f"prefix {list(analytic_module_prefixes())}. Register it in the analytic registry, or add its " diff --git a/tests/micro-tests/test_routine_declarations_match_library.py b/tests/micro-tests/test_routine_declarations_match_library.py new file mode 100644 index 00000000..b1c9a958 --- /dev/null +++ b/tests/micro-tests/test_routine_declarations_match_library.py @@ -0,0 +1,72 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Contributors to the ODPi Egeria project. +"""RS-W2-2 PR-A: pyegeria's RoutineDeclaration mirrors trellis-analytics 0.4's (decision D2: the +library authors the schema, pyegeria mirrors it). Every built-in, read as a routine declaration, +must load as a library `RoutineDeclaration`, and the library's own reading of a pyegeria entry +(`routine_declaration(spec)`) must agree with pyegeria's (`spec.routine()`). Skipped when the +library (0.4 or later) is not installed. + + uv run -p 3.13 --with 'egeria-trellis-analytics>=0.4' pytest tests/micro-tests/test_routine_declarations_match_library.py +""" +import warnings + +import pytest + +routine_lib = pytest.importorskip("trellis_analytics.routine") +library = pytest.importorskip("trellis_analytics.declaration") + +from pyegeria.view.analytic_registry import _BUILTINS, ROW_SHAPES # noqa: E402 + +#: Declaration keys pyegeria may carry that an installed library release lacks: dropped only then. +PYEGERIA_AHEAD = {"measure": {"population"}, "declaration": {"out"}} + + +def _known(model, data: dict, where: str) -> dict: + ahead = {k for k in PYEGERIA_AHEAD[where] if k not in model.model_fields} + return {k: v for k, v in data.items() if k not in ahead} + + +def _for_library(routine) -> dict: + data = routine.model_dump(by_alias=True, exclude_defaults=True) + decl = routine.declaration + if decl is not None: + d = decl.model_dump(exclude_defaults=True) + d["absence"] = decl.absence.model_dump() + d["measures"] = [_known(library.MeasureDeclaration, m, "measure") for m in d.get("measures", [])] + data["declaration"] = _known(library.Declaration, d, "declaration") + return data + + +@pytest.fixture(scope="module", autouse=True) +def _row_shapes_registered(): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + for name, fields in ROW_SHAPES.items(): + library.register_shape(name, {"fields": [f.model_dump(exclude_defaults=True) for f in fields]}) + + +@pytest.mark.parametrize("name", sorted(_BUILTINS)) +def test_builtin_routine_loads_in_the_library(name): + with warnings.catch_warnings(): + warnings.simplefilter("error", DeprecationWarning) # no legacy word reaches the library + routine_lib.RoutineDeclaration.model_validate(_for_library(_BUILTINS[name].routine())) + + +@pytest.mark.parametrize("name", sorted(_BUILTINS)) +def test_the_library_reads_a_pyegeria_entry_as_pyegeria_does(name): + spec = _BUILTINS[name] + theirs = routine_lib.routine_declaration(spec).model_dump(by_alias=True, exclude={"declaration"}) + ours = spec.routine().model_dump(by_alias=True, exclude={"declaration"}) + assert theirs == ours + + +@pytest.mark.parametrize("model", ["RoutineDeclaration", "ParamDeclaration", "Binding", "TileSafety", "Admission"]) +def test_field_names_match(model): + from pyegeria.view import analytic_registry as ours + assert set(getattr(ours, model).model_fields) == set(getattr(routine_lib, model).model_fields) + + +@pytest.mark.parametrize("words", ["ROUTINE_KINDS", "BINDING_KINDS", "PARAM_CATEGORIES", "INPUT_ROLES"]) +def test_vocabularies_match(words): + from pyegeria.view import analytic_registry as ours + assert tuple(getattr(ours, words)) == tuple(getattr(routine_lib, words)) diff --git a/tests/micro-tests/test_rs_w2_2_routine_declaration.py b/tests/micro-tests/test_rs_w2_2_routine_declaration.py new file mode 100644 index 00000000..70274d5f --- /dev/null +++ b/tests/micro-tests/test_rs_w2_2_routine_declaration.py @@ -0,0 +1,210 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright Contributors to the ODPi Egeria project. +"""RS-W2-2 PR-A: a registry entry is a routine declaration plus bindings, and +`AnalyticFunctionSpec` is its compatibility view (`function` is the one python binding). +No behaviour change: every entry converts mechanically and round-trips, and a report spec +still names routines by registered name only (ISSUE-173).""" +import pytest + +from pyegeria.view import format_set_executor as fse +from pyegeria.view.analytic_registry import ( + _BUILTINS, + ROW_SHAPES, + Admission, + AnalyticDeclaration, + AnalyticFunctionSpec, + AnalyticParam, + Binding, + FieldDeclaration, + ParamDeclaration, + RoutineDeclaration, + TileSafety, + clear_runtime_analytic_functions, + get_analytic_registry, + get_routine_registry, + register_analytic_functions, + routine_declaration_errors, +) + +FN = "pyegeria.view.overview_metrics.count_elements" + + +def _view_of(spec: AnalyticFunctionSpec) -> AnalyticFunctionSpec: + return AnalyticFunctionSpec.from_routine(spec.routine(), returns=spec.returns, action=spec.action, + envelope=spec.envelope) + + +# ── Every entry converts, and the view round-trips ── + +def test_every_registry_entry_reads_as_a_routine_declaration(): + assert routine_declaration_errors() == {} + assert set(get_routine_registry()) == set(get_analytic_registry()) + + +@pytest.mark.parametrize("name", sorted(_BUILTINS)) +def test_builtin_round_trips_through_the_routine_declaration(name): + spec = _BUILTINS[name] + routine = spec.routine() + assert routine.python_target() == spec.function + assert [b.kind for b in routine.bindings] == ["python"] + assert routine.declaration == spec.declaration + assert _view_of(spec).model_dump() == spec.model_dump() + + +def _full_routine() -> RoutineDeclaration: + return RoutineDeclaration( + name="type_census", kind="processor", description="Counts rows by type.", version="1.0", + source="egeria", family="Census", tags=["census"], generic=True, + inputs=["egeria_client:MetadataExpert", "viewer"], + params=[ParamDeclaration(name="type_name", required=True, description="what to count"), + ParamDeclaration(name="page_size", type="int", default=100, category="performance"), + ParamDeclaration(name="layout", default="wide", category="shape")], + constants={"metadata_element_type": "Asset"}, + **{"in": "TypeCount"}, + declaration=_BUILTINS["count_elements"].declaration, + pushdown=["count by the repository"], + tile_safe=TileSafety(safe=True, cap={"page_size": 100}, reason="0.2-0.8 s measured"), + admission=Admission(resource_types=["database"], test_only=True), + bindings=[Binding(kind="egeria_query", target="Type census"), Binding(kind="python", target=FN)], + ) + + +def test_a_full_declaration_round_trips_through_the_view(): + routine = _full_routine() + view = AnalyticFunctionSpec.from_routine(routine) + assert view.function == FN + assert view.resource_types == ["database"] and view.test_only is True + assert [p.category for p in view.params] == ["content", "performance", "shape"] + assert view.routine() == routine + assert view.routine().model_dump(by_alias=True)["in"] == "TypeCount" + + +def test_the_view_dumps_and_reloads(): + view = AnalyticFunctionSpec.from_routine(_full_routine()) + assert AnalyticFunctionSpec.model_validate(view.model_dump()).routine() == view.routine() + assert AnalyticFunctionSpec.model_validate(view.model_dump(by_alias=True)).routine() == view.routine() + + +# ── function <-> the one python binding ── + +def test_function_alone_becomes_the_python_binding(): + spec = AnalyticFunctionSpec(name="n", function=FN) + assert [(b.kind, b.target) for b in spec.bindings] == [("python", FN)] + + +def test_a_python_binding_alone_fills_function(): + spec = AnalyticFunctionSpec(name="n", bindings=[{"kind": "python", "target": FN}]) + assert spec.function == FN + + +@pytest.mark.parametrize("data, words", [ + ({"function": FN, "bindings": [{"kind": "python", "target": FN + "_other"}]}, "they are one value"), + ({"bindings": [{"kind": "python", "target": FN}, {"kind": "python", "target": FN + "2"}]}, + "more than one python binding"), + ({}, "declares no binding"), + ({"bindings": [{"kind": "python", "target": "count_elements"}]}, "not a dotted import path"), + ({"bindings": [{"kind": "sql", "target": " "}]}, "names no target"), +]) +def test_binding_refusals_say_why(data, words): + with pytest.raises(ValueError, match=words): + AnalyticFunctionSpec(name="n", **data) + + +def test_an_entry_with_no_python_binding_is_refused_by_name_when_run(): + register_analytic_functions({"saved_census": AnalyticFunctionSpec( + name="saved_census", bindings=[Binding(kind="egeria_query", target="Census query")])}) + try: + assert get_routine_registry()["saved_census"].python_target() == "" + with pytest.raises(fse.AnalyticFunctionRefused, + match=r"'saved_census' has no python binding \(its bindings: egeria_query 'Census " + r"query'\); this pyegeria runs python bindings only"): + fse.resolve_report_spec_function("saved_census") + finally: + clear_runtime_analytic_functions() + + +# ── Declaration rules, mirrored from trellis-analytics 0.4 ── + +@pytest.mark.parametrize("change, words", [ + ({"params": [ParamDeclaration(name="x")], "constants": {"x": 1}}, "as a parameter and a constant"), + ({"params": [ParamDeclaration(name="x"), ParamDeclaration(name="x")]}, "parameter x more than once"), + ({"inputs": ["client"]}, "is not a role word"), + ({"inputs": ["viewer", "viewer"]}, "an input more than once"), + ({"generic": True, "binding_note": "fixed to Asset"}, "a generic routine fixes nothing"), + ({"in": "TypeCount"}, "only a processor reads"), + ({"name": "type census"}, "not an identifier"), + ({"bindings": [Binding(kind="python", target=FN), Binding(kind="python", target=FN + "2")]}, + "more than one python binding"), + ({"surprise": 1}, "Extra inputs are not permitted"), +]) +def test_routine_refusals_say_why(change, words): + with pytest.raises(ValueError, match=words): + RoutineDeclaration(**{"name": "r", **change}) + + +@pytest.mark.parametrize("make, words", [ + (lambda: ParamDeclaration(name="x", required=True, default=1), "required and has a default"), + (lambda: ParamDeclaration(name="x", category="speed"), "content"), + (lambda: TileSafety(safe=True, reason=" "), "declares no reason"), + (lambda: TileSafety(safe=False, cap={"n": 1}, reason="slow"), "a cap says within what it IS safe"), +]) +def test_part_refusals_say_why(make, words): + with pytest.raises(ValueError, match=words): + make() + + +def test_a_breaking_entry_is_named_not_raised(): + """An entry that runs today but breaks a declaration rule is listed with the rule's words; + the registry and the run path are unchanged (PR-A: no behaviour change).""" + register_analytic_functions({"noted_generic": AnalyticFunctionSpec( + name="noted_generic", function=FN, generic=True, binding_note="fixed to Asset")}) + try: + assert "noted_generic" in get_analytic_registry() + assert "noted_generic" not in get_routine_registry() + assert "a generic routine fixes nothing" in routine_declaration_errors()["noted_generic"] + assert callable(fse.resolve_report_spec_function("noted_generic")) + finally: + clear_runtime_analytic_functions() + + +def test_param_category_defaults_to_content(): + assert AnalyticParam(name="x").category == "content" + + +# ── declaration.out in the library's three forms (RS-W2-1a read-back F2) ── + +def test_out_reads_a_field_list_a_shape_name_and_an_inline_shape(): + base = _BUILTINS["count_elements"].declaration.model_dump() + as_list = AnalyticDeclaration.model_validate(base) + assert [f.name for f in as_list.out_fields()] == ["value"] + named = AnalyticDeclaration.model_validate({**base, "out": "TypeCount"}) + assert named.out_fields() == ROW_SHAPES["TypeCount"] + library_only = AnalyticDeclaration.model_validate({**base, "out": "CoverageResult"}) + assert library_only.out_fields() == [] + inline = AnalyticDeclaration.model_validate( + {**base, "out": {"fields": [{"name": "n", "label": "N", "type": "int"}, {"include": "TypeCount.count"}]}}) + assert inline.out_fields() == [FieldDeclaration(name="n", label="N", type="int")] + + +def test_a_routine_reads_its_out_fields_through_its_declaration(): + assert _BUILTINS["counts_by_type"].routine().out_fields() == ROW_SHAPES["TypeCount"] + assert RoutineDeclaration(name="r").out_fields() == [] + + +# ── The allow-list applies to binding targets only (ISSUE-173) ── + +@pytest.mark.parametrize("name", sorted(_BUILTINS)) +def test_every_builtin_python_binding_is_allowed(name): + assert fse.python_binding_allowed(_BUILTINS[name].routine().python_target()) + + +def test_a_report_spec_path_is_refused_even_under_an_allowed_prefix(): + assert fse.python_binding_allowed(FN) + with pytest.raises(fse.AnalyticFunctionRefused, match="use its registered name 'count_elements'"): + fse.resolve_report_spec_function(FN) + + +def test_an_undeclared_out_reads_as_no_fields(): + """The library writes an undeclared `out` as None; pyegeria's built-ins as [].""" + base = _BUILTINS["count_elements"].declaration.model_dump() + assert AnalyticDeclaration.model_validate({**base, "out": None}).out_fields() == []