bytecodealliance / componentize-py

Apache License 2.0
130 stars 13 forks source link

Type checking fails when records are defined in a world with functions #88

Open alec-deason opened 2 months ago

alec-deason commented 2 months ago

I think this is actually a bug but it may not be one that idiomatic wit files would trigger so it may not be an important issue but I'll post it anyway just in case.

When records are defined in a world along side functions which use the record type, the functions are generated first in the binding's init.py file so that type checking fails due to the undefined type.

For example, this wit file will not produce usable bindings:

package example:broken;

world broken-world {
    record a-record {
        field: s32
    }

    import get-a-record: func() -> a-record;
    export do-stuff: func() -> a-record;
}

My naive fix was to stick from __future__ import annotations at the top of the __init__.py file and that does work. But then I realized that the idiomatic thing to do seems to be to put type definitions in a separate types interface and import from there. Doing that produces valid bindings without having to change componentize_py at all.

dicej commented 2 months ago

Good catch. Glad you found a workaround, but we should also fix componentize-py so it generates valid bindings for any valid WIT world, and ideally without having to use __future__ if possible. I'll play around with it when I get a chance.