Skip to content

Remove superfluous code now MicroPython supports inspect API for function signature inspection.#2387

Merged
ntoll merged 4 commits intomainfrom
fix-inspect-in-when-for-micropython
Oct 8, 2025
Merged

Remove superfluous code now MicroPython supports inspect API for function signature inspection.#2387
ntoll merged 4 commits intomainfrom
fix-inspect-in-when-for-micropython

Conversation

@ntoll
Copy link
Copy Markdown
Member

@ntoll ntoll commented Oct 7, 2025

Description

MicroPython didn't support inspect.signature until now. It meant we needed to "hack" around this fact. Now MicroPython supports inspect.signature this work removes the "hacky" code and adds a test to ensure MP continues to work as expected.

Changes

  • Remove the "hack" work-around, and ensure the code is idiomatic "standard" Python
  • Add a test to ensure the various callables work properly.

Checklist

  • I have checked make build works locally.
  • I have created / updated documentation for this change (if applicable).

raise

wrapper = func
else:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elif is_awaitable(func):
  # ...
else:
  # ...

maybe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional (and related else) is to check the number of args the receiving function expects.

TL;DR do you have:

def handler(event):
    ...

or

def handler():
    ...

Inside each branch is where the check for is_awaitable happens. Of course, it could have been done the other way around... but I didn't want to change the original code, just remove the "hack" for MicroPython now it's not needed.

Copy link
Copy Markdown
Contributor

@WebReflection WebReflection Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what it does and I've mentioned it was a nit ... but these two snippets are identical, the latter one is just (big imho) more logical:

current

def decorator(func):
    sig = inspect.signature(func)
    if sig.parameters:
        if is_awaitable(func):
            async def wrapper(event):
                return await func(event)
        else:
            wrapper = func
    else:
        if is_awaitable(func):
            async def wrapper(*args, **kwargs):
                return await func()
        # this is the last possible branch
        else:
            def wrapper(*args, **kwargs):
                return func()

proposed

def decorator(func):
    sig = inspect.signature(func)
    if sig.parameters:
        if is_awaitable(func):
            async def wrapper(event):
                return await func(event)
        else:
            wrapper = func
    elif is_awaitable(func):
        async def wrapper(*args, **kwargs):
            return await func()
    else:
        def wrapper(*args, **kwargs):
            return func()

anyway, as the logic is identical I am OK with this merged in, after all it might be a subjective style I just like to keep if/elses linear in the indentation whenever I can 😇

Copy link
Copy Markdown
Contributor

@WebReflection WebReflection left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nit you can ignore otherwise looks good to me

@ntoll ntoll merged commit ffc78ab into main Oct 8, 2025
2 checks passed
@ntoll ntoll deleted the fix-inspect-in-when-for-micropython branch October 8, 2025 08:27
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