python / python/mypy

stubgen and Cython modules

Offen
#7,542 7 Kommentare 9 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature priority-1-normal topic-stubgen
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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 a PyCFunction) that has the relevant attributes set -- this allows using inspect.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): ...

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das Beispiel mit dem Cython-Modul und stubgen -m test zu reproduzieren, und verfolge dann, wie stubgen Objekte vom Typ cython_function_or_method und die über __annotations__ und inspect.signature() offengelegten Signaturen verarbeitet. Die Aufgabe ist abgeschlossen, wenn die generierten Stubs die gezeigten Signaturen für f und MyClass.run beibehalten, anstatt generische Callable-Argumente zu verwenden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.