Skip to content

Commit 6b67aac

Browse files
[mlir:python] Improve mlir_(attribute|type|value)_subclass for nanobinds stubgen (#127584)
This PR makes several improvements to the stubs that are created by `mlir_(attribute|type|value)_subclass`. First, the PR sets the `__module__` attribute of the classes generated by the nanobind adaptors for attributes, types, and values (via `mlir_(attribute|type|value)_subclass`). By default, the `__module__` property is set to `importlib._bootstrap`, which isn't where we want the new class to live. The new logic sets the property to the name of the module provided as `scope` instead. This also makes nanobind's `stubgen` generate stubs for those classes properly, which ignores classes whose `__module__` does not correspond to the module it is generating stubs for. This resolves #127518. Second, the PR overwrites the function signatures generated by `stubgen` to a format that uses the desired type names (e.g., `mlir.ir.Attribute` instead of `MlirAttribute`). Finally, the PR piggy-backs some minor doc and style improvements to `PythonAdaptors.h`. --------- Signed-off-by: Ingo Müller <[email protected]>
1 parent 1c02c8f commit 6b67aac

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323

2424
#include "mlir-c/Diagnostics.h"
2525
#include "mlir-c/IR.h"
26+
// clang-format off
2627
#include "mlir/Bindings/Python/Nanobind.h"
2728
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
29+
// clang-format on
2830
#include "llvm/ADT/Twine.h"
2931

3032
// Raw CAPI type casters need to be declared before use, so always include them
@@ -349,6 +351,7 @@ class pure_subclass {
349351
thisClass = metaclass(derivedClassName, nanobind::make_tuple(superClass),
350352
attributes);
351353
scope.attr(derivedClassName) = thisClass;
354+
thisClass.attr("__module__") = scope.attr("__name__");
352355
}
353356

354357
template <typename Func, typename... Extra>
@@ -434,7 +437,7 @@ class mlir_attribute_subclass : public pure_subclass {
434437
const nanobind::object &superCls,
435438
GetTypeIDFunctionTy getTypeIDFunction = nullptr)
436439
: pure_subclass(scope, typeClassName, superCls) {
437-
// Casting constructor. Note that it hard, if not impossible, to properly
440+
// Casting constructor. Note that it is hard, if not impossible, to properly
438441
// call chain to parent `__init__` in nanobind due to its special handling
439442
// for init functions that don't have a fully constructed self-reference,
440443
// which makes it impossible to forward it to `__init__` of a superclass.
@@ -465,10 +468,13 @@ class mlir_attribute_subclass : public pure_subclass {
465468
thisClass.attr("__new__") = newCf;
466469

467470
// 'isinstance' method.
471+
static const char kIsinstanceSig[] =
472+
"def isinstance(other_attribute: " MAKE_MLIR_PYTHON_QUALNAME(
473+
"ir") ".Attribute) -> bool";
468474
def_staticmethod(
469475
"isinstance",
470476
[isaFunction](MlirAttribute other) { return isaFunction(other); },
471-
nanobind::arg("other_attribute"));
477+
nanobind::arg("other_attribute"), nanobind::sig(kIsinstanceSig));
472478
def("__repr__", [superCls, captureTypeName](nanobind::object self) {
473479
return nanobind::repr(superCls(self))
474480
.attr("replace")(superCls.attr("__name__"), captureTypeName);
@@ -512,7 +518,7 @@ class mlir_type_subclass : public pure_subclass {
512518
const nanobind::object &superCls,
513519
GetTypeIDFunctionTy getTypeIDFunction = nullptr)
514520
: pure_subclass(scope, typeClassName, superCls) {
515-
// Casting constructor. Note that it hard, if not impossible, to properly
521+
// Casting constructor. Note that it is hard, if not impossible, to properly
516522
// call chain to parent `__init__` in nanobind due to its special handling
517523
// for init functions that don't have a fully constructed self-reference,
518524
// which makes it impossible to forward it to `__init__` of a superclass.
@@ -542,13 +548,17 @@ class mlir_type_subclass : public pure_subclass {
542548
thisClass.attr("__new__") = newCf;
543549

544550
// 'isinstance' method.
551+
static const char kIsinstanceSig[] =
552+
"def isinstance(other_type: " MAKE_MLIR_PYTHON_QUALNAME(
553+
"ir") ".Type) -> bool";
545554
def_staticmethod(
546555
"isinstance",
547556
[isaFunction](MlirType other) { return isaFunction(other); },
548-
nanobind::arg("other_type"));
557+
nanobind::arg("other_type"), nanobind::sig(kIsinstanceSig));
549558
def("__repr__", [superCls, captureTypeName](nanobind::object self) {
550-
return nanobind::repr(superCls(self))
551-
.attr("replace")(superCls.attr("__name__"), captureTypeName);
559+
return nanobind::cast<std::string>(
560+
nanobind::repr(superCls(self))
561+
.attr("replace")(superCls.attr("__name__"), captureTypeName));
552562
});
553563
if (getTypeIDFunction) {
554564
// 'get_static_typeid' method.
@@ -590,7 +600,7 @@ class mlir_value_subclass : public pure_subclass {
590600
IsAFunctionTy isaFunction,
591601
const nanobind::object &superCls)
592602
: pure_subclass(scope, valueClassName, superCls) {
593-
// Casting constructor. Note that it hard, if not impossible, to properly
603+
// Casting constructor. Note that it is hard, if not impossible, to properly
594604
// call chain to parent `__init__` in nanobind due to its special handling
595605
// for init functions that don't have a fully constructed self-reference,
596606
// which makes it impossible to forward it to `__init__` of a superclass.
@@ -620,10 +630,13 @@ class mlir_value_subclass : public pure_subclass {
620630
thisClass.attr("__new__") = newCf;
621631

622632
// 'isinstance' method.
633+
static const char kIsinstanceSig[] =
634+
"def isinstance(other_value: " MAKE_MLIR_PYTHON_QUALNAME(
635+
"ir") ".Value) -> bool";
623636
def_staticmethod(
624637
"isinstance",
625638
[isaFunction](MlirValue other) { return isaFunction(other); },
626-
nanobind::arg("other_value"));
639+
nanobind::arg("other_value"), nanobind::sig(kIsinstanceSig));
627640
}
628641
};
629642

0 commit comments

Comments
 (0)