[Mlir-commits] [mlir] [MLIR] [Python] Fixed type errors in `get_parent_of_type` (PR #186079)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Mar 12 03:20:03 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Sergei Lebedev (superbobry)

<details>
<summary>Changes</summary>

`op.parent` can be `None`, and `None` is not assignable to a variable with type `OpView | Operation`.

Both mypy and Pyrefly flag this.

Perhaps, it's time we add a type checker sanity check to the CI? Wdyt, @<!-- -->makslevental? 

---
Full diff: https://github.com/llvm/llvm-project/pull/186079.diff


1 Files Affected:

- (modified) mlir/python/mlir/ir.py (+5-5) 


``````````diff
diff --git a/mlir/python/mlir/ir.py b/mlir/python/mlir/ir.py
index 710d9601f5443..536ee59e1c16d 100644
--- a/mlir/python/mlir/ir.py
+++ b/mlir/python/mlir/ir.py
@@ -35,13 +35,13 @@ def get_parent_of_type(op: OpView | Operation, op_class: type[OpView]) -> OpView
     if not (isinstance(op_class, type) and issubclass(op_class, OpView)):
         raise TypeError(f"op_class must be an OpView subclass, got {op_class!r}")
     try:
-        op = op.parent
+        parent = op.parent
     except ValueError:
         return None  # No parent chain.
-    while op is not None:
-        if isinstance(op.opview, op_class):
-            return op.opview
-        op = op.parent
+    while parent is not None:
+        if isinstance(parent.opview, op_class):
+            return parent.opview
+        parent = op.parent
     return None
 
 

``````````

</details>


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


More information about the Mlir-commits mailing list