sematic icon indicating copy to clipboard operation
sematic copied to clipboard

Support __getattr__ for Future dataclasses

Open augray opened this issue 2 years ago • 0 comments

Ideally this would work:

@dataclass
class Foo:
    foo: int

@sematic.func
def make_foo(i: int) -> Foo:
    return Foo(foo=i)

@sematic.func
def get_int(foo: Foo) -> int:
    return make_foo(42).foo

But it doesn't because make_foo returns a future, and getitem isn't supported for futures wrapping dataclasses. But since dataclasses have type annotations, we could make this work.

There is a workaround, but it's kind of annoying for a simple field access:

@dataclass
class Foo:
    foo: int

@sematic.func
def make_foo(i: int) -> Foo:
    return Foo(foo=i)

@sematic.func
def get_int(foo: Foo) -> int:
    return get_foo_field(make_foo(42))

@sematic.func
def get_foo_field(foo: Foo) -> int:
    return foo.foo

augray avatar Aug 09 '22 23:08 augray