Skip to content

fix: use inspect for classmethod parameter signatures in createstubs#739

Draft
Copilot wants to merge 6 commits into
mainfrom
copilot/improve-inspect-module-usage
Draft

fix: use inspect for classmethod parameter signatures in createstubs#739
Copilot wants to merge 6 commits into
mainfrom
copilot/improve-inspect-module-usage

Conversation

Copilot AI commented May 3, 2026

Copy link
Copy Markdown
Contributor

createstubs.py used inspect to extract signatures for regular functions/methods but always emitted cls, *args, **kwargs for @classmethod methods (detected as bound_method on MicroPython), ignoring the inspect-derived params already computed above.

Root cause

In the bound_method branch of write_object_stub, the inspect-derived params variable (computed just above) was discarded and replaced with a hardcoded cls, *args, **kwargs string.

Changes

src/stubber/board/createstubs.py (1-line change, propagated to all variants via make-variants):

  • Replace hardcoded cls, *args, **kwargs with params.replace("self", "cls", 1) — uses the inspect-derived params when available, automatically falls back to cls, *args, **kwargs when inspect is unavailable or fails

tests/data/mpy_inspect.py:

  • Extended signature() to support bound_method (how classmethods appear on MicroPython) by extracting the underlying bytecode function from the bound_method memory layout (fun at offset 1 in mp_obj_bound_meth_t)
  • Includes a type-pointer safety check to avoid segfault on native/C classmethods like int.from_bytes; those raise NotImplementedError and fall back gracefully

tests/createstubs/inspect_test.py:

  • test_classmethod_uses_inspect_for_signature: simulates MicroPython's bound_method type using a CPython class named bound_method; verifies actual param names are used
  • test_classmethod_fallback_without_inspect: verifies cls, *args, **kwargs fallback when _use_inspect=False

tests/native/native_inspect_test.py:

  • test_classmethod_params_use_inspect: runs against the real MicroPython unix binary; verifies that inspect.signature() returns the correct parameter count (including cls) for a pure-Python classmethod

Before / After

# Before
@classmethod
def PyWorker(cls, *args, **kwargs) -> Incomplete: ...

# After (bytecode classmethods with inspect available)
@classmethod
def PyWorker(cls, x0, x1) -> Incomplete: ...

Copilot AI linked an issue May 3, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix inspect module usage in createstubs for classmethods fix: use inspect for classmethod parameter signatures in createstubs May 3, 2026
Copilot AI requested a review from Josverl May 3, 2026 21:20
Copilot finished work on behalf of Josverl May 3, 2026 21:20
@Josverl

Josverl commented May 3, 2026

Copy link
Copy Markdown
Owner

@copilot ,
I am very worried that you have based the code on Cpython , rather then on MicroPython behaviour , and have not included a single test against a unix micropython binary to make susre the code even runs.

Also i explicitly asked for a minimal change.
You have replace 1 line of code with 60 - which I would hardly call minimal

At his point this PR is simply lacking quality and will be rejected unless you can address this properly

Copilot AI commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

@copilot ,
I am very worried that you have based the code on Cpython , rather then on MicroPython behaviour , and have not included a single test against a unix micropython binary to make susre the ...

Reworked in commit d0cb00f. The changes are now:

  1. createstubs.py (1-line change): Replace hardcoded cls, *args, **kwargs with params.replace("self", "cls", 1) — the inspect-derived params are already computed just above; this simply uses them instead of ignoring them. Falls back to cls, *args, **kwargs automatically when inspect fails.

  2. tests/data/mpy_inspect.py: Extended signature() to support bound_method (how classmethods appear on MicroPython) by extracting the underlying bytecode function from the bound_method memory layout. Includes a type-pointer safety check to avoid segfault on native/C classmethods like int.from_bytes.

  3. Tests (tests/native/native_inspect_test.py): Added test_classmethod_params_use_inspect that runs directly against the MicroPython unix binary to verify inspect now returns the correct parameter count (3: cls+x+y) for a Python classmethod.

Copilot finished work on behalf of Josverl May 3, 2026 22:05
@codecov

codecov Bot commented May 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 69.42880% with 380 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.16%. Comparing base (8ba5c2e) to head (2da1cb6).

Files with missing lines Patch % Lines
src/stubber/board/createstubs_db_min.py 67.51% 107 Missing and 33 partials ⚠️
src/stubber/board/createstubs_mem_min.py 70.39% 86 Missing and 33 partials ⚠️
src/stubber/board/createstubs_min.py 70.76% 86 Missing and 33 partials ⚠️
src/stubber/board/createstubs_db.py 0.00% 1 Missing ⚠️
src/stubber/board/createstubs_mem.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #739      +/-   ##
==========================================
+ Coverage   72.36%   74.16%   +1.80%     
==========================================
  Files          71       71              
  Lines        8399     7792     -607     
  Branches     1541     1428     -113     
==========================================
- Hits         6078     5779     -299     
+ Misses       1823     1562     -261     
+ Partials      498      451      -47     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Josverl Josverl force-pushed the copilot/improve-inspect-module-usage branch from b54a9fc to 2da1cb6 Compare May 4, 2026 22:50
Copilot AI and others added 5 commits May 5, 2026 01:19
…ve test

- createstubs.py: replace hardcoded 'cls, *args, **kwargs' with
  params.replace('self', 'cls', 1) to use inspect-derived params
- mpy_inspect.py: add bound_method support to signature() with
  bytecode-safety check to avoid segfault on native classmethods
- tests: rewrite classmethod tests using MicroPython-style bound_method mock
- tests: add native MicroPython test for inspect on classmethod

Agent-Logs-Url: https://github.com/Josverl/micropython-stubber/sessions/bbd26efa-d963-4041-9548-22581cc0519a

Co-authored-by: Josverl <981654+Josverl@users.noreply.github.com>
@Josverl Josverl force-pushed the copilot/improve-inspect-module-usage branch from 2da1cb6 to 32eeff5 Compare May 4, 2026 23:19
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.

Inspect module not used for classmethod signature

2 participants