[Mlir-commits] [mlir] [MLIR][Python] Register Containers as Sequences for `match` compatibility (PR #174091)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jan 1 02:08:46 PST 2026


================
@@ -4951,6 +4951,22 @@ void mlir::python::populateIRCore(nb::module_ &m) {
   PyRegionIterator::bind(m);
   PyRegionList::bind(m);
 
+  // Register containers as Sequences, so they can be used with `match`.
+  nanobind::object scope = m.attr("__dict__");
+  nanobind::exec("from collections.abc import Sequence, Mapping\n"
+                 "Sequence.register(BlockArgumentList)\n"
+                 "Sequence.register(BlockList)\n"
+                 "Sequence.register(BlockSuccessors)\n"
+                 "Sequence.register(BlockPredecessors)\n"
+                 "Sequence.register(OperationList)\n"
+                 "Sequence.register(OpOperandList)\n"
+                 "Sequence.register(OpResultList)\n"
+                 "Sequence.register(OpSuccessors)\n"
+                 "Sequence.register(RegionSequence)\n"
+                 "OpAttributeMap.get = Mapping.get\n"
----------------
MaPePeR wrote:

You are right, this was quite careless of me, I'm sorry. I thought it would just register it as an dynamic attribute, but the [nanobind documentation](https://nanobind.readthedocs.io/en/latest/classes.html#dynamic-attributes) says:

> By default, classes exported from C++ do not support this and the only writable attributes are the ones explicitly defined using [class_::def_rw()](https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4I00DpEN8nanobind6class_6def_rwER6class_PKcM1C1DDpRK5Extra) or [class_::def_prop_rw()](https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4I00DpEN8nanobind6class_11def_prop_rwER6class_PKcRR6GetterRR6SetterDpRK5Extra).
> [...]
> Trying to set any other attribute results in an error:

So its indeed a mystery why this even worked.

I have a branch prepared that implements `get` on the C++ side.

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


More information about the Mlir-commits mailing list