|
|
|
@@ -19,7 +19,7 @@ from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from ps_core.parse_spec import OP_SYMBOLS
|
|
|
|
|
from ps_core.parse_spec import OP_SYMBOLS, _referenced_fields_for_kind
|
|
|
|
|
from ps_core.templating import env
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
@@ -115,15 +115,31 @@ def _raise(error_type: str, template: str, context_src: str, indent: str = "
|
|
|
|
|
return f"{indent}raise PydanticCustomError({error_type!r}, {template!r}, {context_src})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _error_type_for_rule(rule: dict[str, Any]) -> str:
|
|
|
|
|
"""
|
|
|
|
|
A cross rule's `kind` alone (e.g. "requires") is not specific enough:
|
|
|
|
|
a table can have two different `requires` rules, about different
|
|
|
|
|
fields, and both would collapse into the same reason in any report
|
|
|
|
|
grouped by error type. This appends the fields the rule is actually
|
|
|
|
|
about — computed once, at generation time, from the rule's own
|
|
|
|
|
definition — never from a row's data, so it stays identical across
|
|
|
|
|
every row that fails this same rule, unlike the message text (which
|
|
|
|
|
bakes in the specific failing values and is unique per row).
|
|
|
|
|
"""
|
|
|
|
|
fields = _referenced_fields_for_kind(rule["kind"], rule)
|
|
|
|
|
return f"{rule['kind']}[{','.join(fields)}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _render_order(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
left, right = rule["fields"]
|
|
|
|
|
op_symbol = OP_SYMBOLS[rule["op"]]
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{left} is not None and self.{right} is not None:",
|
|
|
|
|
f" if not (self.{left} {op_symbol} self.{right}):",
|
|
|
|
|
_raise("order", "{_msg}: {left} vs {right}", f"{{'_msg': _msg, 'left': self.{left}, 'right': self.{right}}}"),
|
|
|
|
|
_raise(error_type, "{_msg}: {left} vs {right}", f"{{'_msg': _msg, 'left': self.{left}, 'right': self.{right}}}"),
|
|
|
|
|
]
|
|
|
|
|
return _wrap_method(name, body)
|
|
|
|
|
|
|
|
|
@@ -131,11 +147,12 @@ def _render_order(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
def _render_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
a, b = rule["fields"]
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{a} is not None and self.{b} is not None:",
|
|
|
|
|
f" if self.{a} != self.{b}:",
|
|
|
|
|
_raise("equals", "{_msg}: {a} vs {b}", f"{{'_msg': _msg, 'a': self.{a}, 'b': self.{b}}}"),
|
|
|
|
|
_raise(error_type, "{_msg}: {a} vs {b}", f"{{'_msg': _msg, 'a': self.{a}, 'b': self.{b}}}"),
|
|
|
|
|
]
|
|
|
|
|
return _wrap_method(name, body)
|
|
|
|
|
|
|
|
|
@@ -145,13 +162,14 @@ def _render_sum_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
sum_expr = " + ".join(f"self.{p}" for p in parts)
|
|
|
|
|
none_checks = " and ".join(f"self.{p} is not None" for p in parts)
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{target} is not None and {none_checks}:",
|
|
|
|
|
f" _expected = {sum_expr}",
|
|
|
|
|
f" if abs(self.{target} - _expected) > {tol!r}:",
|
|
|
|
|
_raise(
|
|
|
|
|
"sum_equals", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
@@ -163,13 +181,14 @@ def _render_ratio_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
power, tol = rule.get("power", 1), rule.get("tolerance", 0)
|
|
|
|
|
denom_expr = f"self.{den}" + (f" ** {power}" if power != 1 else "")
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{target} is not None and self.{num} is not None and self.{den} not in (None, 0):",
|
|
|
|
|
f" _expected = self.{num} / ({denom_expr})",
|
|
|
|
|
f" if abs(self.{target} - _expected) > {tol!r}:",
|
|
|
|
|
_raise(
|
|
|
|
|
"ratio_equals", "{_msg}: got {got}, expected {expected:.4f}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected:.4f}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
@@ -180,13 +199,14 @@ def _render_date_offset_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
base, offset, target = rule["base"], rule["offset_days_field"], rule["target"]
|
|
|
|
|
tol = rule.get("tolerance_days", 0)
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{target} is not None and self.{base} is not None and self.{offset} is not None:",
|
|
|
|
|
f" _expected = self.{base} + timedelta(days=self.{offset})",
|
|
|
|
|
f" if abs((self.{target} - _expected).days) > {tol!r}:",
|
|
|
|
|
_raise(
|
|
|
|
|
"date_offset_equals", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
@@ -197,13 +217,14 @@ def _render_date_diff_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
start, end, target = rule["start"], rule["end"], rule["target"]
|
|
|
|
|
tol = rule.get("tolerance_days", 0)
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
|
f"if self.{target} is not None and self.{start} is not None and self.{end} is not None:",
|
|
|
|
|
f" _expected = (self.{end} - self.{start}).days",
|
|
|
|
|
f" if abs(self.{target} - _expected) > {tol!r}:",
|
|
|
|
|
_raise(
|
|
|
|
|
"date_diff_equals", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
@@ -215,6 +236,7 @@ def _render_comparison_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
op_symbol = OP_SYMBOLS[op]
|
|
|
|
|
tol = rule.get("tolerance", 0)
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
|
|
|
|
|
if "right" in rule:
|
|
|
|
|
right_field = rule["right"]
|
|
|
|
@@ -235,7 +257,7 @@ def _render_comparison_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
f" _expected = bool({cond_expr})",
|
|
|
|
|
f" if bool(self.{target}) != _expected:",
|
|
|
|
|
_raise(
|
|
|
|
|
"comparison_equals", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
@@ -246,6 +268,7 @@ def _render_piecewise_lookup(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
source, target = rule["source"], rule["target"]
|
|
|
|
|
breakpoints, otherwise = rule["breakpoints"], rule["otherwise"].upper()
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
|
|
|
|
|
branch: list[str] = []
|
|
|
|
|
for i, bp in enumerate(breakpoints):
|
|
|
|
@@ -261,7 +284,7 @@ def _render_piecewise_lookup(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
body.append(" if _actual != _expected:")
|
|
|
|
|
body.append(
|
|
|
|
|
_raise(
|
|
|
|
|
"piecewise_lookup", "{_msg}: got {got}, expected {expected} for " + source + "={source_value}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected} for " + source + "={source_value}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': _actual, 'expected': _expected, 'source_value': self.{source}}}",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@@ -271,6 +294,7 @@ def _render_piecewise_lookup(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
def _render_requires(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
if_field, then_field = rule["if_field"], rule["then_field"]
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
cond = _condition_expr(if_field, rule)
|
|
|
|
|
body = [f"_msg = {msg!r}", f"if {cond}:"]
|
|
|
|
|
if "then_value" in rule:
|
|
|
|
@@ -280,7 +304,7 @@ def _render_requires(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
body.append(f" if self.{then_field} is None:")
|
|
|
|
|
body.append(
|
|
|
|
|
_raise(
|
|
|
|
|
"requires", "{_msg}",
|
|
|
|
|
error_type, "{_msg}",
|
|
|
|
|
f"{{'_msg': _msg, 'if_field': self.{if_field}, 'then_field': self.{then_field}}}",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@@ -291,6 +315,7 @@ def _render_forbidden_if(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
if_field, then_fields = rule["if_field"], rule["then_fields"]
|
|
|
|
|
allowed_values = [v.upper() if isinstance(v, str) else v for v in rule.get("then_allowed_values", [])]
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
cond = _condition_expr(if_field, rule)
|
|
|
|
|
body = [f"_msg = {msg!r}", f"if {cond}:"]
|
|
|
|
|
for tf in then_fields:
|
|
|
|
@@ -298,7 +323,7 @@ def _render_forbidden_if(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
body.append(f" if _val is not None and _val not in {allowed_values!r}:")
|
|
|
|
|
body.append(
|
|
|
|
|
_raise(
|
|
|
|
|
"forbidden_if", "{_msg}",
|
|
|
|
|
error_type, "{_msg}",
|
|
|
|
|
f"{{'_msg': _msg, 'if_field': self.{if_field}, 'then_field': self.{tf}, 'value': _val}}",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@@ -308,6 +333,7 @@ def _render_forbidden_if(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
def _render_any_of_implies(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
target = rule["target"]
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
cond_exprs = [_render_single_condition(c) for c in rule["conditions"]]
|
|
|
|
|
body = [f"_msg = {msg!r}", "_components = ("]
|
|
|
|
|
body += [f" {ce}," for ce in cond_exprs]
|
|
|
|
@@ -317,7 +343,7 @@ def _render_any_of_implies(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
body.append(f" if self.{target} != _expected:")
|
|
|
|
|
body.append(
|
|
|
|
|
_raise(
|
|
|
|
|
"any_of_implies", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
@@ -329,6 +355,7 @@ def _render_conditional_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
value_if_true, value_if_false = rule["value_if_true"], rule["value_if_false"]
|
|
|
|
|
tol = rule.get("tolerance", 0)
|
|
|
|
|
msg = rule.get("message") or rule["display"]
|
|
|
|
|
error_type = _error_type_for_rule(rule)
|
|
|
|
|
cond = _condition_expr(if_field, rule)
|
|
|
|
|
body = [
|
|
|
|
|
f"_msg = {msg!r}",
|
|
|
|
@@ -341,7 +368,7 @@ def _render_conditional_equals(rule: dict[str, Any], name: str) -> str:
|
|
|
|
|
body.append(f" if self.{target} != _expected:")
|
|
|
|
|
body.append(
|
|
|
|
|
_raise(
|
|
|
|
|
"conditional_equals", "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
error_type, "{_msg}: got {got}, expected {expected}",
|
|
|
|
|
f"{{'_msg': _msg, 'got': self.{target}, 'expected': _expected}}",
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|