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

Sergei Lebedev llvmlistbot at llvm.org
Tue Feb 24 01:37:08 PST 2026


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

>From b7ddc492d5973acdc43f6ee94214517f50b63637 Mon Sep 17 00:00:00 2001
From: Sergei Lebedev <slebedev at google.com>
Date: Mon, 23 Feb 2026 15:12:23 +0000
Subject: [PATCH] [MLIR] [Python] Added return type to `__repr__` in the
 adaptors

Without an explicit annotation the return type is inferred as nanobind::object
which results in invalid type stubs, since __repr__ must return str.
---
 .../mlir/Bindings/Python/NanobindAdaptors.h   | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
index 918030824c409..538681053743e 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,11 @@ 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::str {
+          return 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



More information about the Mlir-commits mailing list