[Mlir-commits] [mlir] 69594b7 - [MLIR] [Python] A few minor fixes to type annotations (#187956)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Mar 23 00:54:55 PDT 2026


Author: Sergei Lebedev
Date: 2026-03-23T07:54:50Z
New Revision: 69594b76f2ecb2ee254ccb5b9f9f02ca76931630

URL: https://github.com/llvm/llvm-project/commit/69594b76f2ecb2ee254ccb5b9f9f02ca76931630
DIFF: https://github.com/llvm/llvm-project/commit/69594b76f2ecb2ee254ccb5b9f9f02ca76931630.diff

LOG: [MLIR] [Python] A few minor fixes to type annotations (#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.

Added: 
    

Modified: 
    mlir/include/mlir/Bindings/Python/IRAttributes.h
    mlir/include/mlir/Bindings/Python/IRCore.h
    mlir/lib/Bindings/Python/IRAttributes.cpp
    mlir/lib/Bindings/Python/IRCore.cpp

Removed: 
    


################################################################################
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