Skip to content

Conversation

dpgeorge
Copy link
Member

This implements a very basic inspect.signature() function.

At the moment it returns only a simple Signature instance with a parameters attribute that holds an OrderedDict whose length matches the arity of the input function (the number of arguments it takes).

So, the following code works and is compatible with CPython:

def f(a, b, *, c):
    pass

print(len(inspect.signature(f).parameters))

That should print 3.

I tested this on unix 64-bit, unix nanbox (32-bit), webassembly (32-bit) and a 32-bit bare-metal target.


The main aim here is to support PyScript which is currently using a very unreliable hack to work out if a Python function takes arguments or not: https://github.com/pyscript/pyscript/blob/main/core/src/stdlib/pyscript/events.py#L96-L126

@dpgeorge
Copy link
Member Author

@WebReflection @ntoll this will allow you to use inspect.signature() in your "when" event callback code.

@ntoll
Copy link

ntoll commented Sep 19, 2025

Bravo @dpgeorge 🎉 - this is really helpful for us (and removes another "messy" conditional around the type of interpreter in our code). 🏆

This implements a very basic `inspect.signature()` function.

At the moment it returns only a simple `Signature` instance with a
`parameters` attribute that holds an `OrderedDict` whose length matches the
arity of the input function (the number of arguments it takes).

So, the following code works and is compatible with CPython:

    def f(a, b, *, c):
        pass

    print(len(inspect.signature(f).parameters))

That should print 3.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the inspect-add-basic-signature branch from 854f976 to b4565b4 Compare October 1, 2025 14:08
@dpgeorge dpgeorge merged commit b4565b4 into micropython:master Oct 1, 2025
4 checks passed
@dpgeorge dpgeorge deleted the inspect-add-basic-signature branch October 1, 2025 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants