|
| 1 | +import traceback |
| 2 | +from dataclasses import dataclass, field, replace |
| 3 | +from enum import Enum |
| 4 | +from io import StringIO |
| 5 | +from typing import TYPE_CHECKING, Optional, TypeVar |
| 6 | + |
| 7 | +from databricks.bundles.core._location import Location |
| 8 | + |
| 9 | +_T = TypeVar("_T") |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from typing_extensions import Self |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "Diagnostic", |
| 16 | + "Diagnostics", |
| 17 | + "Severity", |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +class Severity(Enum): |
| 22 | + WARNING = "warning" |
| 23 | + ERROR = "error" |
| 24 | + |
| 25 | + |
| 26 | +@dataclass(kw_only=True, frozen=True) |
| 27 | +class Diagnostic: |
| 28 | + severity: Severity |
| 29 | + """ |
| 30 | + Severity of the diagnostics item. |
| 31 | + """ |
| 32 | + |
| 33 | + summary: str |
| 34 | + """ |
| 35 | + Short summary of the error or warning. |
| 36 | + """ |
| 37 | + |
| 38 | + detail: Optional[str] = None |
| 39 | + """ |
| 40 | + Explanation of the error or warning. |
| 41 | + """ |
| 42 | + |
| 43 | + path: Optional[tuple[str, ...]] = None |
| 44 | + """ |
| 45 | + Path in databricks.yml where the error or warning occurred. |
| 46 | + """ |
| 47 | + |
| 48 | + location: Optional[Location] = None |
| 49 | + """ |
| 50 | + Source code location where the error or warning occurred. |
| 51 | + """ |
| 52 | + |
| 53 | + def as_dict(self) -> dict: |
| 54 | + def omit_none(values: dict): |
| 55 | + return {key: value for key, value in values.items() if value is not None} |
| 56 | + |
| 57 | + if self.location: |
| 58 | + location = self.location.as_dict() |
| 59 | + else: |
| 60 | + location = None |
| 61 | + |
| 62 | + return omit_none( |
| 63 | + { |
| 64 | + "severity": self.severity.value, |
| 65 | + "summary": self.summary, |
| 66 | + "detail": self.detail, |
| 67 | + "path": self.path, |
| 68 | + "location": location, |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@dataclass(frozen=True) |
| 74 | +class Diagnostics: |
| 75 | + """ |
| 76 | + Diagnostics is a collection of errors and warnings we print to users. |
| 77 | +
|
| 78 | + Each item can have source location or path associated, that is reported in output to |
| 79 | + indicate where the error or warning occurred. |
| 80 | + """ |
| 81 | + |
| 82 | + items: tuple[Diagnostic, ...] = field(default_factory=tuple, kw_only=False) |
| 83 | + |
| 84 | + def extend(self, diagnostics: "Self") -> "Self": |
| 85 | + """ |
| 86 | + Extend items with another diagnostics. This pattern allows |
| 87 | + to accumulate errors and warnings. |
| 88 | +
|
| 89 | + Example: |
| 90 | +
|
| 91 | + .. code-block:: python |
| 92 | +
|
| 93 | + def foo() -> Diagnostics: ... |
| 94 | + def bar() -> Diagnostics: ... |
| 95 | +
|
| 96 | + diagnostics = Diagnostics() |
| 97 | + diagnostics = diagnostics.extend(foo()) |
| 98 | + diagnostics = diagnostics.extend(bar()) |
| 99 | + """ |
| 100 | + |
| 101 | + return replace( |
| 102 | + self, |
| 103 | + items=(*self.items, *diagnostics.items), |
| 104 | + ) |
| 105 | + |
| 106 | + def extend_tuple(self, pair: tuple[_T, "Self"]) -> tuple[_T, "Self"]: |
| 107 | + """ |
| 108 | + Extend items with another diagnostics. This variant is useful when |
| 109 | + methods return a pair of value and diagnostics. This pattern allows |
| 110 | + to accumulate errors and warnings. |
| 111 | +
|
| 112 | + Example: |
| 113 | +
|
| 114 | + .. code-block:: python |
| 115 | +
|
| 116 | + def foo() -> (int, Diagnostics): ... |
| 117 | +
|
| 118 | + diagnostics = Diagnostics() |
| 119 | + value, diagnostics = diagnostics.extend_tuple(foo()) |
| 120 | + """ |
| 121 | + |
| 122 | + value, other_diagnostics = pair |
| 123 | + |
| 124 | + return value, self.extend(other_diagnostics) |
| 125 | + |
| 126 | + def has_error(self) -> bool: |
| 127 | + """ |
| 128 | + Returns True if there is at least one error in diagnostics. |
| 129 | + """ |
| 130 | + |
| 131 | + for item in self.items: |
| 132 | + if item.severity == Severity.ERROR: |
| 133 | + return True |
| 134 | + |
| 135 | + return False |
| 136 | + |
| 137 | + @classmethod |
| 138 | + def create_error( |
| 139 | + cls, |
| 140 | + msg: str, |
| 141 | + *, |
| 142 | + detail: Optional[str] = None, |
| 143 | + location: Optional[Location] = None, |
| 144 | + path: Optional[tuple[str, ...]] = None, |
| 145 | + ) -> "Self": |
| 146 | + """ |
| 147 | + Create an error diagnostics. |
| 148 | + """ |
| 149 | + |
| 150 | + return cls( |
| 151 | + items=( |
| 152 | + Diagnostic( |
| 153 | + severity=Severity.ERROR, |
| 154 | + summary=msg, |
| 155 | + detail=detail, |
| 156 | + location=location, |
| 157 | + path=path, |
| 158 | + ), |
| 159 | + ), |
| 160 | + ) |
| 161 | + |
| 162 | + @classmethod |
| 163 | + def create_warning( |
| 164 | + cls, |
| 165 | + msg: str, |
| 166 | + *, |
| 167 | + detail: Optional[str] = None, |
| 168 | + location: Optional[Location] = None, |
| 169 | + path: Optional[tuple[str, ...]] = None, |
| 170 | + ) -> "Self": |
| 171 | + """ |
| 172 | + Create a warning diagnostics. |
| 173 | + """ |
| 174 | + |
| 175 | + return cls( |
| 176 | + items=( |
| 177 | + Diagnostic( |
| 178 | + severity=Severity.WARNING, |
| 179 | + summary=msg, |
| 180 | + detail=detail, |
| 181 | + location=location, |
| 182 | + path=path, |
| 183 | + ), |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + @classmethod |
| 188 | + def from_exception( |
| 189 | + cls, |
| 190 | + exc: Exception, |
| 191 | + *, |
| 192 | + summary: str, |
| 193 | + location: Optional[Location] = None, |
| 194 | + path: Optional[tuple[str, ...]] = None, |
| 195 | + explanation: Optional[str] = None, |
| 196 | + ) -> "Self": |
| 197 | + """ |
| 198 | + Create diagnostics from an exception. |
| 199 | +
|
| 200 | + :param exc: exception to create diagnostics from |
| 201 | + :param summary: short summary of the error |
| 202 | + :param location: optional location in the source code where the error occurred |
| 203 | + :param path: optional path to relevant property in databricks.yml |
| 204 | + :param explanation: optional explanation to add to the details |
| 205 | + """ |
| 206 | + |
| 207 | + detail_io = StringIO() |
| 208 | + traceback.print_exception(exc, file=detail_io) |
| 209 | + |
| 210 | + detail = detail_io.getvalue() |
| 211 | + if explanation: |
| 212 | + detail = f"{detail}\n\n\033[0;36mExplanation:\033[0m {explanation}" |
| 213 | + |
| 214 | + diagnostic = Diagnostic( |
| 215 | + severity=Severity.ERROR, |
| 216 | + summary=summary, |
| 217 | + location=location, |
| 218 | + path=path, |
| 219 | + detail=detail, |
| 220 | + ) |
| 221 | + |
| 222 | + return cls(items=(diagnostic,)) |
0 commit comments