[Mlir-commits] [mlir] [MLIR] [Python] Added return type to `__repr__` in the adaptors (PR #182867)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Feb 23 07:16:13 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Sergei Lebedev (superbobry)

<details>
<summary>Changes</summary>

Without an explicit annotation the return type is inferred as `nanobind::object` which results in invalid type stubs, since `__repr__` must return `str`.

---
Full diff: https://github.com/llvm/llvm-project/pull/182867.diff


1 Files Affected:

- (modified) mlir/include/mlir/Bindings/Python/NanobindAdaptors.h (+11-9) 


``````````diff
diff --git a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
index 918030824c409..09d9492c4d3a2 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
@@ -569,10 +569,11 @@ class mlir_attribute_subclass : public pure_subclass {
         "isinstance",
         [isaFunction](MlirAttribute other) { return isaFunction(other); },
         nanobind::arg("other_attribute"), nanobind::sig(kIsinstanceSig));
-    def("__repr__", [superCls, captureTypeName](nanobind::object self) {
-      return nanobind::repr(superCls(self))
-          .attr("replace")(superCls.attr("__name__"), captureTypeName);
-    });
+    def("__repr__",
+        [superCls, captureTypeName](nanobind::object self) -> nanobind::str {
+          return nanobind::repr(superCls(self))
+              .attr("replace")(superCls.attr("__name__"), captureTypeName);
+        });
     if (getTypeIDFunction) {
       def_staticmethod(
           "get_static_typeid",
@@ -652,11 +653,12 @@ class mlir_type_subclass : public pure_subclass {
         "isinstance",
         [isaFunction](MlirType other) { return isaFunction(other); },
         nanobind::arg("other_type"), nanobind::sig(kIsinstanceSig));
-    def("__repr__", [superCls, captureTypeName](nanobind::object self) {
-      return nanobind::cast<std::string>(
-          nanobind::repr(superCls(self))
-              .attr("replace")(superCls.attr("__name__"), captureTypeName));
-    });
+    def("__repr__",
+        [superCls, captureTypeName](nanobind::object self) -> nanobind::repr {
+          return nanobind::cast<std::string>(
+              nanobind::repr(superCls(self))
+                  .attr("replace")(superCls.attr("__name__"), captureTypeName));
+        });
     if (getTypeIDFunction) {
       // 'get_static_typeid' method.
       // This is modeled as a static method instead of a static property because

``````````

</details>


https://github.com/llvm/llvm-project/pull/182867


More information about the Mlir-commits mailing list