Skip to content

diagnostics

Classes:

Diagnostic dataclass

Diagnostic(
    message: str,
    node: AST | None = None,
    code: str | None = None,
    severity: str = "error",
)

Methods:

format

format() -> str
Source code in src/irx/analysis/diagnostics.py
36
37
38
39
40
41
42
43
44
45
46
47
48
def format(self) -> str:
    """
    title: Format the diagnostic for human display.
    returns:
      type: str
    """
    location = ""
    if self.node is not None:
        loc = getattr(self.node, "loc", None)
        if loc is not None and getattr(loc, "line", -1) >= 0:
            location = f"{loc.line}:{loc.col}: "
    code = f"[{self.code}] " if self.code else ""
    return f"{location}{code}{self.message}"

DiagnosticBag

DiagnosticBag()

Methods:

Source code in src/irx/analysis/diagnostics.py
60
61
62
63
64
def __init__(self) -> None:
    """
    title: Initialize DiagnosticBag.
    """
    self.diagnostics: list[Diagnostic] = []

add

add(
    message: str,
    *,
    node: AST | None = None,
    code: str | None = None,
) -> None
Source code in src/irx/analysis/diagnostics.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def add(
    self,
    message: str,
    *,
    node: astx.AST | None = None,
    code: str | None = None,
) -> None:
    """
    title: Add one error diagnostic.
    parameters:
      message:
        type: str
      node:
        type: astx.AST | None
      code:
        type: str | None
    """
    self.diagnostics.append(
        Diagnostic(message=message, node=node, code=code)
    )

extend

extend(diagnostics: Iterable[Diagnostic]) -> None
Source code in src/irx/analysis/diagnostics.py
87
88
89
90
91
92
93
94
def extend(self, diagnostics: Iterable[Diagnostic]) -> None:
    """
    title: Extend the bag with diagnostics.
    parameters:
      diagnostics:
        type: Iterable[Diagnostic]
    """
    self.diagnostics.extend(diagnostics)

format

format() -> str
Source code in src/irx/analysis/diagnostics.py
104
105
106
107
108
109
110
def format(self) -> str:
    """
    title: Format the whole bag.
    returns:
      type: str
    """
    return "\n".join(diag.format() for diag in self.diagnostics)

has_errors

has_errors() -> bool
Source code in src/irx/analysis/diagnostics.py
 96
 97
 98
 99
100
101
102
def has_errors(self) -> bool:
    """
    title: Return True when any diagnostics exist.
    returns:
      type: bool
    """
    return bool(self.diagnostics)

raise_if_errors

raise_if_errors() -> None
Source code in src/irx/analysis/diagnostics.py
112
113
114
115
116
117
def raise_if_errors(self) -> None:
    """
    title: Raise SemanticError when errors exist.
    """
    if self.has_errors():
        raise SemanticError(self)

SemanticError

SemanticError(diagnostics: DiagnosticBag)

Bases: Exception

Source code in src/irx/analysis/diagnostics.py
131
132
133
134
135
136
137
138
139
def __init__(self, diagnostics: DiagnosticBag) -> None:
    """
    title: Initialize SemanticError.
    parameters:
      diagnostics:
        type: DiagnosticBag
    """
    self.diagnostics = diagnostics
    super().__init__(diagnostics.format())