stubgen and Cython modules
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Hello,
I'm trying to use mypy with a Cython module (via stubgen).
Example Cython module (test.pyx):
import typing
def f(path: str, a: int = 0, b: bool = True) -> typing.List[str]:
return []
cdef class MyClass(object):
def __init__(self, name: str = None):
self.name = name
def run(self, action: str) -> None:
pass
Compile this module: cythonize -b test.pyx
The output will be a binary extension module (test..pyd/test..so).
Run stubgen on this via stubgen -m test.
In general, there's two ways in which Cython can expose the function signature:
#cython: embedsignature=True: Cython will embed the signature in the docstring. This has some issues in Cython, see cython/cython#3150.#cython: binding=True: Cython will use a custom function object (not aPyCFunction) that has the relevant attributes set -- this allows usinginspect.signature(). As of Cython 3, this option is enabled by default, and I'll only consider this option in the following.
Both f and MyClass.run are of type cython_function_or_method, which is neither a Python builtin function, nor a PyCFunction.
Instead, it's effectively a custom callable that has most of the attributes expected for a function:
>>> dir(test.f)
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__vectorcalloffset__', '_is_coroutine', 'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals', 'func_name']
>>> test.f.__annotations__
{'path': 'str', 'a': 'int', 'b': 'bool', 'return': 'typing.List[str]'}
>>> inspect.signature(test.MyClass.run)
<Signature (self, action: 'str') -> 'None'>
However stubgen fails to correctly handle this custom callable, and ends creating:
import _cython_3_1_3
__test__: dict
f: _cython_3_1_3.cython_function_or_method
class MyClass:
def __init__(self, *args, **kwargs) -> None: ...
def run(self, *args, **kwargs): ...
def __reduce__(self): ...
def __reduce_cython__(self, *args, **kwargs): ...
def __setstate_cython__(self, *args, **kwargs): ...
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、Cython モジュールと stubgen -m test を使って例を再現し、次に stubgen が cython_function_or_method オブジェクトと __annotations__ および inspect.signature() を通じて公開されるシグネチャをどのように処理するかを追跡します。f と MyClass.run の示されたシグネチャが、汎用的な Callable 引数を使うのではなく、生成されたスタブに保持されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100