python / python/mypy

`stubgen --inspect-mode` fails while non-inspect mode works ok

Aperta
#17,725 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

stubgen with --inspect-mode fails obscurely on some python module, where it succeeds with the AST based generator. said python module imports a C module.

this can easily be reproduced like so:

To Reproduce

$ pip install mypy jpype
...
from mypy import stubgen

# this doesn't fail, but doesn't incorporate symbols from the indirectly imported C module:
options = stubgen.parse_options(" --verbose  -p jpype -p _jpype".split())
stubgen.generate_stubs(options)

# this raises an obscure error:
options = stubgen.parse_options("--inspect-mode  --verbose  -p jpype -p _jpype".split())
stubgen.generate_stubs(options)

Expected Behavior

no failure

Actual Behavior

In [1]: from mypy import stubgen
In [2]: options = stubgen.parse_options("--inspect-mode --verbose -m jpype._classpath".split())
   ...: stubgen.generate_stubs(options)
Trying to import 'jpype._classpath' for runtime introspection
Processing 1 files...
Processing jpype._classpath
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 3
      1 options = stubgen.parse_options("--inspect-mode --verbose -m jpype._classpath".split())
----> 3 stubgen.generate_stubs(options)

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgen.py:1703, in generate_stubs()

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgen.py:1661, in generate_stub_for_py_module()

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgenc.py:419, in InspectionStubGenerator.generate_module(self)
    417 for name, obj in items:
    418     if self.is_function(obj):
--> 419         self.generate_function_stub(name, obj, output=functions)
    420     elif inspect.isclass(obj):
    421         self.generate_class_stub(name, obj, output=types)

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgenc.py:600, in InspectionStubGenerator.generate_function_stub(self, name, obj, output, class_info)
    598 default_sig = self.get_default_function_sig(obj, ctx)
    599 inferred = self.get_signatures(default_sig, self.sig_generators, ctx)
--> 600 self.process_inferred_sigs(inferred)
    602 decorators = []
    603 if len(inferred) > 1:

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgenc.py:569, in InspectionStubGenerator.process_inferred_sigs(self, inferred)
    567 for arg in sig.args:
    568     if arg.type is not None:
--> 569         arg.type = self.strip_or_import(arg.type)
    570 if sig.ret_type is not None:
    571     inferred[i] = sig._replace(ret_type=self.strip_or_import(sig.ret_type))

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/stubgenc.py:365, in InspectionStubGenerator.strip_or_import(self, type_name)
    356 """Strips unnecessary module names from typ.
    357
    358 If typ represents a type that is inside module or is a type coming from builtins, remove
   (...)
    362     typ: name of the type
    363 """
    364 local_modules = ["builtins", self.module_name]
--> 365 parsed_type = parse_type_comment(type_name, 0, 0, None)[1]
    366 assert parsed_type is not None, type_name
    367 return self.print_annotation(parsed_type, self.known_modules, local_modules)

File ~/tool_Studio/pysos/312/lib/python3.12/site-packages/mypy/fastparse.py:284, in parse_type_comment()

TypeError: str object expected; got type

Your Environment

(312) suob261820@DEL02573:~/tool_Studio$ pip list --local
Package           Version
----------------- -------
ipython           8.26.0
JPype1            1.5.0
mypy              1.11.2
mypy-extensions   1.0.0
...
(312) suob261820@DEL02573:~/tool_Studio$ python --version
Python 3.12.5

but it fails the same on python 3.10

and it fails the same with jpype actually being active, like so:

In [1]: from mypy import stubgen
   ...: import jpype
   ...: jpype.startJVM()

In [2]: options = stubgen.parse_options("--inspect-mode --verbose -m jpype._classpath --out wjvm".split())
   ...: stubgen.generate_stubs(options)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in mypy/stubgenc.py, in corrispondenza di InspectionStubGenerator.strip_or_import e process_inferred_sigs, poi esamina fastparse.py, dove parse_type_comment genera l’errore segnalato. Riproduci il problema con jpype e stubgen --inspect-mode usando i comandi dell’issue. Il lavoro è completato quando la modalità inspect gestisce i tipi dei moduli C importati senza errori, preservando il comportamento esistente al di fuori della modalità inspect.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.