Skip to content

Commit

Permalink
Make node optional for type building
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-koch committed Nov 21, 2023
1 parent de454bd commit 4dba8a7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 3 additions & 1 deletion guppy/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions guppy/prelude/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
17 changes: 11 additions & 6 deletions guppy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 4dba8a7

Please sign in to comment.