Skip to content

webassembly: registered JS module function fails with named arguments #18190

Closed

Description

@WebReflection

Port, board and/or hardware

webassembly

MicroPython version

MicroPython v1.27.0-preview-252

Reproduction

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <script type="module">
      globalThis.ok = (...args) => {
        console.log("ok", args);
      };

      const base = 'https://cdn.jsdelivr.net/npm/@micropython/micropython-webassembly-pyscript@1.27.0-preview-252';
      const { loadMicroPython } = await import(`${base}/micropython.mjs`);
      const interpreter = await loadMicroPython({ url: `${base}/micropython.wasm` });
      interpreter.registerJsModule("issue", {
        test: (...args) => {
          console.log("test", args);
        }
      });
      const { textContent } = document.querySelector('script[type=micropython]');
      await interpreter.runPythonAsync(textContent);
    </script>
    <script type="micropython">
      # regular functions are fine
      from js import ok

      ok("a", { "b": "c" })
      # 'ok' ['a', Proxy(PyProxy)]

      ok("a", b = "c")
      # 'ok' ['a', {…}]

      # module functions are weird ...
      from issue import test

      test("a", { "b": "c" })
      # 'test' ['a', Proxy(PyProxy)]

      test("a", b = "c")
      # Uncaught PythonError: Traceback (most recent call last):
      # File "<stdin>", line 8, in <module>
      # TypeError: function expected at most 1 arguments, got 2

      # at proxy_convert_mp_to_js_obj_jsside (micropython.mjs:1:101294)
      # at proxy_convert_mp_to_js_obj_jsside_with_free (micropython.mjs:1:103306)
      # at Object.runPythonAsync (micropython.mjs:1:92725)
      # at (index):20:25
    </script>
  </head>
</html>

Expected behaviour

With latest we tested all our integration tests and everything is fine except the fact we register utilities that accept objects/setup/init references and we are incapable of using the more Pythonic syntax at the Python level which used to work just like these work on globally exposed callbacks.

Something seems off when **kw args are around instead of dictionaries, we expect those named arguments to be passed as dictionary like these do already, and have done, for a while, thanks.

Observed behaviour

The error is thrown for no concrete reason but it's also misleading as it passes two args if no named argument is ued.

Additional Information

@dpgeorge is already aware of this issue which has now been formalized.

Code of Conduct

Yes, I agree

Activity

  1. dpgeorge commented on Oct 2, 2025

    @dpgeorge
    Member

    Thanks, I can reproduce the issue.

    For the record TypeError: function expected at most 1 arguments, got 2 is a correct error message, it means that the function takes at most 1 argument, but was called with 2. Eg (lambda a:0)(1, 2) would give that error message, also under CPython.

  2. WebReflection commented on Oct 2, 2025

    @WebReflection
    ContributorAuthor

    For the record TypeError: function expected at most 1 arguments, got 2 is a correct error message

    well, test('a', { 'b': 'c' }) uses two arguments and just works, so it surprises me test('a', b = 'c') considers test as a function that accepts only one argument, when it results into test('a', { 'b': 'c' }) on the other side like the ok function demonstrates.

    That's the misleading / gotcha I was referring to, thanks for confirming and taking a look ... it's probably the only thing that keeps blocking us from updating to latest MicroPython with all its goodness backed in!

  3. dpgeorge commented on Oct 2, 2025

    @dpgeorge
    Member

    Should be fixed by #18191.

  4. WebReflection commented on Oct 2, 2025

    @WebReflection
    ContributorAuthor

    Thanks, that looks great and well tested, all expectations will be met, please do publish to npm once it's green and merged so we can finally update MicroPython 🙏

    edit never mind, we're on pyscript/polyscript#153

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions