-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.py
More file actions
63 lines (47 loc) · 1.91 KB
/
Copy pathdiagnostics.py
File metadata and controls
63 lines (47 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Structured validation diagnostics — the result contract consumed by engineers AND the LLM.
Mirrors AWS ValidateStateMachineDefinition: callers branch on `result` (+ codes), never on exact
wording. ERROR-severity diagnostics block admission; WARNING informs. The canonical code list
lives in wosool-dsl/conformance/README.md and wosool-dsl/03-VALIDATION-AND-TYPES.md.
"""
from __future__ import annotations
from dataclasses import dataclass, field
from typing import Literal
Severity = Literal["ERROR", "WARNING"]
@dataclass(frozen=True)
class Diagnostic:
code: str
severity: Severity
message: str
location: str = ""
node: str | None = None
@dataclass(frozen=True)
class ValidationResult:
result: Literal["OK", "FAIL"]
diagnostics: tuple[Diagnostic, ...] = ()
@property
def ok(self) -> bool:
return self.result == "OK"
@classmethod
def of(cls, diagnostics: list[Diagnostic]) -> ValidationResult:
"""Admit unless any diagnostic is an ERROR."""
blocking = any(d.severity == "ERROR" for d in diagnostics)
return cls(result="FAIL" if blocking else "OK", diagnostics=tuple(diagnostics))
@dataclass
class DiagnosticCollector:
"""Accumulates diagnostics across the validator passes."""
items: list[Diagnostic] = field(default_factory=list)
def error(
self, code: str, message: str, *, node: str | None = None, location: str = ""
) -> None:
self.items.append(
Diagnostic(code=code, severity="ERROR", message=message, node=node, location=location)
)
def warning(
self, code: str, message: str, *, node: str | None = None, location: str = ""
) -> None:
self.items.append(
Diagnostic(code=code, severity="WARNING", message=message, node=node, location=location)
)
@property
def has_errors(self) -> bool:
return any(d.severity == "ERROR" for d in self.items)