Skip to content

Commit b1d185b

Browse files
Fix signature of isinstance bindings.
This commit overwrites the signature of the `isinstance` member functions created by `mlir_(attribute|type|value)_subclass` by adding a `nanobind::signature` argument to the `def` call that creates the functions. This improves the typing information that `stubgen` creates for such classes. Signed-off-by: Ingo Müller <[email protected]>
1 parent e0d5ad7 commit b1d185b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

mlir/include/mlir/Bindings/Python/NanobindAdaptors.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,13 @@ class mlir_attribute_subclass : public pure_subclass {
466466
thisClass.attr("__new__") = newCf;
467467

468468
// 'isinstance' method.
469+
static const char kIsinstanceSig[] =
470+
"def isinstance(other_attribute: " MAKE_MLIR_PYTHON_QUALNAME(
471+
"ir") ".Attribute) -> bool";
469472
def_staticmethod(
470473
"isinstance",
471474
[isaFunction](MlirAttribute other) { return isaFunction(other); },
472-
nanobind::arg("other_attribute"));
475+
nanobind::arg("other_attribute"), nanobind::sig(kIsinstanceSig));
473476
def("__repr__", [superCls, captureTypeName](nanobind::object self) {
474477
return nanobind::repr(superCls(self))
475478
.attr("replace")(superCls.attr("__name__"), captureTypeName);
@@ -543,13 +546,17 @@ class mlir_type_subclass : public pure_subclass {
543546
thisClass.attr("__new__") = newCf;
544547

545548
// 'isinstance' method.
549+
static const char kIsinstanceSig[] =
550+
"def isinstance(other_type: " MAKE_MLIR_PYTHON_QUALNAME(
551+
"ir") ".Type) -> bool";
546552
def_staticmethod(
547553
"isinstance",
548554
[isaFunction](MlirType other) { return isaFunction(other); },
549-
nanobind::arg("other_type"));
555+
nanobind::arg("other_type"), nanobind::sig(kIsinstanceSig));
550556
def("__repr__", [superCls, captureTypeName](nanobind::object self) {
551-
return nanobind::repr(superCls(self))
552-
.attr("replace")(superCls.attr("__name__"), captureTypeName);
557+
return nanobind::cast<std::string>(
558+
nanobind::repr(superCls(self))
559+
.attr("replace")(superCls.attr("__name__"), captureTypeName));
553560
});
554561
if (getTypeIDFunction) {
555562
// 'get_static_typeid' method.
@@ -621,10 +628,13 @@ class mlir_value_subclass : public pure_subclass {
621628
thisClass.attr("__new__") = newCf;
622629

623630
// 'isinstance' method.
631+
static const char kIsinstanceSig[] =
632+
"def isinstance(other_value: " MAKE_MLIR_PYTHON_QUALNAME(
633+
"ir") ".Value) -> bool";
624634
def_staticmethod(
625635
"isinstance",
626636
[isaFunction](MlirValue other) { return isaFunction(other); },
627-
nanobind::arg("other_value"));
637+
nanobind::arg("other_value"), nanobind::sig(kIsinstanceSig));
628638
}
629639
};
630640

0 commit comments

Comments
 (0)