[Mlir-commits] [mlir] [mlir:python] Add manual typing annotations to `mlir.register_*` functions. (PR #170627)

Ingo Müller llvmlistbot at llvm.org
Thu Dec 4 00:56:32 PST 2025


https://github.com/ingomueller-net created https://github.com/llvm/llvm-project/pull/170627

This PR adds a manual typing annotations to the `register_operation` and `register_(type|value)_caster` functions in the main `mlir` module. Since those functions return the result `nb::cpp_function`, which is of type `nb::object`, the automatic typing annocations are of the form `def f() -> object`. This isn't particularly precise and leads to type checking errors when the functions are used. Manually defining the annotation with `nb::sig` solves the problem.

>From 60b41370f596d2a93cbe5db1ba63a431f1ed54a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ingo=20M=C3=BCller?= <ingomueller at google.com>
Date: Thu, 4 Dec 2025 08:51:38 +0000
Subject: [PATCH] [mlir:python] Add manual typing annotations to
 `mlir.register_*` functions.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This PR adds a manual typing annotations to the `register_operation` and
`register_(type|value)_caster` functions in the main `mlir` module.
Since those functions return the result `nb::cpp_function`, which is of
type `nb::object`, the automatic typing annocations are of the form
`def f() -> object`. This isn't particularly precise and leads to type
checking errors when the functions are used. Manually defining the
annotation with `nb::sig` solves the problem.

Signed-off-by: Ingo Müller <ingomueller at google.com>
---
 mlir/lib/Bindings/Python/MainModule.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mlir/lib/Bindings/Python/MainModule.cpp b/mlir/lib/Bindings/Python/MainModule.cpp
index a14f09f77d2c3..38259c9d962e5 100644
--- a/mlir/lib/Bindings/Python/MainModule.cpp
+++ b/mlir/lib/Bindings/Python/MainModule.cpp
@@ -102,6 +102,8 @@ NB_MODULE(_mlir, m) {
               return opClass;
             });
       },
+      nb::sig("def register_operation(dialect_class: type, *, "
+              "replace: bool = False) -> typing.Callable[..., type]"),
       "dialect_class"_a, nb::kw_only(), "replace"_a = false,
       "Produce a class decorator for registering an Operation class as part of "
       "a dialect");
@@ -114,6 +116,9 @@ NB_MODULE(_mlir, m) {
           return typeCaster;
         });
       },
+      nb::sig(
+          "def register_type_caster(typeid: mlir.ir.TypeID, *, "
+          "replace: bool = False) -> typing.Callable[...,  mlir.ir.TypeID]"),
       "typeid"_a, nb::kw_only(), "replace"_a = false,
       "Register a type caster for casting MLIR types to custom user types.");
   m.def(
@@ -126,6 +131,9 @@ NB_MODULE(_mlir, m) {
               return valueCaster;
             });
       },
+      nb::sig(
+          "def register_value_caster(typeid: mlir.ir.TypeID, *, "
+          "replace: bool = False) -> typing.Callable[...,  mlir.ir.TypeID]"),
       "typeid"_a, nb::kw_only(), "replace"_a = false,
       "Register a value caster for casting MLIR values to custom user values.");
 



More information about the Mlir-commits mailing list