From 1b700fc732e5b410ed6ec0ff3915ab4bbc5386fe Mon Sep 17 00:00:00 2001 From: Victor Adossi Date: Mon, 4 Aug 2025 17:53:03 +0900 Subject: [PATCH 1/4] fix(lang/py): componentize-py update This commit fixes the discrepancy due to changes in how `componentize-py` works that have been merged recently. Both bindings generation and the generated code have changed such that the guide is no longer correct as to what to import and implement -- the new docs prefer a `wit_world` import which is generated. --- .../src/language-support/python.md | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/component-model/src/language-support/python.md b/component-model/src/language-support/python.md index eacffc64..2daee5aa 100644 --- a/component-model/src/language-support/python.md +++ b/component-model/src/language-support/python.md @@ -28,21 +28,39 @@ world adder { If you want to generate bindings produced for the WIT world (for an IDE or typechecker), you can generate them using the `bindings` subcommand. Specify the path to the WIT interface with the world you are targeting: -```sh -$ componentize-py --wit-path wit/component/wit --world adder bindings . +```console +componentize-py --wit-path wit --world adder bindings . ``` > [!NOTE] > You do not need to generate the bindings in order to `componentize` in the next step. `componentize` will generate bindings on-the-fly and bundle them into the produced component. +> +> If you attempt to run bindings generation twice, it will fail if the `bindings` folder already exists. + +Bindings are generated in a folder called `wit_world` by default: + +``` + +├── wit +│   └── component.wit +└── wit_world + ├── exports + │   ├── add.py + │   └── __init__.py + ├── __init__.py + └── types.py +``` -Bindings were created in an `adder` package which contains an `Add` protocol with an `add` method that we can implement. +The `wit_world/exports` folder contains an `Add` protocol which has an `add` method that we can implement, +which represents the export defined in the `adder` world WIT. -To implement this interface, put the following code in a file called `app.py`: +To implement the `adder` world (in particular the `add` interface that is exported), put the following code +in a file called `app.py`: ```py -import adder +from wit_world import exports -class Add(adder.Adder): +class Add(exports.Adder): def add(self, x: int, y: int) -> int: return x + y ``` From 9371b87e18d7a499d564552846ceb62e4e7b14e0 Mon Sep 17 00:00:00 2001 From: Victor Adossi Date: Mon, 4 Aug 2025 17:55:56 +0900 Subject: [PATCH 2/4] fix(lang/py): remove section regarding exporting interface This commit removes the section regarding exporting interfaces (instead of functions) as the code was long ago changed to use interfaces by default. --- .../src/language-support/python.md | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/component-model/src/language-support/python.md b/component-model/src/language-support/python.md index 2daee5aa..0170b923 100644 --- a/component-model/src/language-support/python.md +++ b/component-model/src/language-support/python.md @@ -87,41 +87,6 @@ $ cargo run --release -- 1 2 ../path/to/add.wasm See [`componentize-py`'s examples](https://github.com/bytecodealliance/componentize-py/tree/main/examples) to try out build HTTP, CLI, and TCP components from Python applications. -### Building a Component that Exports an Interface - -The [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit) exports a function. However, you'll often prefer to export an interface, either to comply with an existing specification or to capture a set of functions and types that tend to go together. Let's expand our example world to export an interface rather than directly export the function. - -```wit -// add-interface.wit -package example:component; - -interface add { - add: func(x: u32, y: u32) -> u32; -} - -world example { - export add; -} -``` - -If you peek at the bindings, you'll notice that we now implement a class for the `add` interface rather than for the `example` world. This is a consistent pattern. As you export more interfaces from your world, you implement more classes. Our add example gets the slight update of: - -```py -# app.py -import example - -class Add(example.Example): - def add(self, a: int, b: int) -> int: - return a + b -``` - -Once again, compile an application to a Wasm component using the `componentize` subcommand: - -```sh -$ componentize-py --wit-path add-interface.wit --world example componentize app -o add.wasm -Component built successfully -``` - ## Running components from Python Applications Wasm components can also be invoked from Python applications. This section walks through using tooling From d971f6912e47f55bedd121184c1eea6c9e73c86f Mon Sep 17 00:00:00 2001 From: Victor Adossi Date: Mon, 4 Aug 2025 17:57:00 +0900 Subject: [PATCH 3/4] refactor(lang/py): use markdown include for WIT file --- component-model/src/language-support/python.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/component-model/src/language-support/python.md b/component-model/src/language-support/python.md index 0170b923..eaf1d0a0 100644 --- a/component-model/src/language-support/python.md +++ b/component-model/src/language-support/python.md @@ -15,15 +15,7 @@ Next, create or download the WIT world you would like to target. For this exampl world][adder-wit] with an `add` function (e.g. `wit/component.wit`): ```wit -package docs:adder@0.1.0; - -interface add { - add: func(x: u32, y: u32) -> u32; -} - -world adder { - export add; -} +{{#include ../../examples/tutorial/wit/adder/world.wit}} ``` If you want to generate bindings produced for the WIT world (for an IDE or typechecker), you can generate them using the `bindings` subcommand. Specify the path to the WIT interface with the world you are targeting: From 6c1b3f85080991c754df33109df4390017859154 Mon Sep 17 00:00:00 2001 From: Victor Adossi <123968127+vados-cosmonic@users.noreply.github.com> Date: Mon, 4 Aug 2025 19:30:24 +0900 Subject: [PATCH 4/4] fix(lang/py): exports interface in python docs Co-authored-by: Wolfgang Meier --- component-model/src/language-support/python.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/component-model/src/language-support/python.md b/component-model/src/language-support/python.md index eaf1d0a0..624a6c5f 100644 --- a/component-model/src/language-support/python.md +++ b/component-model/src/language-support/python.md @@ -52,7 +52,7 @@ in a file called `app.py`: ```py from wit_world import exports -class Add(exports.Adder): +class Add(exports.Add): def add(self, x: int, y: int) -> int: return x + y ```