@dataclass
class TyServer(LSPServerInfo):
"""Ty (Astral) type checker with GitLab JSON diagnostic parsing."""
def _parse_json_diagnostics(self, output: str) -> list[Diagnostic]:
"""Parse ty GitLab JSON output."""
import anyenv
diagnostics: list[Diagnostic] = []
try:
data = anyenv.load_json(output, return_type=list)
for item in data:
location = item.get("location", {})
positions = location.get("positions", {})
begin = positions.get("begin", {})
end = positions.get("end", {})
# Map GitLab severity to our severity
gitlab_severity = item.get("severity", "major")
if gitlab_severity in ("blocker", "critical", "major"):
severity = "error"
elif gitlab_severity == "minor":
severity = "warning"
else:
severity = "information"
diagnostics.append(
Diagnostic(
file=location.get("path", ""),
line=begin.get("line", 1),
column=begin.get("column", 1),
end_line=end.get("line", begin.get("line", 1)),
end_column=end.get("column", begin.get("column", 1)),
severity=severity_from_string(severity),
message=item.get("description", ""),
code=item.get("check_name"),
source=self.id,
)
)
except anyenv.JsonLoadError:
pass
return diagnostics