[Mlir-commits] [mlir] [mlir][python] Make Python bindings compatible with Nanobind v3.0.0. (PR #218562)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Aug 24 17:47:16 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Peter Hawkins (hawkinsp)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/218562.diff
1 Files Affected:
- (modified) mlir/include/mlir/Bindings/Python/NanobindUtils.h (+16-6)
``````````diff
diff --git a/mlir/include/mlir/Bindings/Python/NanobindUtils.h b/mlir/include/mlir/Bindings/Python/NanobindUtils.h
index ea43356d2cf54..a2950c70d1997 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,13 @@ 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) + "]";
+ elemTyName.c_str() + "]";
if constexpr (!Derived::typeParams.empty()) {
sig += ", typing.Generic[";
for (size_t i = 0; i < Derived::typeParams.size(); ++i) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/218562
More information about the Mlir-commits
mailing list