[Mlir-commits] [mlir] [mlir][python] Make Python bindings compatible with Nanobind v3.0.0. (PR #218562)
Peter Hawkins
llvmlistbot at llvm.org
Tue Aug 25 04:55:51 PDT 2026
https://github.com/hawkinsp updated https://github.com/llvm/llvm-project/pull/218562
>From e96925e88f0259c43ce0d2189a4d5bfac89962e2 Mon Sep 17 00:00:00 2001
From: Peter Hawkins <phawkins at google.com>
Date: Tue, 25 Aug 2026 00:40:38 +0000
Subject: [PATCH] [mlir][python] Make Python bindings compatible with Nanobind
v3.0.0.
Nanobind v3.0.0 has several breaking changes, notably changes to how
iterators work and removal of several nanobind::detail APIs.
https://nanobind.readthedocs.io/en/latest/changelog.html#version-3-0-0-aug-22-2026
Assisted-by: Gemini
---
.../mlir/Bindings/Python/NanobindUtils.h | 23 +++++++++++++------
1 file changed, 16 insertions(+), 7 deletions(-)
diff --git a/mlir/include/mlir/Bindings/Python/NanobindUtils.h b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
index ea43356d2cf54..4f3dd7446b588 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindUtils.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
@@ -24,6 +24,16 @@
#include <typeinfo>
#include <variant>
+#if NB_VERSION_MAJOR >= 3
+template <bool IsTuple>
+struct std::iterator_traits<nanobind::detail::seq_iterator<IsTuple>> {
+ using value_type = nanobind::handle;
+ using reference = const value_type;
+ using pointer = void;
+ using difference_type = std::ptrdiff_t;
+ using iterator_category = std::forward_iterator_tag;
+};
+#else
template <>
struct std::iterator_traits<nanobind::detail::fast_iterator> {
using value_type = nanobind::handle;
@@ -32,6 +42,7 @@ struct std::iterator_traits<nanobind::detail::fast_iterator> {
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;
};
+#endif
namespace mlir {
namespace python {
@@ -475,14 +486,12 @@ class Sliceable {
return nullptr;
})},
{0, nullptr}};
- const std::type_info &elemTy = typeid(ElementTy);
- PyObject *elemTyInfo = nanobind::detail::nb_type_lookup(&elemTy);
- assert(elemTyInfo &&
- "expected nb_type_lookup to succeed for Sliceable elemTy");
- nanobind::handle elemTyName = nanobind::detail::nb_type_name(elemTyInfo);
+ nanobind::handle elemTyInfo = nanobind::type<ElementTy>();
+ assert(elemTyInfo.is_valid() &&
+ "expected nanobind::type to succeed for Sliceable elemTy");
+ nanobind::str elemTyName = nanobind::type_name(elemTyInfo);
std::string sig = std::string("class ") + Derived::pyClassName +
- "(collections.abc.Sequence[" +
- nanobind::cast<std::string>(elemTyName) + "]";
+ "(collections.abc.Sequence[" + elemTyName.c_str() + "]";
if constexpr (!Derived::typeParams.empty()) {
sig += ", typing.Generic[";
for (size_t i = 0; i < Derived::typeParams.size(); ++i) {
More information about the Mlir-commits
mailing list