feat: handle multi-parameter generic in Python

This commit is contained in:
2026-06-29 14:24:38 +02:00
parent 78eba39ae3
commit 27f3fa7d1e
8 changed files with 26 additions and 18 deletions

View File

@@ -15,7 +15,7 @@ from midas.ast.location import Location
###> MidasType | Type annotations | node ###> MidasType | Type annotations | node
class BaseType: class BaseType:
base: str base: str
param: Optional[MidasType] args: tuple[MidasType, ...]
class ConstraintType: class ConstraintType:

View File

@@ -549,7 +549,13 @@ class PythonAstPrinter(
self._write_line("BaseType") self._write_line("BaseType")
with self._child_level(): with self._child_level():
self._write_line(f"base: {node.base}") self._write_line(f"base: {node.base}")
self._write_optional_child("param", node.param, last=True) self._write_line("args:", last=True)
with self._child_level():
for i, arg in enumerate(node.args):
self._idx = i
if i == len(node.args) - 1:
self._mark_last()
arg.accept(self)
def visit_constraint_type(self, node: p.ConstraintType) -> None: def visit_constraint_type(self, node: p.ConstraintType) -> None:
self._write_line("ConstraintType") self._write_line("ConstraintType")

View File

@@ -44,7 +44,7 @@ class MidasType(ABC):
@dataclass(frozen=True) @dataclass(frozen=True)
class BaseType(MidasType): class BaseType(MidasType):
base: str base: str
param: Optional[MidasType] args: tuple[MidasType, ...]
def accept(self, visitor: MidasType.Visitor[T]) -> T: def accept(self, visitor: MidasType.Visitor[T]) -> T:
return visitor.visit_base_type(self) return visitor.visit_base_type(self)

View File

@@ -761,9 +761,9 @@ class PythonTyper(
self.reporter.warning(node.location, f"Unknown type '{node.base}'") self.reporter.warning(node.location, f"Unknown type '{node.base}'")
return UnknownType() return UnknownType()
if node.param is not None: if len(node.args) != 0:
param: Type = self.resolve_type_expr(node.param) args: list[Type] = [self.resolve_type_expr(arg) for arg in node.args]
return self.types.apply_generic(base, [param]) return self.types.apply_generic(base, args)
return base return base
def visit_constraint_type(self, node: p.ConstraintType) -> Type: def visit_constraint_type(self, node: p.ConstraintType) -> Type:

View File

@@ -134,9 +134,9 @@ class PythonHighlighter(
def visit_base_type(self, node: p.BaseType) -> None: def visit_base_type(self, node: p.BaseType) -> None:
self.wrap(node, "base-type") self.wrap(node, "base-type")
if node.param is not None: for arg in node.args:
self.wrap(node.param, "param") self.wrap(arg, "arg")
node.param.accept(self) arg.accept(self)
def visit_constraint_type(self, node: p.ConstraintType) -> None: def visit_constraint_type(self, node: p.ConstraintType) -> None:
self.wrap(node, "constraint-type") self.wrap(node, "constraint-type")

View File

@@ -3,7 +3,7 @@ span {
--col: 108, 233, 108; --col: 108, 233, 108;
} }
&.param { &.arg {
--col: 103, 192, 224; --col: 103, 192, 224;
} }

View File

@@ -300,26 +300,28 @@ class PythonParser:
case ast.Subscript(value=ast.Name(id="Frame"), slice=schema): case ast.Subscript(value=ast.Name(id="Frame"), slice=schema):
return self._parse_frame_type(schema) return self._parse_frame_type(schema)
case ast.Subscript(value=ast.Name(id=name), slice=param): case ast.Subscript(value=ast.Name(id=name), slice=arg):
args: tuple[MidasType, ...] = (
tuple(self._parse_type(a) for a in arg.elts)
if isinstance(arg, ast.Tuple)
else (self._parse_type(arg),)
)
return BaseType( return BaseType(
location=loc, location=loc,
base=name, base=name,
param=self._parse_type(param), args=args,
) )
case ast.Name(id=name): case ast.Name(id=name):
return BaseType( return BaseType(
location=loc, location=loc,
base=name, base=name,
param=None, args=(),
) )
case ast.BinOp(left=left_expr, op=ast.Add(), right=right_expr): case ast.BinOp(left=left_expr, op=ast.Add(), right=right_expr):
left = self._parse_type(left_expr) left = self._parse_type(left_expr)
match left: match left:
case None:
raise InvalidSyntaxError()
# If chained constraints, separate base type and rebuild constraint # If chained constraints, separate base type and rebuild constraint
case ConstraintType(type=left_type, constraint=left_constraint): case ConstraintType(type=left_type, constraint=left_constraint):
constraint = ast.BinOp( constraint = ast.BinOp(
@@ -345,7 +347,7 @@ class PythonParser:
return BaseType( return BaseType(
location=loc, location=loc,
base="None", base="None",
param=None, args=(),
) )
case _: case _:

View File

@@ -98,7 +98,7 @@ class PythonAstJsonSerializer(
return { return {
"_type": "BaseType", "_type": "BaseType",
"base": node.base, "base": node.base,
"param": self._serialize_optional(node.param), "args": self._serialize_list(node.args),
} }
def visit_constraint_type(self, node: ConstraintType) -> dict: def visit_constraint_type(self, node: ConstraintType) -> dict: