[Mlir-commits] [mlir] [mlir][python] Define __index__ on PyIntegerAttribute (PR #215555)
Peter Hawkins
llvmlistbot at llvm.org
Tue Aug 11 06:29:54 PDT 2026
https://github.com/hawkinsp created https://github.com/llvm/llvm-project/pull/215555
In nanobind v2.14.0+, integer casting from Python objects to C++ integer types was updated to require `__index__` (`PyNumber_Index`) instead of `__int__`.
https://nanobind.readthedocs.io/en/latest/changelog.html#version-2-14-0-aug-7-2026
Because `PyIntegerAttribute` defined `__int__` but not `__index__`, passing `IntegerAttr` instances to bindings expecting integer sequences failed. Specifically, in `mlir/test/python/ir/attributes.py`:
# CHECK: input: [IntegerAttr(4 : i64), IntegerAttr(2 : i64)] (<class 'list'>),
# CHECK-SAME: result: array<i8: 4, 2>
create_and_print(DenseI8ArrayAttr, [Attribute.parse(f"{x}") for x in [4, 2]])
Failed because `DenseI8ArrayAttr.get([IntegerAttr(4 : i64), IntegerAttr(2 : i64)])` raised:
TypeError: get(): incompatible function arguments. The following argument types are supported:
1. get(values: collections.abc.Sequence[int], context: _mlir.ir.Context | None = None) -> mlir._mlir_libs._mlir.ir.DenseI8ArrayAttr
Define `__index__` on `PyIntegerAttribute` using `toPyInt` to restore implicit integer conversions for `IntegerAttr`.
Assisted-by: Gemini
>From 4ab66e7c03418f10c9e13050c5ceed729b01dc23 Mon Sep 17 00:00:00 2001
From: Peter Hawkins <phawkins at google.com>
Date: Tue, 11 Aug 2026 13:25:40 +0000
Subject: [PATCH] [mlir][python] Define __index__ on PyIntegerAttribute
In nanobind v2.14.0+, integer casting from Python objects to C++ integer
types was updated to require `__index__` (`PyNumber_Index`) instead of `__int__`.
Because `PyIntegerAttribute` defined `__int__` but not `__index__`, passing
`IntegerAttr` instances to bindings expecting integer sequences failed.
Specifically, in `mlir/test/python/ir/attributes.py`:
# CHECK: input: [IntegerAttr(4 : i64), IntegerAttr(2 : i64)] (<class 'list'>),
# CHECK-SAME: result: array<i8: 4, 2>
create_and_print(DenseI8ArrayAttr, [Attribute.parse(f"{x}") for x in [4, 2]])
Failed because `DenseI8ArrayAttr.get([IntegerAttr(4 : i64), IntegerAttr(2 : i64)])` raised:
TypeError: get(): incompatible function arguments. The following argument types are supported:
1. get(values: collections.abc.Sequence[int], context: _mlir.ir.Context | None = None) -> mlir._mlir_libs._mlir.ir.DenseI8ArrayAttr
Define `__index__` on `PyIntegerAttribute` using `toPyInt` to restore implicit
integer conversions for `IntegerAttr`.
Assisted-by: Gemini
---
mlir/lib/Bindings/Python/IRAttributes.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 7fada5bbc8502..d4a71dd4937a9 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -423,6 +423,8 @@ void PyIntegerAttribute::bindDerived(ClassTy &c) {
c.def_prop_ro("value", toPyInt, "Returns the value of the integer attribute");
c.def("__int__", toPyInt,
"Converts the value of the integer attribute to a Python int");
+ c.def("__index__", toPyInt,
+ "Converts the value of the integer attribute to a Python int");
c.def_prop_ro_static("static_typeid", [](nb::object & /*class*/) {
return PyTypeID(mlirIntegerAttrGetTypeID());
});
More information about the Mlir-commits
mailing list