|
| 1 | +from __future__ import absolute_import, annotations, division, print_function |
| 2 | + |
| 3 | +import mypy.stubgen |
| 4 | +import mypy.stubgenc |
| 5 | +from mypy.stubgenc import SignatureGenerator, DocstringSignatureGenerator |
| 6 | + |
| 7 | +from stubgenlib.siggen import ( |
| 8 | + AdvancedSignatureGenerator, |
| 9 | + AdvancedSigMatcher, |
| 10 | +) |
| 11 | +from stubgenlib.utils import add_positional_only_args |
| 12 | + |
| 13 | + |
| 14 | +PY_TO_STDVECTOR_RESULT = "float | list[float] | tuple[float, ...]" |
| 15 | + |
| 16 | + |
| 17 | +class OIIOSignatureGenerator(AdvancedSignatureGenerator): |
| 18 | + sig_matcher = AdvancedSigMatcher( |
| 19 | + signature_overrides={ |
| 20 | + # signatures for these special methods include many inaccurate overloads |
| 21 | + "*.__ne__": "(self, other: object) -> bool", |
| 22 | + "*.__eq__": "(self, other: object) -> bool", |
| 23 | + }, |
| 24 | + arg_type_overrides={ |
| 25 | + # FIXME: Buffer may in fact be more accurate here |
| 26 | + ("*", "*", "Buffer"): "numpy.ndarray", |
| 27 | + # these use py_to_stdvector util |
| 28 | + ("*.ImageBufAlgo.*", "min", "object"): PY_TO_STDVECTOR_RESULT, |
| 29 | + ("*.ImageBufAlgo.*", "max", "object"): PY_TO_STDVECTOR_RESULT, |
| 30 | + ("*.ImageBufAlgo.*", "black", "object"): PY_TO_STDVECTOR_RESULT, |
| 31 | + ("*.ImageBufAlgo.*", "white", "object"): PY_TO_STDVECTOR_RESULT, |
| 32 | + ("*.ImageBufAlgo.*", "sthresh", "object"): PY_TO_STDVECTOR_RESULT, |
| 33 | + ("*.ImageBufAlgo.*", "scontrast", "object"): PY_TO_STDVECTOR_RESULT, |
| 34 | + ("*.ImageBufAlgo.*", "white_balance", "object"): PY_TO_STDVECTOR_RESULT, |
| 35 | + ("*.ImageBufAlgo.*", "values", "object"): PY_TO_STDVECTOR_RESULT, |
| 36 | + ("*.ImageBufAlgo.*", "top", "object"): PY_TO_STDVECTOR_RESULT, |
| 37 | + ("*.ImageBufAlgo.*", "bottom", "object"): PY_TO_STDVECTOR_RESULT, |
| 38 | + ("*.ImageBufAlgo.*", "topleft", "object"): PY_TO_STDVECTOR_RESULT, |
| 39 | + ("*.ImageBufAlgo.*", "topright", "object"): PY_TO_STDVECTOR_RESULT, |
| 40 | + ("*.ImageBufAlgo.*", "bottomleft", "object"): PY_TO_STDVECTOR_RESULT, |
| 41 | + ("*.ImageBufAlgo.*", "bottomright", "object"): PY_TO_STDVECTOR_RESULT, |
| 42 | + ("*.ImageBufAlgo.*", "color", "object"): PY_TO_STDVECTOR_RESULT, |
| 43 | + # BASETYPE & str are implicitly converible to TypeDesc |
| 44 | + ("*", "*", "*.TypeDesc"): "Union[TypeDesc, BASETYPE, str]", |
| 45 | + # list is not strictly required |
| 46 | + ("*.ImageOutput.open", "specs", "list[ImageSpec]"): "Iterable[ImageSpec]", |
| 47 | + }, |
| 48 | + result_type_overrides={ |
| 49 | + # FIXME: is there a way to use std::optional for these? |
| 50 | + ("*.ImageOutput.create", "object"): "ImageOutput | None", |
| 51 | + ("*.ImageOutput.open", "object"): "ImageOutput | None", |
| 52 | + ("*.ImageInput.create", "object"): "ImageInput | None", |
| 53 | + ("*.ImageInput.open", "object"): "ImageInput | None", |
| 54 | + |
| 55 | + # ("*.TextureSystem.imagespec", "object"): "ImageSpec | None", |
| 56 | + # ("*.ImageInput.read_native_deep_*", "object"): "DeepData | None", |
| 57 | + |
| 58 | + # pybind11 has special support, so it may be possible to get it to emit these types |
| 59 | + # by using py::numpy in our wrapper code. |
| 60 | + ("*.ImageInput.read_*", "object"): "numpy.ndarray | None", |
| 61 | + ("*", "Buffer"): "numpy.ndarray", |
| 62 | + ("*.get_pixels", "object"): "numpy.ndarray | None", |
| 63 | + |
| 64 | + # For results, object is too restrictive (produces spurious errors during type analysis) |
| 65 | + ("*.getattribute", "object"): "Any", |
| 66 | + ("*.ImageSpec.get", "object"): "Any", |
| 67 | + |
| 68 | + # pybind11 does not have a way to emit tuple[T, ...], e.g. from std:vector<T>. |
| 69 | + ("*.ImageBufAlgo.histogram", "tuple"): "tuple[int, ...]", |
| 70 | + ("*.ImageBufAlgo.isConstantColor", "*"): "tuple[float, ...] | None", |
| 71 | + ("*.ImageBufAlgo.color_range_check", "*"): "tuple[int, ...] | None", |
| 72 | + ("*.TextureSystem.texture", "tuple"): "tuple[float, ...]", |
| 73 | + ("*.TextureSystem.texture3d", "tuple"): "tuple[float, ...]", |
| 74 | + ("*.TextureSystem.environment", "tuple"): "tuple[float, ...]", |
| 75 | + ("*.ImageBuf.getpixel", "tuple"): "tuple[float, ...]", |
| 76 | + ("*.ImageBuf.interpixel*", "tuple"): "tuple[float, ...]", |
| 77 | + ("*.ImageSpec.get_channelformats", "tuple"): "tuple[TypeDesc, ...]", |
| 78 | + }, |
| 79 | + |
| 80 | + property_type_overrides={ |
| 81 | + # FIXME: this isn't working |
| 82 | + ("*.ParamValue.value", "object"): "Any", |
| 83 | + }, |
| 84 | + ) |
| 85 | + |
| 86 | + def process_sig( |
| 87 | + self, ctx: mypy.stubgen.FunctionContext, sig: mypy.stubgen.FunctionSig |
| 88 | + ) -> mypy.stubgen.FunctionSig: |
| 89 | + # Analyze the signature and add a '/' argument if necessary to mark |
| 90 | + # arguments which cannot be access by name. |
| 91 | + return add_positional_only_args(ctx, super().process_sig(ctx, sig)) |
| 92 | + |
| 93 | + |
| 94 | +class InspectionStubGenerator(mypy.stubgenc.InspectionStubGenerator): |
| 95 | + def get_sig_generators(self) -> list[SignatureGenerator]: |
| 96 | + return [ |
| 97 | + OIIOSignatureGenerator( |
| 98 | + fallback_sig_gen=DocstringSignatureGenerator(), |
| 99 | + ) |
| 100 | + ] |
| 101 | + |
| 102 | + def set_defined_names(self, defined_names: set[str]) -> None: |
| 103 | + super().set_defined_names(defined_names) |
| 104 | + for typ in ["Any", "Iterable"]: |
| 105 | + self.add_name(f"typing.{typ}", require=False) |
| 106 | + |
| 107 | + |
| 108 | +mypy.stubgen.InspectionStubGenerator = InspectionStubGenerator # type: ignore[attr-defined,misc] |
| 109 | +mypy.stubgenc.InspectionStubGenerator = InspectionStubGenerator # type: ignore[misc] |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + mypy.stubgen.main() |
0 commit comments