APrioriInvestments / typed_python

An llvm-based framework for generating and calling into high-performance native code from Python.
Apache License 2.0
198 stars 8 forks source link

Distinction between FunctionType closure using PyCell and TypedCell is confusing #419

Open braxtonmckee opened 1 year ago

braxtonmckee commented 1 year ago

I wrote this code:

from typed_python import TypeFunction, Class, Final, Member, Entrypoint

class Callback(Class):
    def execute(self) -> None:
        raise NotImplementedError(self)

@TypeFunction
def TypedCallback(T):
    class TypedCallback(Callback, Final, __name__=f"TypedCallback({T.__name__})"):
        callback = Member(T)

        def __init__(self, callback):
            self.callback = callback

        def execute(self):
            self.callback()

    return TypedCallback

@Entrypoint
def makeCallback(callback):
    return TypedCallback(type(callback))(callback)

x = ListOf(int)()

@Entrypoint
def f():
    x.append(10)

c = makeCallback(f)

Depending on whether i have 'Entrypoint' on makeCallback, I get completely different behavior because with the Entrypoint I get a FunctionType that has typed closures, and without it I get one with PyCell (which we can't compile).

We need to tighten up the way we handle lambdas so that we can use them like this with coherent semantics.