Skip to content
Closed
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
9 changes: 4 additions & 5 deletions opshin/type_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def type_from_annotation(self, ann: expr):

def visit_sequence(self, node_seq: typing.List[stmt]) -> plt.AST:
additional_functions = []
for n in node_seq:
for i, n in enumerate(node_seq):
if not isinstance(n, ast.ClassDef):
continue
non_method_attributes = []
Expand Down Expand Up @@ -675,12 +675,11 @@ def visit_sequence(self, node_seq: typing.List[stmt]) -> plt.AST:
custom_fix_missing_locations(ann, attribute.args.args[0])
ann.orig_id = attribute.args.args[0].orig_arg
func.args.args[0].annotation = ann
additional_functions.append(func)
additional_functions.append((i, func))
n.body = non_method_attributes
if additional_functions:
last = node_seq.pop()
node_seq.extend(additional_functions)
node_seq.append(last)
for i, f in reversed(additional_functions):
node_seq.insert(i + 1, f)

stmts = []
prevtyps = {}
Expand Down
24 changes: 24 additions & 0 deletions tests/test_class_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,27 @@ def validator(a: int, b: int, c:int) -> bool:
"""
ret = eval_uplc_value(source_code, 1, 2, 3)
self.assertEqual(ret, True)

@given(x=st.integers(), y=st.integers())
@example(x=0, y=0)
def test_method_back_ref(self, x: int, y: int):
source_code = """
from typing import Self
from opshin.prelude import *
@dataclass()
class Foo(PlutusData):
a: int

def __ge__(self, other: Self) -> bool:
return self.a >= other.a

def compare_foos(foo1: Foo, foo2: Foo) -> bool:
return foo1 >= foo2

def validator(a: int, b: int) -> bool:
foo1 = Foo(a)
foo2 = Foo(b)
return compare_foos(foo1, foo2)
"""
ret = eval_uplc_value(source_code, x, y)
self.assertEqual(ret, x >= y)
Loading