python / python/mypy

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

Open
#17,725 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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)

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.py at InspectionStubGenerator.strip_or_import and process_inferred_sigs, then review fastparse.py where parse_type_comment raises the reported error. Reproduce the failure with jpype and stubgen --inspect-mode using the commands in the issue. Done means inspect mode handles the imported C-module types without failing while preserving the existing non-inspect behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.