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

Maksim Levental llvmlistbot at llvm.org
Thu Dec 4 14:21:50 PST 2025


================
@@ -126,6 +134,10 @@ NB_MODULE(_mlir, m) {
               return valueCaster;
             });
       },
+      nb::sig("def register_value_caster(typeid: _mlir.ir.TypeID, *, "
+              "replace: bool = False) "
+              "-> typing.Callable[[typing.Callable[[T], U]], "
+              "typing.Callable[[T], U]]"),
----------------
makslevental wrote:

With what you currently have, the stub is actually generated correctly:

```python
from . import (
    ir as ir,
    ...
)

def register_value_caster(typeid: ir.TypeID, *, replace: bool = False) -> Callable[[Callable[[T], U]], Callable[[T], U]]:
    """Register a value caster for casting MLIR values to custom user values."""
```

but there are a lot of weird people doing a lot of weird things downstream (myself included) so I wonder if we can't depend on the main extension module being called `_mlir`. I tried an alternative (`MAKE_MLIR_PYTHON_QUALNAME("ir.TypeID")`) but that generates something which in principal should lead to a circular import:

```python
import mlir
def register_value_caster(typeid: mlir.ir.TypeID, *, replace: bool = False) -> Callable[[Callable[[T], U]], Callable[[T], U]]:
    """Register a value caster for casting MLIR values to custom user values."""
```

which we prior went to great lengths to avoid hmmm. 

Anyway I guess let's just leave what you have but as a nit can you please format like this:


```suggestion
      // clang-format off
      nb::sig("def register_value_caster(typeid: _mlir.ir.TypeID, *, replace: bool = False) "
                        "-> typing.Callable[[typing.Callable[[T], U]], typing.Callable[[T], U]]"),
      // clang-format on
```

https://github.com/llvm/llvm-project/pull/170627


More information about the Mlir-commits mailing list