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

Sergei Lebedev llvmlistbot at llvm.org
Thu Mar 12 03:19:27 PDT 2026


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

`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? 

>From 797944744e038ceb583de566217d261c6bfeb835 Mon Sep 17 00:00:00 2001
From: Sergei Lebedev <slebedev at google.com>
Date: Thu, 12 Mar 2026 10:13:46 +0000
Subject: [PATCH] [MLIR] [Python] Fixed type errors in `get_parent_of_type`

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

Both mypy and Pyrefly flag this.
---
 mlir/python/mlir/ir.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

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
 
 



More information about the Mlir-commits mailing list