[Mlir-commits] [mlir] [MLIR][Python] make Sliceable inherit from Sequence (PR #170551)

Maksim Levental llvmlistbot at llvm.org
Wed Dec 3 12:23:16 PST 2025


https://github.com/makslevental created https://github.com/llvm/llvm-project/pull/170551

None

>From e2291a908b95afb1da4153983063332520c2981e Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Wed, 3 Dec 2025 12:22:06 -0800
Subject: [PATCH] [MLIR][Python] make Sliceable inherit from Sequence

---
 mlir/lib/Bindings/Python/NanobindUtils.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Bindings/Python/NanobindUtils.h b/mlir/lib/Bindings/Python/NanobindUtils.h
index 658e8ad5330ef..3fbff7bfd088b 100644
--- a/mlir/lib/Bindings/Python/NanobindUtils.h
+++ b/mlir/lib/Bindings/Python/NanobindUtils.h
@@ -19,6 +19,7 @@
 #include "llvm/Support/raw_ostream.h"
 
 #include <string>
+#include <typeinfo>
 #include <variant>
 
 template <>
@@ -344,7 +345,16 @@ class Sliceable {
 
   /// Binds the indexing and length methods in the Python class.
   static void bind(nanobind::module_ &m) {
-    auto clazz = nanobind::class_<Derived>(m, Derived::pyClassName)
+    const std::type_info &elemTy = typeid(ElementTy);
+    PyObject *elemTyInfo = nanobind::detail::nb_type_lookup(&elemTy);
+    nanobind::handle elemTyName = nanobind::detail::nb_type_name(elemTyInfo);
+    std::string sig = "class ";
+    sig += Derived::pyClassName;
+    sig += "(collections.abc.Sequence[";
+    sig += nanobind::cast<std::string>(elemTyName);
+    sig += "])";
+    auto clazz = nanobind::class_<Derived>(m, Derived::pyClassName,
+                                           nanobind::sig(sig.c_str()))
                      .def("__add__", &Sliceable::dunderAdd);
     Derived::bindDerived(clazz);
 



More information about the Mlir-commits mailing list