Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow sub-diagnostic labels to refer to fields of their parent #600

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions guppylang/diagnostic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import string
import textwrap
from dataclasses import dataclass, field, fields
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import ClassVar, Final, Literal, Protocol, overload, runtime_checkable
from typing import (
Any,
ClassVar,
Final,
Literal,
Protocol,
overload,
runtime_checkable,
)

from typing_extensions import Self

Expand Down Expand Up @@ -54,6 +64,9 @@ class SubDiagnostic(Protocol):
#: Message that is printed if no span is provided.
message: ClassVar[str | None] = None

#: The parent main diagnostic this sub-diagnostic is attached to.
_parent: "Diagnostic | None" = field(default=None, init=False)

def __post_init__(self) -> None:
if self.span_label and self.span is None:
raise InternalGuppyError("SubDiagnostic: Span label provided without span")
Expand All @@ -78,8 +91,17 @@ def _render(self, s: str | None) -> str | None:
"""Helper method to fill in placeholder values in strings with fields of this
diagnostic.
"""
values = {f.name: getattr(self, f.name) for f in fields(self)}
return s.format(**values) if s is not None else None

class CustomFormatter(string.Formatter):
def get_value(
_self, key: int | str, args: Sequence[Any], kwargs: Mapping[str, Any]
) -> Any:
assert isinstance(key, str)
if hasattr(self, key):
return getattr(self, key)
return getattr(self._parent, key)

return CustomFormatter().format(s) if s is not None else None


@runtime_checkable
Expand Down Expand Up @@ -114,6 +136,7 @@ def add_sub_diagnostic(self, sub: "SubDiagnostic") -> Self:
raise InternalGuppyError(
"Diagnostic: Cross-file sub-diagnostics are not supported"
)
object.__setattr__(sub, "_parent", self)
self.children.append(sub)
return self

Expand Down
7 changes: 7 additions & 0 deletions tests/diagnostics/snapshots/test_advanced_formatting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Error: Can't compare apples with oranges (at <unknown>:1:0)
|
1 | apple == orange
| ^^^^^ This is an apple
|
1 | apple == orange
| ------ This is not an apple
aborgna-q marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions tests/diagnostics/test_diagnostics_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ class MySubDiagnostic(Note):
run_test(source, diagnostic, snapshot, request)


def test_advanced_formatting(snapshot, request):
@dataclass(frozen=True)
class MyDiagnostic(Error):
title: ClassVar[str] = "Can't compare apples with oranges"
span_label: ClassVar[str] = "This is an {a}{pp}{le}"
a: str

@property
def pp(self) -> str:
return "pp"

@property
def le(self) -> str:
return "le"

@dataclass(frozen=True)
class MySubDiagnostic(Note):
span_label: ClassVar[str] = "This is not an {a}{pp}{p}{le}"
p: str

@property
def pp(self) -> str:
return "p"

source = "apple == orange"
span_apple = Span(Loc(file, 1, 0), Loc(file, 1, 5))
span_orange = Span(Loc(file, 1, 9), Loc(file, 1, 15))
diagnostic = MyDiagnostic(span_apple, "a")
diagnostic.add_sub_diagnostic(MySubDiagnostic(span_orange, "p"))
run_test(source, diagnostic, snapshot, request)


def test_long_label(snapshot, request):
@dataclass(frozen=True)
class MyDiagnostic(Error):
Expand Down
Loading