Skip to content

Commit

Permalink
Improvements to stubgenc (#14564)
Browse files Browse the repository at this point in the history
This bundles together a handful of improvements to stubgenc. Each one
has its own commit, but it can be somewhat time consuming to get a PR
reviewed and merged, so I'm putting them together for the sake of
expediency. I'll break them up if it is preferred.

An overview of the main changes:

- infer return types for known special methods: previously only argument
types were inferred
- check class docstrings for signatures in addition to `__init__`:
shiboken binding generator is known to put constructor signatures in
class docstrings rather than on `__init__`
- use the list of analyzed modules to produce better imports: previously
when given `foo.Bar.Spangle`, it was assumed that `import foo.Bar`
should be added, however, it could be that `Bar.Spangle` refers to
nested classes within the module `foo`.
- when fixing up types, also process children of compound types
- evaluate descriptors when getting members: necessary for shiboken
bindings
  • Loading branch information
chadrik committed Apr 15, 2023
1 parent 1a47b19 commit 8f94d9a
Show file tree
Hide file tree
Showing 3 changed files with 373 additions and 147 deletions.
5 changes: 4 additions & 1 deletion mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,7 @@ def generate_stubs(options: Options) -> None:
)

# Separately analyse C modules using different logic.
all_modules = sorted(m.module for m in (py_modules + c_modules))
for mod in c_modules:
if any(py_mod.module.startswith(mod.module + ".") for py_mod in py_modules + c_modules):
target = mod.module.replace(".", "/") + "/__init__.pyi"
Expand All @@ -1728,7 +1729,9 @@ def generate_stubs(options: Options) -> None:
target = os.path.join(options.output_dir, target)
files.append(target)
with generate_guarded(mod.module, target, options.ignore_errors, options.verbose):
generate_stub_for_c_module(mod.module, target, sig_generators=sig_generators)
generate_stub_for_c_module(
mod.module, target, known_modules=all_modules, sig_generators=sig_generators
)
num_modules = len(py_modules) + len(c_modules)
if not options.quiet and num_modules > 0:
print("Processed %d modules" % num_modules)
Expand Down
Loading

0 comments on commit 8f94d9a

Please sign in to comment.