-
Notifications
You must be signed in to change notification settings - Fork 12
/
stubgen_katana.py
401 lines (361 loc) · 16 KB
/
stubgen_katana.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
from __future__ import absolute_import, annotations, division, print_function
import os
import pathlib
import re
from typing import Any
import mypy.stubgen
import mypy.stubgenc
from mypy.stubgenc import FunctionContext, FunctionSig, SignatureGenerator
import Callbacks.Callbacks # type: ignore[import]
from Callbacks.Callbacks import _TypeEnum # type: ignore[import]
from stubgenlib import (
get_mypy_ignore_directive,
AdvancedSigMatcher,
AdvancedSignatureGenerator,
FixableCDocstringSigGen,
FixableDocstringSigGen,
CFunctionStub,
Notifier,
DocstringTypeFixer,
Optionality,
)
notifier = Notifier()
class KatanaDocstringTypeFixer(DocstringTypeFixer):
SPECIAL_REPLACEMENTS = [
("FnGeolib", "PyFnGeolib"),
("FnAttribute", "PyFnAttribute"),
("FnGeolibServices", "PyFnGeolibServices"),
("FnGeolibProducers", "PyFnGeolibProducers"),
(
r"PyFnGeolib.GeolibRuntime\.Transaction",
"PyFnGeolib.GeolibRuntimeTransaction",
),
(r"PyFnGeolib.GeolibRuntime\.Op", "PyFnGeolib.GeolibRuntimeOp"),
(r"NodegraphAPI\.LiveGroupMixin", "NodegraphAPI.LiveGroup.LiveGroupMixin"),
]
def get_replacements(self, is_return: bool) -> list[tuple[str, str]]:
return super().get_replacements(is_return) + self.SPECIAL_REPLACEMENTS
def get_full_name(self, type_name: str) -> str:
# FIXME: would be nice to have something that can do a search through known objects
absolute_names = (
(
"TerminalOpDelegate",
"Nodes3DAPI.TerminalOpDelegates.TerminalOpDelegate.TerminalOpDelegate",
),
("Nodes?", "NodegraphAPI.Node"),
("GroupNode", "NodegraphAPI.GroupNode"),
("Port", "NodegraphAPI.Port"),
("GraphState", "NodegraphAPI.GraphState"),
("Op", "PyFnGeolib.GeolibRuntimeOp"),
("WorkingSet", "PyUtilModule.WorkingSet.WorkingSet"),
("PortOpClient", "Nodes3DAPI.PortOpClient.PortOpClient"),
("GroupAttribute", "PyFnAttribute.GroupAttribute"),
("Package", "PackageSuperToolAPI.Packages.Package"),
# stuubgen will add imported modules that it discovers to generated
# output, but types in docstrings may refer to modules that are not
# imported into the module namespace. e.g. QtCore. Forcing to the
# full path will ensure that an import is added. An alternative may
# be to register `import PyQt5.QtCore as QtCore' in every module:
# it's likely that the imports will only be added if the imported
# object is actually used.
("(Qt.*)", r"PyQt5.\1"),
)
for short_name, full_name in absolute_names:
type_name = re.sub(
r"(?<![A-Za-z0-9._]){}\b".format(short_name), full_name, type_name
)
return type_name
class KatanaDocstringSignatureGenerator(
KatanaDocstringTypeFixer, FixableDocstringSigGen
):
# FIXME: implement?
def get_property_type(
self, default_type: str | None, ctx: FunctionContext
) -> str | None:
return default_type
class KatanaCSignatureGenerator(KatanaDocstringTypeFixer, FixableCDocstringSigGen):
pass
class KatanaSignatureGenerator(AdvancedSignatureGenerator):
sig_matcher = AdvancedSigMatcher(
signature_overrides={
# these docstring sigs are malformed
"NodegraphAPI_cmodule.GraphState.edit": "(self) -> GraphStateBuilder",
"NodegraphAPI_cmodule.GraphState.getDynamicEntry": "(self, path: str)",
"NodegraphAPI_cmodule.GraphState.getOpSystemArgs": "(self) -> GroupAttribute",
"NodegraphAPI_cmodule.GraphState.getStaticEntry": "(self, path: str)",
"NodegraphAPI_cmodule.Node.getInputPort": "(self, name: str) -> Port | None",
"NodegraphAPI_cmodule.Node.getOutputPort": "(self, name: str) -> Port | None",
"NodegraphAPI_cmodule.Node.getParameterValue": "(self, name: str, time: float)",
},
# FIXME: enabling these creates an explosion of errors to resolve.
# optional_result = [
# "NodegraphAPI_cmodule.Node.getInputPortByIndex",
# "NodegraphAPI_cmodule.Node.getOutputPortByIndex",
# "NodegraphAPI_cmodule.Node.getParameter",
# "NodegraphAPI_cmodule.GroupNode.getChild",
# "NodegraphAPI_cmodule.Parameter.getChild",
# "NodegraphAPI_cmodule.Parameter.getChildByIndex",
# "NodegraphAPI_cmodule.Port.getPort",
# "PyFnAttribute.GroupAttribute.getChildByIndex",
# "PyFnAttribute.GroupAttribute.getChildByName",
# "PyFnGeolibProducers.GeometryProducer.getAttribute",
# "PyFnGeolibProducers.GeometryProducer.getChildByName",
# "PyFnGeolibProducers.GeometryProducer.getDelimitedGlobalAttribute",
# "PyFnGeolibProducers.GeometryProducer.getDelimitedLocalAttribute",
# "PyFnGeolibProducers.GeometryProducer.getFirstChild",
# "PyFnGeolibProducers.GeometryProducer.getFnAttribute",
# "PyFnGeolibProducers.GeometryProducer.getGlobalAttribute",
# "PyFnGeolibProducers.GeometryProducer.getNextSibling",
# "PyFnGeolibProducers.GeometryProducer.getProducerByPath",
# ],
arg_type_overrides={
# FIXME: I'm using typing.Optional here because " | None" is getting stripped
(
"*",
"graphState",
"Incomplete | None",
): "typing.Optional[NodegraphAPI.GraphState]",
("*", "graphState", None): "NodegraphAPI.GraphState",
("*", "port", "Incomplete | None"): "typing.Optional[NodegraphAPI.Port]",
("*", "port", None): "NodegraphAPI.Port",
("*", "node", "Incomplete | None"): "typing.Optional[NodegraphAPI.Node]",
("*", "node", None): "NodegraphAPI.Node",
(
"*",
"producer",
"Incomplete | None",
): "typing.Optional[PyFnGeolibProducers.GeometryProducer]",
("*", "producer", None): "PyFnGeolibProducers.GeometryProducer",
("*", "*Callback", "Incomplete | None"): "typing.Optional[typing.Callable]",
("*", "*Callback", None): "typing.Callable",
(
"Nodes3DAPI.RenderNodeUtil.SyncOutputPorts",
"node",
"*",
): "NodegraphAPI.Node",
(
"Nodes3DAPI.RenderNodeUtil.GetRenderNodeInfo",
"node",
"*",
): "NodegraphAPI.Node",
},
result_type_overrides={
# None means the type is unset/unknown
("NodegraphAPI_cmodule.*.getNode", "Any"): "Node",
("*.getNode", None): "NodegraphAPI.Node",
(
"NodegraphAPI_cmodule.Parameter.getValue",
"*",
): "Any",
("NodegraphAPI_cmodule.GroupNode.getChild", "*"): "Node",
("NodegraphAPI_cmodule.GroupNode.getChildren", "*"): "list[Node]",
("NodegraphAPI_cmodule.Parameter.getChildren", "*"): "list[Parameter]",
("NodegraphAPI_cmodule.Port.getConnectedPorts", "*"): "list[Port]",
(
"PyFnAttribute.GroupAttribute.childList",
"*",
): "list[tuple[str, Attribute]]",
(
"PyFnGeolibProducers.GeometryProducer_childIter.__next__",
"*",
): "GeometryProducer",
(
"PyFnGeolibProducers.GeometryProducer.getFlattenedGlobalXform",
"*",
): "tuple[float, float, float, float, float, float, float, float, float, float, float, float, float, float, float, float]",
(
"drawing_cmodule.nodeWorld_getBounds",
"*",
): "tuple[float, float, float, float]",
(
"drawing_cmodule.nodeWorld_getBoundsOfListOfNodes",
"*",
): "tuple[float, float, float, float]",
},
optional_args={
(
"UI4.FormMaster.KatanaFactory.KatanaWidgetFactoryClass.buildWidget",
"policy",
"*",
): Optionality(accepts_none=True, has_default=False),
},
)
def get_property_type(
self, default_type: str | None, ctx: FunctionContext
) -> str | None:
return self.fallback_sig_gen.get_property_type(default_type, ctx)
class InspectionStubGenerator(mypy.stubgenc.InspectionStubGenerator):
DATA_ATTRS = {
"DataAttribute": "T",
"DoubleAttribute": "float",
"FloatAttribute": "float",
"IntAttribute": "int",
"StringAttribute": "str",
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.known_modules.extend(["PyQt5.QtCore", "PyQt5.QtWidgets", "PyQt5.QtGui"])
# preserve original sorting to redude the diff, for now
self.resort_members = True
def get_sig_generators(self) -> list[SignatureGenerator]:
if self.is_c_module:
return [
KatanaSignatureGenerator(fallback_sig_gen=KatanaCSignatureGenerator())
]
else:
return [
KatanaSignatureGenerator(
fallback_sig_gen=KatanaDocstringSignatureGenerator()
)
]
def should_reexport(self, name: str, full_module: str, name_is_alias: bool) -> bool:
# the usual logic breaks down because Katana has so many damned packages
return full_module in self.known_modules
def is_defined_in_module(self, obj: object) -> bool:
# _TypeEnum is a type, but it's created dynamically. This change ensures
# that we don't assume things of type _TypeEnum are external to their
# current module and should thus be imported.
return super().is_defined_in_module(obj) or type(obj).__name__ == "_TypeEnum"
def set_defined_names(self, defined_names: set[str]) -> None:
super().set_defined_names(defined_names)
for typ in ["Tuple", "Set"]:
self.add_name(f"typing.{typ}", require=True)
def strip_or_import(self, type_name: str) -> str:
if not self.is_c_module and re.match("^[A-Za-z0-9_.]+$", type_name):
parts = type_name.split(".")
# It's impossible to get access to members of certain modules without
# changing the import style, because the modules are replaced with
# objects of the same name
if (len(parts) >= 2 and parts[-2] == parts[-1]) or (
len(parts) >= 3 and parts[-3] == parts[-2]
):
self.import_tracker.add_import_from(
".".join(parts[:-1]), [(parts[-1], None)]
)
self.import_tracker.require_name(parts[-1])
return parts[-1]
return super().strip_or_import(type_name)
def get_imports(self) -> str:
if self.module_name == "PyFnAttribute":
self.add_name("typing.TypeVar")
type_vars = 'T = TypeVar("T")\n'
else:
type_vars = ""
imports = super().get_imports()
return (
get_mypy_ignore_directive(
["misc", "override", "attr-defined", "no-redef", "assignment"]
)
+ imports
+ type_vars
)
def get_members(self, obj: object) -> list[tuple[str, Any]]:
# Note that there is a mix of fixes here for C and non-C modules, but
# I'm not separating them because it's easy to get mixed up
members = dict(super().get_members(obj))
if self.is_c_module:
def add(x):
members[x.__name__] = x
if isinstance(obj, type) and obj.__name__ in self.DATA_ATTRS:
sub_type = self.DATA_ATTRS[obj.__name__]
is_abstract = obj.__name__ == "DataAttribute"
# Add abstract methods that are shared by all sub-classes
add(
CFunctionStub(
"getValue",
f"getValue(self, defaultValue: {sub_type} = ..., throwOnError: bool = ...) -> {sub_type}",
is_abstract=is_abstract,
)
)
add(
CFunctionStub(
"getData",
f"getData(self) -> ConstVector[{sub_type}]",
is_abstract=is_abstract,
)
)
add(
CFunctionStub(
"getNearestSample",
f"getNearestSample(self, sampleTime: float) -> ConstVector[{sub_type}]",
is_abstract=is_abstract,
)
)
add(
CFunctionStub(
"getSamples",
f"getSamples(self) -> dict[float, ConstVector[{sub_type}]]",
is_abstract=is_abstract,
)
)
elif isinstance(obj, type) and obj.__name__ == "ConstVector":
add(CFunctionStub("__iter__", "__iter__(self) -> typing.Iterator[T]"))
add(
CFunctionStub(
"__getitem__",
"__getitem__(self, arg0: int) -> T\n"
"__getitem__(self, arg0: slice) -> ConstVector[T]",
)
)
return list(members.items())
else:
if isinstance(obj, type) and obj.__name__ == "CallbacksManager":
enums = {
x: _TypeEnum(x)
for x in dir(Callbacks.Callbacks.Type)
if not x.startswith("_")
}
enumType = type("_TypeEnumList", (), enums)
enumType.__module__ = "Callbacks.Callbacks"
members["Type"] = enumType
return list(members.items())
def get_base_types(self, obj: type) -> list[str]:
bases = super().get_base_types(obj)
if obj.__name__ in self.DATA_ATTRS or obj.__name__ == "ConstVector":
sub_type = self.DATA_ATTRS.get(obj.__name__, "T")
if obj.__name__ in ["DataAttribute", "ConstVector"]:
self.add_name("typing.Generic")
return bases + [f"Generic[{sub_type}]"]
else:
base = bases[0]
return [f"{base}[{sub_type}]"]
else:
return bases
mypy.stubgen.InspectionStubGenerator = InspectionStubGenerator # type: ignore[misc]
mypy.stubgenc.InspectionStubGenerator = InspectionStubGenerator # type: ignore[misc]
def main(outdir: str, katana_site_dir: str) -> None:
modules = [
"_FnKatanaCoreUI",
"drawing_cmodule",
"AssetAPI_cmodule",
"ConfigurationAPI_cmodule",
"NodegraphAPI_cmodule",
"Nodes2DAPI_cmodule",
"Nodes3DAPI_cmodule",
"PyFCurve",
"PyFnAttribute",
"PyFnGeolib",
"PyFnGeolibProducers",
"PyFnGeolibServices",
"PyResolutionTableFn",
"PyXmlIO",
"RenderingAPI_cmodule",
"PyOpenColorIO",
]
args = ["-v", "--inspect-mode", "--include-private", "-o", outdir]
for module in modules:
args.append(f"-p={module}")
for path in pathlib.Path(katana_site_dir).iterdir():
if path.is_dir() and path.name[0].isupper():
module = path.name
if module == "PyQt5":
continue
args.append(f"-p={module}")
mypy.stubgen.main(args)
# the cg-stubs repo provides stubs for PyOpenColorIO 2.x, but Katana uses
# OCIO 1.x until Katana 6. So we generate stubs for Katana's OCIO lib.
# However, we make it private and only refer to it via Katana.OCIO so that
# the Katana stubs for this lib are not used by other apps which may be using
# differnet OCIO versions.
out = pathlib.Path(outdir)
out.joinpath("PyOpenColorIO.pyi").rename(out.joinpath("_PyOpenColorIO.pyi"))