[Mlir-commits] [mlir] [MLIR] [Python] A few minor fixes to type annotations (PR #187956)
Sergei Lebedev
llvmlistbot at llvm.org
Sun Mar 22 15:36:44 PDT 2026
https://github.com/superbobry created https://github.com/llvm/llvm-project/pull/187956
* ir.IntegerAttr.value is now an int
* ir.Value.type correctly returns a value of type _T
* ir.OpView.build_generic returns ir.Operation, which matches the implementation.
>From e0db636d60ef61a4d901f1d99ad4c5e3f9d508e9 Mon Sep 17 00:00:00 2001
From: Sergei Lebedev <slebedev at google.com>
Date: Sun, 22 Mar 2026 22:34:17 +0000
Subject: [PATCH] [MLIR] [Python] A few minor fixes to type annotations
* ir.IntegerAttr.value is now an int
* ir.Value.type correctly returns a value of type _T
* ir.OpView.build_generic returns ir.Operation, which matches the
implementation.
---
mlir/include/mlir/Bindings/Python/IRAttributes.h | 2 +-
mlir/include/mlir/Bindings/Python/IRCore.h | 2 +-
mlir/lib/Bindings/Python/IRAttributes.cpp | 4 ++--
mlir/lib/Bindings/Python/IRCore.cpp | 6 +++---
4 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/mlir/include/mlir/Bindings/Python/IRAttributes.h b/mlir/include/mlir/Bindings/Python/IRAttributes.h
index 64a31d3f3e42d..28a7134dc2d45 100644
--- a/mlir/include/mlir/Bindings/Python/IRAttributes.h
+++ b/mlir/include/mlir/Bindings/Python/IRAttributes.h
@@ -342,7 +342,7 @@ class MLIR_PYTHON_API_EXPORTED PyIntegerAttribute
static void bindDerived(ClassTy &c);
private:
- static nanobind::object toPyInt(PyIntegerAttribute &self);
+ static nanobind::int_ toPyInt(PyIntegerAttribute &self);
};
/// Bool Attribute subclass - BoolAttr.
diff --git a/mlir/include/mlir/Bindings/Python/IRCore.h b/mlir/include/mlir/Bindings/Python/IRCore.h
index f24b3c6ac6f80..55d6f61324500 100644
--- a/mlir/include/mlir/Bindings/Python/IRCore.h
+++ b/mlir/include/mlir/Bindings/Python/IRCore.h
@@ -739,7 +739,7 @@ class MLIR_PYTHON_API_EXPORTED PyOpView : public PyOperationBase {
nanobind::object getOperationObject() { return operationObject; }
- static nanobind::object
+ static nanobind::typed<nanobind::object, PyOperation>
buildGeneric(std::string_view name, std::tuple<int, bool> opRegionSpec,
nanobind::object operandSegmentSpecObj,
nanobind::object resultSegmentSpecObj,
diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index a0cfaa441bfbf..9f5602cc61b35 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -426,7 +426,7 @@ void PyIntegerAttribute::bindDerived(ClassTy &c) {
});
}
-nb::object PyIntegerAttribute::toPyInt(PyIntegerAttribute &self) {
+nb::int_ PyIntegerAttribute::toPyInt(PyIntegerAttribute &self) {
MlirType type = mlirAttributeGetType(self);
unsigned bitWidth = mlirIntegerAttrGetValueBitWidth(self);
@@ -463,7 +463,7 @@ nb::object PyIntegerAttribute::toPyInt(PyIntegerAttribute &self) {
}
}
- return result;
+ return nb::cast<nb::int_>(result);
}
void PyBoolAttribute::bindDerived(ClassTy &c) {
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 89e1e21cd1240..f3f1ee4ce343f 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1591,7 +1591,7 @@ static MlirValue getOpResultOrValue(nb::handle operand) {
throw nb::value_error("is not a Value");
}
-nb::object PyOpView::buildGeneric(
+nb::typed<nb::object, PyOperation> PyOpView::buildGeneric(
std::string_view name, std::tuple<int, bool> opRegionSpec,
nb::object operandSegmentSpecObj, nb::object resultSegmentSpecObj,
std::optional<nb::list> resultTypeList, nb::list operandList,
@@ -4844,12 +4844,12 @@ void populateIRCore(nb::module_ &m) {
"Returns the string form of value as an operand (i.e., the ValueID).")
.def_prop_ro(
"type",
- [](PyValue &self) -> nb::typed<nb::object, PyType> {
+ [](PyValue &self) {
return PyType(self.getParentOperation()->getContext(),
mlirValueGetType(self.get()))
.maybeDownCast();
},
- "Returns the type of the value.")
+ "Returns the type of the value.", nb::sig("def type(self) -> _T"))
.def(
"set_type",
[](PyValue &self, const PyType &type) {
More information about the Mlir-commits
mailing list