Commit e435594
stubgen: unify C extension and pure python stub generators with object oriented design (#15770)
This MR is a major overhaul to `stubgen`. It has been tested extensively
in the process of creating stubs for multiple large and varied libraries
(detailed below).
## User story
The impetus of this change is as follows: as a maintainer of third-party
stubs I do _not_ want to use `stubgen` as a starting point for
hand-editing stub files, I want a framework to regenerate stubs against
upstream changes to a library.
## Summary of Changes
- Introduces an object-oriented design for C extension stub generation,
including a common base class that is shared between inspection-based
and parsing-based stub generation.
- Generally unifies and harmonizes the behavior between inspection and
parsing approaches. For example, function formatting, import tracking,
signature generators, and attribute filtering are now handled with the
same code.
- Adds support for `--include-private` and `--export-less` to
c-extensions (inspection-based generation).
- Adds support for force enabling inspection-based stub generation (the
approach used for C extensions) on pure python code using a new
`--inspect-mode` flag. Useful for packages that employ dynamic function
or class factories. Also makes it possible to generate stubs for
pyc-only modules (yes, this is a real use case)
- Adds an alias `--no-analysis` for `--parse-only` to clarify the
purpose of this option.
- Removes filtering of `__version__` attribute from modules: I've
encountered a number of cases in real-world code that utilize this
attribute.
- Adds a number of tests for inspection mode. Even though these run on
pure python code they increase coverage of the C extension code since it
shares much of hte same code base.
Below I've compiled some basic information about each stub library that
I've created using my changes, and a link to the specialized code for
procedurally generating the stubs.
| Library | code type | other notes |
| --- | --- | --- |
|
[USD](https://github.com/LumaPictures/cg-stubs/blob/master/usd/stubgen_usd.py)
| boost-python | integrates types from doxygen |
|
[katana](https://github.com/LumaPictures/cg-stubs/blob/master/katana/stubgen_katana.py)
| pyc and C extensions | uses epydoc docstrings. has pyi-only packages |
|
[mari](https://github.com/LumaPictures/cg-stubs/blob/master/mari/stubgen_mari.py)
| pure python and C extensions | uses epydoc docstrings |
|
[opencolorio](https://github.com/LumaPictures/cg-stubs/blob/master/ocio/stubgen_ocio.py)
| pybind11 | |
|
[pyside2](https://github.com/LumaPictures/cg-stubs/blob/master/pyside/stubgen_pyside.py)
| shiboken | |
| substance_painter | pure python | basic / non-custom. reads types from
annotations |
| pymel | pure python | integrates types parsed from custom docs |
I know that this is a pretty big PR, and I know it's a lot to go
through, but I've spent a huge amount of time on it and I believe this
makes mypy's stubgen tool the absolute best available. If it helps, I
also have 13 merged mypy PRs under my belt and I'll be around to fix any
issues if they come up.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <[email protected]>1 parent ff9deb3 commit e435594
12 files changed
Lines changed: 2125 additions & 1442 deletions
File tree
- docs/source
- mypy
- test
- test-data
- pybind11_mypy_demo/stubgen/pybind11_mypy_demo
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
| 130 | + | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
136 | 146 | | |
137 | 147 | | |
138 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
42 | 46 | | |
43 | 47 | | |
44 | 48 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
| |||
35 | 38 | | |
36 | 39 | | |
37 | 40 | | |
38 | | - | |
39 | | - | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
44 | 51 | | |
45 | 52 | | |
46 | 53 | | |
| |||
59 | 66 | | |
60 | 67 | | |
61 | 68 | | |
62 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
63 | 143 | | |
64 | 144 | | |
65 | 145 | | |
| |||
176 | 256 | | |
177 | 257 | | |
178 | 258 | | |
179 | | - | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
180 | 265 | | |
181 | 266 | | |
182 | 267 | | |
183 | 268 | | |
184 | 269 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | 270 | | |
191 | 271 | | |
192 | 272 | | |
| |||
240 | 320 | | |
241 | 321 | | |
242 | 322 | | |
243 | | - | |
| 323 | + | |
244 | 324 | | |
245 | 325 | | |
246 | 326 | | |
| |||
0 commit comments