Source code for cadrumo.domain.calculations.registry._formula_operator_contracts
"""Canonical arity contracts for registry formula operators.The registry schema and calculation runtime consume the same table so anoperator cannot be accepted by the authored authority with a shape the runtimewill later refuse. Leaf-kind requirements remain with the specialised runtimeevaluators; this module owns the operator-wide argument-count contract only."""from__future__importannotationsfromdataclassesimportdataclassfromtypingimportFinalfrom.errorsimportRegistryValidationErrorfrom.schema_baseimportFormulaOperator__all__=["FORMULA_OPERATOR_ARITIES","FormulaOperatorArity","require_formula_operator_arity"]
[docs]@dataclass(frozen=True,slots=True)classFormulaOperatorArity:"""Inclusive argument-count bounds for one formula operator."""minimum:intmaximum:int|None=None
[docs]defaccepts(self,count:int)->bool:"""Return whether ``count`` satisfies these bounds."""returncount>=self.minimumand(self.maximumisNoneorcount<=self.maximum)
[docs]defrequire_formula_operator_arity(op:str,count:int)->None:"""Refuse an argument count outside the canonical contract for ``op``."""ifopnotinFORMULA_OPERATOR_ARITIES:raiseRegistryValidationError(f"formula expression uses unsupported op {op!r}")contract=FORMULA_OPERATOR_ARITIES[op]ifcontract.accepts(count):returnifcontract.maximum==contract.minimum:expectation=f"{contract.minimum} arg"+("s"ifcontract.minimum!=1else"")elifcontract.maximumisNone:expectation=f"at least {contract.minimum} arg"+("s"ifcontract.minimum!=1else"")else:expectation=f"{contract.minimum}..{contract.maximum} args"raiseRegistryValidationError(f"formula op {op!r} expects {expectation}, got {count}")