python / python/mypy

stubgen (stubgenc): Support for __new__ in C types

Open
#10,493 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

Implement support for __new__ in mypy.stubgenc.generate_c_type_stub

Pitch

Currently, there is a # TODO: We should support __new__. in mypy.stubgenc.generate_c_type_stub.
AFAIK, C extensions autogenerate the docstring Create and return a new object. See help(type) for accurate signature. for the __new__ / __init__ attributes of C type objects and I am not aware of a way to customize this.
Therefore, my proposal would be to use the docstring of the C type itself and scan for TypeName(...) (if TypeName is the name of the C type) and pass this to infer_sig_from_docstring.
Not sure if TypeName(...) should be required to include self / cls or if such an argument should be made optional (automatically add self to the signature of __init__ if it is missing in TypeName(...)).

I would offer to implement this, if we could specify the desired behavior.

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 in mypy.stubgenc.generate_c_type_stub and read how it currently handles C type attributes and calls infer_sig_from_docstring. Resolve the intended parsing and self/cls behavior for TypeName(...) before implementing it. Done means generated C type stubs include the agreed new and init signatures derived from the type docstring.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.