feat(checker): add context to reports

This commit is contained in:
2026-07-08 09:39:11 +02:00
parent 1acf33f376
commit 10c6ea7dda
3 changed files with 34 additions and 18 deletions

View File

@@ -51,24 +51,25 @@ class FrameGroupByMethodRegistry(MethodRegistry[Call]):
new_columns: list[DataFrameType.Column] = [] new_columns: list[DataFrameType.Column] = []
for column in call.groupby.frame.columns: for column in call.groupby.frame.columns:
column_groupby: ColumnGroupBy = ColumnGroupBy(column=column.type) with self.reporter.with_context(f"in column '{column.name}'"):
result_type: Type = self.typer.call_method( column_groupby: ColumnGroupBy = ColumnGroupBy(column=column.type)
location=call.location, result_type: Type = self.typer.call_method(
call_expr=call.call_expr, location=call.location,
obj=(call.groupby_expr, column_groupby), call_expr=call.call_expr,
method_name=method, obj=(call.groupby_expr, column_groupby),
positional=call.positional, method_name=method,
keywords=call.keywords, positional=call.positional,
) keywords=call.keywords,
if not isinstance(result_type, ColumnType): )
result_type = ColumnType(type=UnknownType()) if not isinstance(result_type, ColumnType):
new_columns.append( result_type = ColumnType(type=UnknownType())
DataFrameType.Column( new_columns.append(
index=column.index, DataFrameType.Column(
name=column.name, index=column.index,
type=result_type, name=column.name,
type=result_type,
)
) )
)
return DataFrameType(columns=new_columns) return DataFrameType(columns=new_columns)

View File

@@ -159,7 +159,10 @@ class FrameMethodRegistry(MethodRegistry[Call]):
col_type2 = ColumnType(type=operand[1]) col_type2 = ColumnType(type=operand[1])
if col_type2 is not None: if col_type2 is not None:
col_type = self._get_method_result(call, col_type1, col_type2, method) with self.reporter.with_context(f"in column '{column.name}'"):
col_type = self._get_method_result(
call, col_type1, col_type2, method
)
new_column = DataFrameType.Column( new_column = DataFrameType.Column(
index=column.index, index=column.index,

View File

@@ -1,5 +1,6 @@
from __future__ import annotations from __future__ import annotations
from contextlib import contextmanager
from typing import Optional from typing import Optional
from midas.ast.location import Location from midas.ast.location import Location
@@ -54,6 +55,7 @@ class FileReporter:
def __init__(self, base_reporter: Reporter, path: Optional[str]) -> None: def __init__(self, base_reporter: Reporter, path: Optional[str]) -> None:
self.base_reporter: Reporter = base_reporter self.base_reporter: Reporter = base_reporter
self.path: Optional[str] = path self.path: Optional[str] = path
self._context: list[str] = []
def for_file(self, path: Optional[str]) -> FileReporter: def for_file(self, path: Optional[str]) -> FileReporter:
"""Create a new file reporter for the given path with the same base reporter """Create a new file reporter for the given path with the same base reporter
@@ -66,6 +68,14 @@ class FileReporter:
""" """
return FileReporter(self.base_reporter, path) return FileReporter(self.base_reporter, path)
@contextmanager
def with_context(self, ctx: str):
self._context.append(ctx)
try:
yield
finally:
self._context.pop()
def report(self, type: DiagnosticType, location: Location, message: str): def report(self, type: DiagnosticType, location: Location, message: str):
"""Report a diagnostic to the base reporter """Report a diagnostic to the base reporter
@@ -74,6 +84,8 @@ class FileReporter:
location (Location): the location of the diagnostic in the file location (Location): the location of the diagnostic in the file
message (str): the diagnostic's message message (str): the diagnostic's message
""" """
for ctx in self._context:
message = message + ", " + ctx
self.base_reporter.report(self.path, type, location, message) self.base_reporter.report(self.path, type, location, message)
def error(self, location: Location, message: str): def error(self, location: Location, message: str):