Skip to content

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

Open
@braxtonmckee

Description

@braxtonmckee

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerIssue that does not affect the un-compiled subset of typed_python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions