Commit 1e96b24
Fix class ordering to preserve inheritance hierarchy (pybind#231)
Classes in generated .pyi stubs were sorted alphabetically, causing
derived classes to appear before their base classes and breaking type
checkers.
Three changes fix this:
- Parser: use module.__dict__.items() instead of inspect.getmembers()
to preserve the pybind11 registration order (definition order)
- Printer: replace alphabetical sort with a configurable _order_classes()
dispatch supporting "definition" (default), "topological" (Kahn's
algorithm ensuring bases precede derived classes), and "alphabetical"
- CLI: add --sort-by option to select the class ordering strategy
The topological sort ignores external bases (from other modules) and
breaks ties by input position for deterministic output. Cyclic cross-
references between classes (e.g. aliases, method signatures) are not
inheritance cycles and are already handled by `from __future__ import
annotations` in the generated stubs.
Closes pybind#231
Based on the approaches in PR pybind#275 by @juelg and PR pybind#294 by
@daltairwalter, informed by review feedback from @skarndev and
@sizmailov.
Co-Authored-By: juelg <[email protected]>
Co-Authored-By: daltairwalter <[email protected]>
Co-Authored-By: skarndev <[email protected]>
Co-Authored-By: sizmailov <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>1 parent fcd02aa commit 1e96b24
3 files changed
Lines changed: 86 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
216 | 217 | | |
217 | 218 | | |
218 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
219 | 230 | | |
220 | 231 | | |
221 | 232 | | |
| |||
310 | 321 | | |
311 | 322 | | |
312 | 323 | | |
313 | | - | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
314 | 328 | | |
315 | 329 | | |
316 | 330 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
28 | 32 | | |
29 | 33 | | |
30 | 34 | | |
31 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
32 | 90 | | |
33 | | - | |
| 91 | + | |
34 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
35 | 102 | | |
36 | 103 | | |
37 | 104 | | |
| |||
90 | 157 | | |
91 | 158 | | |
92 | 159 | | |
93 | | - | |
| 160 | + | |
94 | 161 | | |
95 | 162 | | |
96 | 163 | | |
| |||
232 | 299 | | |
233 | 300 | | |
234 | 301 | | |
235 | | - | |
| 302 | + | |
236 | 303 | | |
237 | 304 | | |
238 | 305 | | |
| |||
0 commit comments