python / python/mypy

stubgen failed to find function signature from native modules

Open
#12,178 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-stubgen
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Feature

I have written a native module with a hand-written docstring and signature.
The signature is available via

import inspect
import mymod

inspect.signature(mymod.MyClass.method1)
# or
help(mymod.MyClass)
# correctly prints (a,b,c)

However stubgen -p mymod generates .pyi with

class Myclass:
  @classmethod
  def __init__(cls, *args, **kwargs) -> None: ...
  def method1(self, *args, **kwargs) -> Any: ...

filled with *args and **kwargs instead of correct signatures. (Worse, method1 is in fact staticmethod so vscode complains with this stub)

The binding is written with rust pyo3.
Since all inspect.signature, help, and IPython can read the signature, I think the problem is in stubgen.
I don't know the internals of stubgen, but would it help to use inspect.signature when other methods in generate_stub_for_c_module fail?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading stubgen's generate_stub_for_c_module path and compare its current signature-discovery fallbacks with inspect.signature on the native module example. Done means stubgen emits the documented parameter signatures and preserves method kinds such as staticmethod instead of using generic *args and **kwargs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.