Stubgen doesn't look at C type's docstring when inferring __init__'s type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
Given a class with a docstring similar to "ClassName(initparam: str)" or "ClassName(initparam: str) -> ClassName" or "(initparam: str)" or "__init__(initparam: str) -> None" the stub generation via runtime introspection will not digest this information.
This is relevant, because the CPython API doesn't allow setting the __init__ method's docstring, like other members' or type objects', making automatic annotation of constructors impossible.
To Reproduce
You have to build a C extension, but I can provide modules if needed for testing.
PyTypeObject TestType_ = { \
PyVarObject_HEAD_INIT(NULL, 0) \
/* tp_name */ "module.Test", \
/* tp_basicsize */ sizeof(t_test), \
/* tp_itemsize */ 0, \
/* tp_dealloc */ (destructor)dealloc, \
/* tp_print */ 0, \
/* tp_getattr */ 0, \
/* tp_setattr */ 0, \
/* tp_compare */ 0, \
/* tp_repr */ 0, \
/* tp_as_number */ 0, \
/* tp_as_sequence */ 0, \
/* tp_as_mapping */ 0, \
/* tp_hash */ 0, \
/* tp_call */ 0, \
/* tp_str */ 0, \
/* tp_getattro */ 0, \
/* tp_setattro */ 0, \
/* tp_as_buffer */ 0, \
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, \
/* tp_doc */ PyDoc_STR("Test(param: int)"), \
/* tp_traverse */ 0, \
/* tp_clear */ 0, \
/* tp_richcompare */ 0, \
/* tp_weaklistoffset */ 0, \
/* tp_iter */ 0, \
/* tp_iternext */ 0, \
/* tp_methods */ t_test_methods, \
/* tp_members */ 0, \
/* tp_getset */ 0, \
/* tp_base */ 0, \
/* tp_dict */ 0, \
/* tp_descr_get */ 0, \
/* tp_descr_set */ 0, \
/* tp_dictoffset */ 0, \
/* tp_init */ (initproc)t_type_init, \
/* tp_alloc */ 0, \
/* tp_new */ 0, \
};
Expected Behavior
Stubgen should generate the lines in .pyi:
class Test():
def __init__ (self, param: int) -> None: ...
[other methods]
Actual Behavior
Stubgen instead generates:
class Test():
def init(self, *args, **kwargs) -> None
[other methods]
Your Environment
- Mypy version used: 0.990
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): Defaults/None - Python version used: 3.10
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dal punto di ingresso dell’introspezione a runtime di stubgen e analizza come vengono gestite le docstring delle classi e le firme di init. Riproduci il caso di C-extension descritto nell’issue, quindi aggiungi la copertura per le forme di docstring elencate. Il lavoro è completo quando l’output .pyi generato usa i parametri del costruttore ricavati dalla docstring del tipo invece di argomenti generici.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100