-
Notifications
You must be signed in to change notification settings - Fork 198
/
stubgen.py
executable file
·1474 lines (1239 loc) · 52.2 KB
/
stubgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# pyright: strict
"""
stubgen.py: nanobind stub generation tool
This file provides both an API (``nanobind.stubgen.StubGen``) and a command
line interface to generate stubs for nanobind extensions.
To generate stubs on the command line, invoke the stub generator with a module
name, which will place the newly generated ``.pyi`` file directly into the
module folder.
```
python -m nanobind.stubgen <module name>
```
Specify ``-o <filename>`` or ``-O <path>`` to redirect the output somewhere
else in case this is not desired.
To programmatically generate stubs, construct an instance of the ``StubGen``
class and repeatedly call ``.put()`` to register modules or contents within the
modules (specific methods, classes, etc.). Afterwards, the ``.get()`` method
returns a string containing the stub declarations.
```
from nanobind.stubgen import StubGen
import my_module
sg = StubGen()
sg.put(my_module)
print(sg.get())
```
Internals:
----------
The implementation of stubgen performs a DFS traversal of the loaded module and
directly generates the stub in lockstep. There are no ASTs or other
intermediate data structures to keep things as simple as possible. Every kind
of object that could be encountered (functions, properties, values, types) has
a corresponding ``put_..`` method, along with the main ``put()`` entry point
that dispatches to the others as appropriate.
Internally, stub generation involves two potentially complex steps: converting
nanobind overload chains into '@overload' declarations that can be understood
by the 'typing' module, and turning default values back into Python
expressions. To make both steps well-defined, the implementation relies on an
internal ``__nb_signature__`` property that nanobind functions expose
specifically to simplify stub generation.
(Note that for now, the StubGen API is considered experimental and not subject
to the semantic versioning policy used by the nanobind project.)
"""
import argparse
import builtins
from inspect import Signature, Parameter, signature, ismodule, getmembers
import textwrap
import importlib
import importlib.machinery
import importlib.util
import types
import typing
from dataclasses import dataclass
from typing import Dict, Sequence, List, Optional, Tuple, cast, Generator, Any, Callable, Union, Protocol, Literal
from pathlib import Path
import re
import sys
if sys.version_info < (3, 9):
from typing import Match, Pattern
else:
from re import Match, Pattern
if sys.version_info < (3, 11):
try:
import typing_extensions
except ImportError:
raise RuntimeError(
"stubgen.py requires the 'typing_extensions' package on Python <3.11"
)
else:
typing_extensions = None
# Standard operations supported by arithmetic enumerations
# fmt: off
ENUM_OPS = [
"add", "sub", "mul", "floordiv", "eq", "ne", "gt", "ge", "lt", "le",
"index", "repr", "hash", "int", "rshift", "lshift", "and", "or", "xor",
"neg", "abs", "invert",
]
# Exclude various standard elements found in modules, classes, etc.
SKIP_LIST = [
"__doc__", "__module__", "__name__", "__new__", "__builtins__",
"__cached__", "__path__", "__version__", "__spec__", "__loader__",
"__package__", "__nb_signature__", "__class_getitem__", "__orig_bases__",
"__file__", "__dict__", "__weakref__", "@entries"
]
# fmt: on
# This type is used to track per-module imports (``import name as desired_name``)
# during stub generation. The actual name in the stub is given by the value element.
# (name, desired_as_name) -> actual_as_name
ImportDict = Dict[Tuple[Optional[str], Optional[str]], Optional[str]]
# This type maps a module name to an `ImportDict` tuple that tracks the
# import declarations from that module.
# package_name -> ((name, desired_as_name) -> actual_as_name)
PackagesDict = Dict[str, ImportDict]
# Type of an entry of the ``__nb_signature__`` tuple of nanobind functions.
# It stores a function signature string, docstring, and a tuple of default function values.
# (signature_str, doc_str, (default_arg_1, default_arg_2, ...))
NbSignature = Tuple[Optional[str], Optional[str]]
# Type of an entry of the ``__nb_signature__`` tuple of nanobind functions.
NbFunctionSignature = Tuple[Optional[str], Optional[str], Optional[Tuple[Any, ...]]]
# Type of an entry of the ``__nb_signature__`` tuple of nanobind getters and setters.
NbGetterSetterSignature = Tuple[str, str]
class NamedObject(Protocol):
"""
Typing protocol representing an object with __name__ and __module__ members
"""
__module__: str
__name__: str
class NbFunction(Protocol):
"""
Typing protocol representing a nanobind function with its __nb_signature__ property
"""
__module__: Literal["nanobind"]
__name__: Literal["nb_func", "nb_method"]
__nb_signature__: Tuple[NbFunctionSignature, ...]
__call__: Callable[..., Any]
@typing.runtime_checkable
class NbGetterSetter(Protocol):
__nb_signature__: Tuple[NbGetterSetterSignature, ...]
class NbStaticProperty(Protocol):
"""Typing protocol representing a nanobind static property"""
__module__: Literal["nanobind"]
__name__: Literal["nb_static_property"]
fget: NbGetterSetter
fset: NbGetterSetter
class NbType(Protocol):
"""typing protocol representing a nanobind type object"""
__module__: Literal["nanobind"]
__name__: Literal["nb_type"]
__nb_signature__: str
__bases__: Tuple[Any, ...]
@dataclass
class ReplacePattern:
"""
A compiled query (regular expression) and replacement pattern. Patterns can
be loaded using the ``load_pattern_file()`` function defined below
"""
# A replacement patterns as produced by ``load_pattern_file()`` below
query: Pattern[str]
lines: List[str]
matches: int
class StubGen:
def __init__(
self,
module: types.ModuleType,
recursive: bool = False,
include_docstrings: bool = True,
include_private: bool = False,
include_internal_imports: bool = True,
include_external_imports: bool = False,
max_expr_length: int = 50,
patterns: List[ReplacePattern] = [],
quiet: bool = True,
output_file: Optional[Path] = None
) -> None:
# Module to check for name conflicts when adding helper imports
self.module = module
# Include docstrings in the generated stub?
self.include_docstrings = include_docstrings
# Recurse into submodules?
self.recursive = recursive
# Include private members that start or end with a single underscore?
self.include_private = include_private
# Include types and functions imported from the same package (but a different module)
self.include_internal_imports = include_internal_imports
# Include types and functions imported from external packages?
self.include_external_imports = include_external_imports
# Maximal length (in characters) before an expression gets abbreviated as '...'
self.max_expr_length = max_expr_length
# Replacement patterns as produced by ``load_pattern_file()`` below
self.patterns = patterns
# Set this to ``True`` if output to stdout is unacceptable
self.quiet = quiet
# Target filename, only needed for recursive stub generation
self.output_file = output_file
# ---------- Internal fields ----------
# Current depth / indentation level
self.depth = 0
# Output will be appended to this string
self.output = ""
# A stack to avoid infinite recursion
self.stack: List[object] = []
# An identifier associated with the top element of the stack
self.prefix = module.__name__
# Dictionary to keep track of import directives added by the stub generator
# Maps package_name -> ((name, desired_as_name) -> actual_as_name)
self.imports: PackagesDict = {}
# ---------- Regular expressions ----------
# Negative lookbehind matching word boundaries except '.'
sep_before = r"(?<![\\B\.])"
# Negative lookforward matching word boundaries except '.'
sep_after = r"(?![\\B\.])"
# Regexp matching a Python identifier
identifier = r"[^\d\W]\w*"
# Precompile RE a sequence of identifiers separated by periods
self.id_seq = re.compile(
sep_before
+ "((?:"
+ identifier
+ r"\.)+)("
+ identifier
+ r")\b"
+ sep_after
)
# Precompile RE to extract nanobind nd-arrays
self.ndarray_re = re.compile(
sep_before + r"(numpy\.ndarray|ndarray|torch\.Tensor)\[([^\]]*)\]"
)
# Types which moved from typing.* to collections.abc in Python 3.9
self.abc_re = re.compile(
'typing.(AsyncGenerator|AsyncIterable|AsyncIterator|Awaitable|Callable|'
'Collection|Container|Coroutine|Generator|Hashable|ItemsView|'
'Iterable|Iterator|KeysView|Mapping|MappingView|MutableMapping|'
'MutableSequence|MutableSet|Sequence|ValuesView)'
)
# Should we insert a dummy base class to handle enumerations?
self.abstract_enum = False
self.abstract_enum_arith = False
def write(self, s: str) -> None:
"""Append raw characters to the output"""
self.output += s
def write_ln(self, line: str) -> None:
"""Append an indented line"""
if len(line) != 0 and not line.isspace():
self.output += " " * self.depth + line
self.output += "\n"
def write_par(self, line: str) -> None:
"""Append an indented paragraph"""
self.output += textwrap.indent(line, " " * self.depth)
def put_docstr(self, docstr: str) -> None:
"""Append an indented single or multi-line docstring"""
docstr = textwrap.dedent(docstr).strip()
raw_str = ""
if "''" in docstr or "\\" in docstr:
# Escape all double quotes so that no unquoted triple quote can exist
docstr = docstr.replace("''", "\\'\\'")
raw_str = "r"
if len(docstr) > 70 or "\n" in docstr:
docstr = "\n" + docstr + "\n"
docstr = f'{raw_str}"""{docstr}"""\n'
self.write_par(docstr)
def put_nb_overload(self, fn: NbFunction, sig: NbFunctionSignature, name: Optional[str] = None) -> None:
"""
The ``put_nb_func()`` repeatedly calls this method to render the
individual method overloads.
"""
sig_str, docstr, start = cast(str, sig[0]), cast(str, sig[1]), 0
# Label anonymous functions
if sig_str.startswith("def (") and name is not None:
sig_str = "def " + name + sig_str[4:]
# Simplify type names present in the signature
paren = sig_str.find("(")
sig_str = sig_str[:paren] + self.simplify_types(sig_str[paren:])
# Substitute in string versions of the default arguments
default_args = sig[2]
if default_args:
for index, arg in enumerate(default_args):
pos = -1
pattern = None
arg_str = None
# First, handle the case where the user overrode the default value signature
if isinstance(arg, str):
pattern = f"\\={index}"
pos = sig_str.find(pattern, start)
if pos >= 0:
arg_str = arg
# General case
if pos < 0:
pattern = f"\\{index}"
pos = sig_str.find(pattern, start)
if pos < 0:
raise Exception(
"Could not locate default argument in function signature"
)
if not arg_str:
# Call expr_str to convert the default value to a string.
# Abbreviate with '...' if it is too long.
expr = self.expr_str(arg, abbrev=True)
arg_str = expr if expr else "..."
assert (
"\n" not in arg_str
), "Default argument string may not contain newlines."
assert pattern is not None
sig_str = sig_str[:pos] + arg_str + sig_str[pos + len(pattern) :]
start = pos + len(arg_str)
if type(fn).__name__ == "nb_func" and self.depth > 0:
self.write_ln("@staticmethod")
if not docstr or not self.include_docstrings:
for s in sig_str.split("\n"):
self.write_ln(s)
self.output = self.output[:-1] + ": ...\n"
else:
docstr = textwrap.dedent(docstr)
for s in sig_str.split("\n"):
self.write_ln(s)
self.output = self.output[:-1] + ":\n"
self.depth += 1
self.put_docstr(docstr)
self.depth -= 1
self.write("\n")
def put_nb_func(self, fn: NbFunction, name: Optional[str] = None) -> None:
"""Append a nanobind function binding to the stub"""
sigs = fn.__nb_signature__
count = len(sigs)
assert count > 0
if count == 1:
# No overloads write directly
self.put_nb_overload(fn, sigs[0], name)
else:
# Render an @overload-decorated chain
overload = self.import_object("typing", "overload")
for s in sigs:
self.write_ln(f"@{overload}")
self.put_nb_overload(fn, s, name)
def put_function(self, fn: Callable[..., Any], name: Optional[str] = None, parent: Optional[object] = None):
"""Append a function of an arbitrary type to the stub"""
# Don't generate a constructor for nanobind classes that aren't constructible
if name == "__init__" and type(parent).__name__.startswith("nb_type"):
return
fn_module = getattr(fn, "__module__", None)
fn_name = getattr(fn, "__name__", None)
# Check if this function is an alias from *another* module
if name and fn_module and fn_module != self.module.__name__:
self.put_value(fn, name)
return
# Check if this function is an alias from the *same* module
if name and fn_name and name != fn_name:
self.write_ln(f"{name} = {fn_name}\n")
return
if isinstance(fn, staticmethod):
self.write_ln("@staticmethod")
fn = fn.__func__
elif isinstance(fn, classmethod):
self.write_ln("@staticmethod")
fn = fn.__func__
# Special handling for nanobind functions with overloads
if type(fn).__module__ == "nanobind":
fn = cast(NbFunction, fn)
self.put_nb_func(fn, name)
return
if name is None:
name = fn.__name__
assert name
overloads: Sequence[Callable[..., Any]] = []
if hasattr(fn, "__module__"):
if typing_extensions:
overloads = typing_extensions.get_overloads(fn)
else:
overloads = typing.get_overloads(fn)
if not overloads:
overloads = [fn]
for i, fno in enumerate(overloads):
if len(overloads) > 1:
overload = self.import_object("typing", "overload")
self.write_ln(f"@{overload}")
sig_str = f"{name}{self.signature_str(signature(fno))}"
# Potentially copy docstring from the implementation function
docstr = fno.__doc__
if i == 0 and not docstr and fn.__doc__:
docstr = fn.__doc__
if not docstr or not self.include_docstrings:
self.write_ln("def " + sig_str + ": ...")
else:
self.write_ln("def " + sig_str + ":")
self.depth += 1
self.put_docstr(docstr)
self.depth -= 1
self.write("\n")
def put_property(self, prop: property, name: Optional[str]):
"""Append a Python 'property' object"""
fget, fset = prop.fget, prop.fset
self.write_ln("@property")
self.put(fget, name=name)
if fset:
self.write_ln(f"@{name}.setter")
docstrings_backup = self.include_docstrings
if isinstance(fget, NbGetterSetter) and isinstance(fset, NbGetterSetter):
doc1 = fget.__nb_signature__[0][1]
doc2 = fset.__nb_signature__[0][1]
if doc1 and doc2 and doc1 == doc2:
self.include_docstrings = False
self.put(prop.fset, name=name)
self.include_docstrings = docstrings_backup
def put_nb_static_property(self, name: Optional[str], prop: NbStaticProperty):
"""Append a 'nb_static_property' object"""
getter_sig = prop.fget.__nb_signature__[0][0]
getter_sig = getter_sig[getter_sig.find("/) -> ") + 6 :]
self.write_ln(f"{name}: {getter_sig} = ...")
if prop.__doc__ and self.include_docstrings:
self.put_docstr(prop.__doc__)
self.write("\n")
def put_type(self, tp: NbType, name: Optional[str]):
"""Append a 'nb_type' type object"""
tp_name, tp_mod_name = tp.__name__, tp.__module__
mod_name = self.module.__name__
if name and (name != tp_name or mod_name != tp_mod_name):
same_module = tp_mod_name == mod_name
same_toplevel_module = tp_mod_name.split(".")[0] == mod_name.split(".")[0]
if same_module:
# This is an alias of a type in the same module or same top-level module
alias_tp = self.import_object("typing", "TypeAlias")
self.write_ln(f"{name}: {alias_tp} = {tp.__qualname__}\n")
elif self.include_external_imports or (same_toplevel_module and self.include_internal_imports):
# Import from a different module
self.put_value(tp, name)
else:
is_enum = self.is_enum(tp)
docstr = tp.__doc__
tp_dict = dict(tp.__dict__)
tp_bases: Union[List[str], Tuple[Any, ...], None] = None
if is_enum:
# Rewrite enumerations so that they derive from a helper type
# to avoid bloat from a large number of repeated function
# declarations
docstr = docstr.__doc__
is_arith = "__add__" in tp_dict
self.abstract_enum = True
self.abstract_enum_arith |= is_arith
tp_bases = ["_Enum" + ("Arith" if is_arith else "")]
del tp_dict["name"]
del tp_dict["value"]
for op in ENUM_OPS:
name, rname = f"__{op}__", f"__r{op}__"
if name in tp_dict:
del tp_dict[name]
if rname in tp_dict:
del tp_dict[rname]
if "__nb_signature__" in tp.__dict__:
# Types with a custom signature override
for s in tp.__nb_signature__.split("\n"):
self.write_ln(self.simplify_types(s))
self.output = self.output[:-1] + ":\n"
else:
self.write_ln(f"class {tp_name}:")
if tp_bases is None:
tp_bases = getattr(tp, "__orig_bases__", None)
if tp_bases is None:
tp_bases = tp.__bases__
tp_bases = [self.type_str(base) for base in tp_bases]
if tp_bases != ["object"]:
self.output = self.output[:-2] + "("
for i, base in enumerate(tp_bases):
if i:
self.write(", ")
self.write(base)
self.write("):\n")
self.depth += 1
output_len = len(self.output)
if docstr and self.include_docstrings:
self.put_docstr(docstr)
if len(tp_dict):
self.write("\n")
for k, v in tp_dict.items():
self.put(v, k, tp)
if output_len == len(self.output):
self.write_ln("pass\n")
self.depth -= 1
def is_enum(self, tp: object) -> bool:
"""Check if the given type is an enumeration"""
return hasattr(tp, "@entries")
def is_function(self, tp: type) -> bool:
"""
Test if this is one of the many types of built-in functions supported
by Python, or if it is a nanobind ``nb_func``.
"""
return (
issubclass(tp, types.FunctionType)
or issubclass(tp, types.BuiltinFunctionType)
or issubclass(tp, types.BuiltinMethodType)
or issubclass(tp, types.WrapperDescriptorType)
or issubclass(tp, staticmethod)
or issubclass(tp, classmethod)
or (tp.__module__ == "nanobind" and tp.__name__ == "nb_func")
)
def put_value(self, value: object, name: str, parent: Optional[object] = None, abbrev: bool = True) -> None:
"""
Render a ``name: type = value`` assignment at the module, class, or
enum scope.
The parameter ``abbrev`` indicates if it is acceptable to reduce very
long expressions to ``...``.
"""
tp = type(value)
# Ignore module imports of non-type values like 'from typing import Optional'
if (
not self.include_external_imports
and tp.__module__ == "typing"
and str(value) == f"typing.{name}"
):
return
if isinstance(parent, type) and issubclass(tp, parent) and self.is_enum(parent):
# This is an entry of an enumeration
self.write_ln(f"{name}: {self.type_str(tp)}")
if value.__doc__ and self.include_docstrings:
self.put_docstr(value.__doc__)
self.write("\n")
elif self.is_function(tp) or isinstance(value, type):
named_value = cast(NamedObject, value)
same_toplevel_module = named_value.__module__.split(".")[0] == self.module.__name__.split(".")[0]
if self.include_external_imports or (same_toplevel_module and self.include_internal_imports):
# This is a function or a type, import it from its actual source
self.import_object(named_value.__module__, named_value.__name__, name)
else:
value_str = self.expr_str(value, abbrev)
if value_str is None:
value_str = "..."
# Catch a few different typing.* constructs
if self.is_type_var(tp):
types = ""
elif typing.get_origin(value):
types = ": " + self.import_object("typing", "TypeAlias")
else:
types = f": {self.type_str(tp)}"
self.write_ln(f"{name}{types} = {value_str}\n")
def is_type_var(self, tp: type) -> bool:
return (issubclass(tp, typing.TypeVar)
or (sys.version_info >= (3, 11) and issubclass(tp, typing.TypeVarTuple))
or (typing_extensions is not None
and (
issubclass(tp, typing_extensions.TypeVar)
or issubclass(tp, typing_extensions.TypeVarTuple))))
def simplify_types(self, s: str) -> str:
"""
Process types that occur within a signature string ``s`` and simplify
them. This function applies the following rules:
- "local_module.X" -> "X"
- "other_module.X" -> "other_module.XX"
(with "import other_module" added at top)
- "builtins.X" -> "X"
- "NoneType" -> "None"
- "ndarray[...]" -> "Annotated[ArrayLike, dict(...)]"
- "collections.abc.X" -> "X"
(with "from collections.abc import X" added at top)
- "typing.X" -> "X"
(with "from typing import X" added at top, potentially
changed to 'collections.abc' on newer Python versions)
"""
# Process nd-array type annotations so that MyPy accepts them
def process_ndarray(m: Match[str]) -> str:
s = m.group(2)
ndarray = self.import_object("numpy.typing", "ArrayLike")
assert ndarray
s = re.sub(r"dtype=([\w]*)\b", r"dtype='\g<1>'", s)
s = s.replace("*", "None")
if s:
annotated = self.import_object("typing", "Annotated")
return f"{annotated}[{ndarray}, dict({s})]"
else:
return ndarray
s = self.ndarray_re.sub(process_ndarray, s)
if sys.version_info >= (3, 9, 0):
s = self.abc_re.sub(r'collections.abc.\1', s)
# Process other type names and add suitable import statements
def process_general(m: Match[str]) -> str:
full_name, mod_name, cls_name = m.group(0), m.group(1)[:-1], m.group(2)
if mod_name == "builtins":
# Simplify builtins
return cls_name if cls_name != "NoneType" else "None"
if full_name.startswith(self.module.__name__):
# Strip away the module prefix for local classes
return full_name[len(self.module.__name__) + 1 :]
elif mod_name == "typing" or mod_name == "collections.abc":
# Import frequently-occurring typing classes and ABCs directly
return self.import_object(mod_name, cls_name)
else:
# Import the module and reference the contained class by name
self.import_object(mod_name, None)
return full_name
s = self.id_seq.sub(process_general, s)
return s
def apply_pattern(self, query: str, value: object) -> bool:
"""
Check if ``value`` matches an entry of a pattern file. Applies the
pattern and returns ``True`` in that case, otherwise returns ``False``.
"""
match: Optional[Match[str]] = None
pattern: Optional[ReplacePattern] = None
for pattern in self.patterns:
match = pattern.query.search(query)
if match:
break
if not match or not pattern:
return False
for line in pattern.lines:
ls = line.strip()
if ls == "\\doc":
# Docstring reference
tp = type(value)
doc: Optional[str] = None
if tp.__module__ == "nanobind" and tp.__name__ in (
"nb_func",
"nb_method",
):
value = cast(NbFunction, value)
for tp_i in value.__nb_signature__:
doc = tp_i[1]
if doc:
break
else:
doc = getattr(value, "__doc__", None)
self.depth += 1
if doc and self.include_docstrings:
self.put_docstr(doc)
else:
self.write_ln("pass")
self.depth -= 1
continue
elif ls.startswith("\\from "):
items = ls[5:].split(" import ")
if len(items) != 2:
raise RuntimeError(f"Could not parse import declaration {ls}")
for item in items[1].strip("()").split(","):
item_list = item.split(" as ")
import_module, import_name = (
items[0].strip(),
item_list[0].strip(),
)
import_as = item_list[1].strip() if len(item_list) > 1 else None
self.import_object(import_module, import_name, import_as)
continue
groups = match.groups()
for i in reversed(range(len(groups))):
line = line.replace(f"\\{i+1}", groups[i])
for k, v in match.groupdict().items():
line = line.replace(f"\\{k}", v)
self.write_ln(line)
# Success, pattern was applied
return True
def put(self, value: object, name: Optional[str] = None, parent: Optional[object] = None) -> None:
old_prefix = self.prefix
if value in self.stack:
# Avoid infinite recursion due to cycles
return
try:
self.stack.append(value)
self.prefix = self.prefix + (("." + name) if name else "")
# Check if an entry in a provided pattern file matches
if self.apply_pattern(self.prefix, value):
return
# Exclude various standard elements found in modules, classes, etc.
if name in SKIP_LIST:
return
is_type_alias = typing.get_origin(value) or (
isinstance(value, type)
and (value.__name__ != name or value.__module__ != self.module.__name__)
)
# Ignore private members unless the user requests their inclusion
if (
not self.include_private
and name
and not is_type_alias
and len(name) > 2
and (
(name[0] == "_" and name[1] != "_")
or (name[-1] == "_" and name[-2] != "_")
)
):
return
tp = type(value)
tp_mod, tp_name = tp.__module__, tp.__name__
if ismodule(value):
if len(self.stack) != 1:
value_name_s = value.__name__.split(".")
module_name_s = self.module.__name__.split(".")
is_external = value_name_s[0] != module_name_s[0]
if not self.include_external_imports and is_external:
return
# Do not include submodules in the same stub, but include a directive to import them
self.import_object(value.__name__, name=None, as_name=name)
# If the user requested this, generate a separate stub recursively
if self.recursive and value_name_s[:-1] == module_name_s and self.output_file:
module_file = getattr(value, '__file__', None)
if not module_file or module_file.endswith('__init__.py'):
dir_name = self.output_file.parents[0] / value_name_s[-1]
dir_name.mkdir(parents=False, exist_ok=True)
output_file = dir_name / '__init__.pyi'
else:
output_file = self.output_file.parents[0] / (value_name_s[-1] + '.py')
sg = StubGen(
module=value,
recursive=self.recursive,
include_docstrings=self.include_docstrings,
include_private=self.include_private,
include_external_imports=self.include_external_imports,
include_internal_imports=self.include_internal_imports,
max_expr_length=self.max_expr_length,
patterns=self.patterns,
output_file=output_file,
quiet=self.quiet
)
sg.put(value)
if not self.quiet:
print(f' - writing stub "{output_file}" ..')
with open(output_file, "w", encoding='utf-8') as f:
f.write(sg.get())
return
else:
self.apply_pattern(self.prefix + ".__prefix__", None)
for name, child in getmembers(value):
self.put(child, name=name, parent=value)
self.apply_pattern(self.prefix + ".__suffix__", None)
elif self.is_function(tp):
value = cast(NbFunction, value)
self.put_function(value, name, parent)
elif issubclass(tp, type):
value = cast(NbType, value)
self.put_type(value, name)
elif tp_mod == "nanobind":
if tp_name == "nb_method":
value = cast(NbFunction, value)
self.put_nb_func(value, name)
elif tp_name == "nb_static_property":
value = cast(NbStaticProperty, value)
self.put_nb_static_property(name, value)
elif tp_mod == "builtins":
if tp is property:
value = cast(property, value)
self.put_property(value, name)
else:
assert name is not None
abbrev = name != "__all__"
self.put_value(value, name, parent, abbrev=abbrev)
else:
assert name is not None
self.put_value(value, name, parent)
finally:
self.stack.pop()
self.prefix = old_prefix
def import_object(
self, module: str, name: Optional[str], as_name: Optional[str] = None
) -> str:
"""
Import a type (e.g. typing.Optional) used within the stub, ensuring
that this does not cause conflicts. Specify ``as_name`` to ensure that
the import is bound to a specified name.
When ``name`` is None, the entire module is imported.
"""
if module == "builtins" and name and (not as_name or name == as_name):
return name
# Rewrite module name if this is relative import from a submodule
if module.startswith(self.module.__name__):
module_short = module[len(self.module.__name__) :]
if not name and as_name and module_short[0] == ".":
name = as_name = module_short[1:]
module_short = "."
else:
module_short = module
# Query a cache of previously imported objects
imports_module: Optional[ImportDict] = self.imports.get(module_short, None)
if not imports_module:
imports_module = {}
self.imports[module_short] = imports_module
key = (name, as_name)
final_name = imports_module.get(key, None)
if final_name:
return final_name
# Cache miss, import the object
final_name = as_name if as_name else name
# If no as_name constraint was set, potentially adjust the name to
# avoid conflicts with an existing object of the same name
if name and not as_name:
test_name = name
while True:
# Accept the name if there are no conflicts
if not hasattr(self.module, test_name):
break
value = getattr(self.module, test_name)
try:
if module == ".":
mod_o = self.module
else:
mod_o = importlib.import_module(module)
# If there is a conflict, accept it if it refers to the same object
if getattr(mod_o, name) is value:
break
except ImportError:
pass
# Prefix with an underscore
test_name = "_" + test_name
final_name = test_name
imports_module[key] = final_name
return final_name if final_name else ""
def expr_str(self, e: Any, abbrev: bool = True) -> Optional[str]:
"""
Attempt to convert a value into valid Python syntax that regenerates
that value. When ``abbrev`` is True, the implementation gives up and
returns ``None`` when the expression is considered to be too
complicated.
"""
tp = type(e)
for t in [bool, int, type(None), type(builtins.Ellipsis)]:
if issubclass(tp, t):
return repr(e)
if issubclass(tp, float):
s = repr(e)
if "inf" in s or "nan" in s:
return f"float('{s}')"
else:
return s
elif self.is_enum(tp):
return self.type_str(type(e)) + "." + e.__name__
elif issubclass(tp, type) or typing.get_origin(e):
return self.type_str(e)
elif issubclass(tp, typing.ForwardRef):
return f'"{e.__forward_arg__}"'
elif (sys.version_info >= (3, 11) and issubclass(tp, typing.TypeVarTuple)) \
or (typing_extensions is not None and issubclass(tp, typing_extensions.TypeVarTuple)):
tv = self.import_object(tp.__module__, "TypeVarTuple")
return f'{tv}("{e.__name__}")'
elif issubclass(tp, typing.TypeVar):
tv = self.import_object("typing", "TypeVar")
s = f'{tv}("{e.__name__}"'
for v in getattr(e, "__constraints__", ()):
v = self.expr_str(v)
assert v
s += ", " + v
for k in ["contravariant", "covariant", "bound", "infer_variance"]:
v = getattr(e, f"__{k}__", None)
if v:
v = self.expr_str(v)
if v is None:
return None
s += f", {k}=" + v
s += ")"
return s
elif issubclass(tp, str):
s = repr(e)
if len(s) < self.max_expr_length or not abbrev:
return s
elif issubclass(tp, list) or issubclass(tp, tuple):
e = [self.expr_str(v, abbrev) for v in e]
if None in e:
return None
if issubclass(tp, list):
s = "[" + ", ".join(e) + "]"
else:
s = "(" + ", ".join(e) + ")"
if len(s) < self.max_expr_length or not abbrev:
return s
elif issubclass(tp, dict):
e = [