[Mlir-commits] [mlir] [mlir][Python] Generate type stubs for dialect extensions (PR #236657)

Maksim Levental llvmlistbot at llvm.org
Thu Oct 29 21:11:00 PDT 2026


================
@@ -598,6 +636,32 @@ function(add_mlir_python_modules name)
     _process_target(${name} ${sources_target} "${_mlir_python_support_libs}")
   endforeach()
 
+  if(MLIR_PYTHON_STUBGEN_ENABLED)
+    # Import package initializers only after its native extensions and Python
+    # sources are ready. Depend on concrete targets, not the package aggregate:
+    # the latter also depends on the generated stubs and would form a cycle.
+    set(_stubgen_inputs ${ARG_STUBGEN_DEPENDS_TARGETS})
+    foreach(sources_target ${_flat_targets})
+      get_target_property(_source_type ${sources_target} mlir_python_SOURCES_TYPE)
+      if(_source_type STREQUAL "extension")
+        get_target_property(_module_name ${sources_target} mlir_python_EXTENSION_MODULE_NAME)
+        list(APPEND _stubgen_inputs "${name}.extension.${_module_name}.dso")
+      elseif(_source_type STREQUAL "pure")
+        list(APPEND _stubgen_inputs "${name}.sources.${sources_target}")
----------------
makslevental wrote:

I think this is roughly the reason I never did this for the "leaf extensions":
https://github.com/llvm/llvm-project/blob/bd9712adb3b03751ca8e51d62efa66977edd0fb1/mlir/python/CMakeLists.txt#L919-L925

So the core extension type stubs are generated without doing an import from package root exactly so that none of the module initializers (all the stuff in `__init__.py`s) get run (because I was afraid there would be arbitrary code in there that we couldn't depend on being in place.

Can you try doing it the way I did for the core extension where only the leaf extension is imported. I think you'll have trouble with this because you do need to import the core extension as well. Maybe codex can figure this out 🤷.

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


More information about the Mlir-commits mailing list