diff --git a/guppy/decorator.py b/guppy/decorator.py index 6220c8aa..67948cec 100644 --- a/guppy/decorator.py +++ b/guppy/decorator.py @@ -98,7 +98,9 @@ class NewType(GuppyType): name = _name @staticmethod - def build(*args: GuppyType, node: AstNode) -> "GuppyType": + def build( + *args: GuppyType, node: Optional[AstNode] = None + ) -> "GuppyType": # At the moment, custom types don't support type arguments. if len(args) > 0: raise GuppyError( diff --git a/guppy/prelude/_internal.py b/guppy/prelude/_internal.py index 39816296..7a06ec5d 100644 --- a/guppy/prelude/_internal.py +++ b/guppy/prelude/_internal.py @@ -101,9 +101,7 @@ def synthesize(self, args: list[ast.expr]) -> tuple[ast.expr, GuppyType]: call = with_loc( self.node, GlobalCall(func=Int.__float__, args=[args[i]]) ) - args[i] = with_type( - self.ctx.globals.types["float"].build(node=self.node), call - ) + args[i] = with_type(self.ctx.globals.types["float"].build(), call) return super().synthesize(args) diff --git a/guppy/types.py b/guppy/types.py index 4cfa330e..ca5c010a 100644 --- a/guppy/types.py +++ b/guppy/types.py @@ -20,7 +20,7 @@ class GuppyType(ABC): @staticmethod @abstractmethod - def build(*args: "GuppyType", node: AstNode) -> "GuppyType": + def build(*args: "GuppyType", node: Optional[AstNode] = None) -> "GuppyType": pass @property @@ -53,7 +53,7 @@ def __str__(self) -> str: return f"({', '.join(str(a) for a in self.args)}) -> {self.returns}" @staticmethod - def build(*args: GuppyType, node: AstNode) -> GuppyType: + def build(*args: GuppyType, node: Optional[AstNode] = None) -> GuppyType: # Function types cannot be constructed using `build`. The type parsing code # has a special case for function types. raise NotImplementedError() @@ -71,7 +71,12 @@ class TupleType(GuppyType): name: str = "tuple" @staticmethod - def build(*args: GuppyType, node: AstNode) -> GuppyType: + def build(*args: GuppyType, node: Optional[AstNode] = None) -> GuppyType: + from guppy.error import GuppyError + + # TODO: Parse empty tuples via `tuple[()]` + if len(args) == 0: + raise GuppyError("Tuple type requires generic type arguments", node) return TupleType(list(args)) def __str__(self) -> str: @@ -91,7 +96,7 @@ class SumType(GuppyType): element_types: Sequence[GuppyType] @staticmethod - def build(*args: GuppyType, node: AstNode) -> GuppyType: + def build(*args: GuppyType, node: Optional[AstNode] = None) -> GuppyType: # Sum types cannot be parsed and constructed using `build` since they cannot be # written by the user raise NotImplementedError() @@ -118,7 +123,7 @@ class NoneType(GuppyType): linear: bool = False @staticmethod - def build(*args: GuppyType, node: AstNode) -> GuppyType: + def build(*args: GuppyType, node: Optional[AstNode] = None) -> GuppyType: if len(args) > 0: from guppy.error import GuppyError @@ -144,7 +149,7 @@ def __init__(self) -> None: super().__init__([TupleType([]), TupleType([])]) @staticmethod - def build(*args: GuppyType, node: AstNode) -> GuppyType: + def build(*args: GuppyType, node: Optional[AstNode] = None) -> GuppyType: if len(args) > 0: from guppy.error import GuppyError