[Mlir-commits] [mlir] [mlir][Python] use canonical Python `isinstance` instead of `Type.isinstance` (PR #172892)

Maksim Levental llvmlistbot at llvm.org
Fri Dec 19 13:08:52 PST 2025


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/172892

>From 22d24204561882c4e4e9bc7dae392083dda0abd8 Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Thu, 18 Dec 2025 11:22:57 -0800
Subject: [PATCH 1/2] [mlir][Python] use canonical Python isinstance instead of
 Type.isinstance

---
 mlir/python/mlir/dialects/arith.py            |  24 +----
 .../dialects/linalg/opdsl/lang/emitter.py     | 101 ++++++------------
 mlir/python/mlir/dialects/memref.py           |  15 ++-
 mlir/test/python/dialects/arith_dialect.py    |   6 +-
 mlir/test/python/ir/auto_location.py          |   2 +-
 5 files changed, 50 insertions(+), 98 deletions(-)

diff --git a/mlir/python/mlir/dialects/arith.py b/mlir/python/mlir/dialects/arith.py
index 88e8502a29eae..555fb4c5ef3eb 100644
--- a/mlir/python/mlir/dialects/arith.py
+++ b/mlir/python/mlir/dialects/arith.py
@@ -21,26 +21,6 @@
     raise RuntimeError("Error loading imports from extension module") from e
 
 
-def _isa(obj: Any, cls: type):
-    try:
-        cls(obj)
-    except ValueError:
-        return False
-    return True
-
-
-def _is_any_of(obj: Any, classes: List[type]):
-    return any(_isa(obj, cls) for cls in classes)
-
-
-def _is_integer_like_type(type: Type):
-    return _is_any_of(type, [IntegerType, IndexType])
-
-
-def _is_float_type(type: Type):
-    return _is_any_of(type, [BF16Type, F16Type, F32Type, F64Type])
-
-
 @_ods_cext.register_operation(_Dialect, replace=True)
 class ConstantOp(ConstantOp):
     """Specialization for the constant op class."""
@@ -96,9 +76,9 @@ def value(self):
 
     @property
     def literal_value(self) -> Union[int, float]:
-        if _is_integer_like_type(self.type):
+        if isinstance(self.type, (IntegerType, IndexType)):
             return IntegerAttr(self.value).value
-        elif _is_float_type(self.type):
+        elif isinstance(self.type, FloatType):
             return FloatAttr(self.value).value
         else:
             raise ValueError("only integer and float constants have literal values")
diff --git a/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py b/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
index 254458a978828..a338643bad54d 100644
--- a/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
+++ b/mlir/python/mlir/dialects/linalg/opdsl/lang/emitter.py
@@ -412,9 +412,9 @@ def _cast(
             )
         if operand.type == to_type:
             return operand
-        if _is_integer_type(to_type):
+        if isinstance(to_type, IntegerType):
             return self._cast_to_integer(to_type, operand, is_unsigned_cast)
-        elif _is_floating_point_type(to_type):
+        elif isinstance(to_type, FloatType):
             return self._cast_to_floating_point(to_type, operand, is_unsigned_cast)
 
     def _cast_to_integer(
@@ -422,11 +422,11 @@ def _cast_to_integer(
     ) -> Value:
         to_width = IntegerType(to_type).width
         operand_type = operand.type
-        if _is_floating_point_type(operand_type):
+        if isinstance(operand_type, FloatType):
             if is_unsigned_cast:
                 return arith.FPToUIOp(to_type, operand).result
             return arith.FPToSIOp(to_type, operand).result
-        if _is_index_type(operand_type):
+        if isinstance(operand_type, IndexType):
             return arith.IndexCastOp(to_type, operand).result
         # Assume integer.
         from_width = IntegerType(operand_type).width
@@ -444,13 +444,15 @@ def _cast_to_floating_point(
         self, to_type: Type, operand: Value, is_unsigned_cast: bool
     ) -> Value:
         operand_type = operand.type
-        if _is_integer_type(operand_type):
+        if isinstance(operand_type, IntegerType):
             if is_unsigned_cast:
                 return arith.UIToFPOp(to_type, operand).result
             return arith.SIToFPOp(to_type, operand).result
         # Assume FloatType.
-        to_width = _get_floating_point_width(to_type)
-        from_width = _get_floating_point_width(operand_type)
+        assert isinstance(to_type, FloatType)
+        assert isinstance(operand_type, FloatType)
+        to_width = to_type.width
+        from_width = operand_type.width
         if to_width > from_width:
             return arith.ExtFOp(to_type, operand).result
         elif to_width < from_width:
@@ -466,89 +468,89 @@ def _type_cast_unsigned(self, type_var_name: str, operand: Value) -> Value:
         return self._cast(type_var_name, operand, True)
 
     def _unary_exp(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return math.ExpOp(x).result
         raise NotImplementedError("Unsupported 'exp' operand: {x}")
 
     def _unary_log(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return math.LogOp(x).result
         raise NotImplementedError("Unsupported 'log' operand: {x}")
 
     def _unary_abs(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return math.AbsFOp(x).result
         raise NotImplementedError("Unsupported 'abs' operand: {x}")
 
     def _unary_ceil(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return math.CeilOp(x).result
         raise NotImplementedError("Unsupported 'ceil' operand: {x}")
 
     def _unary_floor(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return math.FloorOp(x).result
         raise NotImplementedError("Unsupported 'floor' operand: {x}")
 
     def _unary_negf(self, x: Value) -> Value:
-        if _is_floating_point_type(x.type):
+        if isinstance(x.type, FloatType):
             return arith.NegFOp(x).result
-        if _is_complex_type(x.type):
+        if isinstance(x.type, ComplexType):
             return complex.NegOp(x).result
         raise NotImplementedError("Unsupported 'negf' operand: {x}")
 
     def _binary_add(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.AddFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.AddIOp(lhs, rhs).result
-        if _is_complex_type(lhs.type):
+        if isinstance(lhs.type, ComplexType):
             return complex.AddOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'add' operands: {lhs}, {rhs}")
 
     def _binary_sub(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.SubFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.SubIOp(lhs, rhs).result
-        if _is_complex_type(lhs.type):
+        if isinstance(lhs.type, ComplexType):
             return complex.SubOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'sub' operands: {lhs}, {rhs}")
 
     def _binary_mul(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.MulFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.MulIOp(lhs, rhs).result
-        if _is_complex_type(lhs.type):
+        if isinstance(lhs.type, ComplexType):
             return complex.MulOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'mul' operands: {lhs}, {rhs}")
 
     def _binary_max_signed(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.MaximumFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.MaxSIOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'max' operands: {lhs}, {rhs}")
 
     def _binary_max_unsigned(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.MaximumFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.MaxUIOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'max_unsigned' operands: {lhs}, {rhs}")
 
     def _binary_min_signed(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.MinimumFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.MinSIOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'min' operands: {lhs}, {rhs}")
 
     def _binary_min_unsigned(self, lhs: Value, rhs: Value) -> Value:
-        if _is_floating_point_type(lhs.type):
+        if isinstance(lhs.type, FloatType):
             return arith.MinimumFOp(lhs, rhs).result
-        if _is_integer_type(lhs.type) or _is_index_type(lhs.type):
+        if isinstance(lhs.type, IntegerType) or isinstance(lhs.type, IndexType):
             return arith.MinUIOp(lhs, rhs).result
         raise NotImplementedError("Unsupported 'min_unsigned' operands: {lhs}, {rhs}")
 
@@ -609,40 +611,3 @@ def _add_type_mapping(
             )
     type_mapping[name] = element_or_self_type
     block_arg_types.append(element_or_self_type)
-
-
-def _is_complex_type(t: Type) -> bool:
-    return ComplexType.isinstance(t)
-
-
-def _is_floating_point_type(t: Type) -> bool:
-    # TODO: Create a FloatType in the Python API and implement the switch
-    # there.
-    return (
-        F64Type.isinstance(t)
-        or F32Type.isinstance(t)
-        or F16Type.isinstance(t)
-        or BF16Type.isinstance(t)
-    )
-
-
-def _is_integer_type(t: Type) -> bool:
-    return IntegerType.isinstance(t)
-
-
-def _is_index_type(t: Type) -> bool:
-    return IndexType.isinstance(t)
-
-
-def _get_floating_point_width(t: Type) -> int:
-    # TODO: Create a FloatType in the Python API and implement the switch
-    # there.
-    if F64Type.isinstance(t):
-        return 64
-    if F32Type.isinstance(t):
-        return 32
-    if F16Type.isinstance(t):
-        return 16
-    if BF16Type.isinstance(t):
-        return 16
-    raise NotImplementedError(f"Unhandled floating point type switch {t}")
diff --git a/mlir/python/mlir/dialects/memref.py b/mlir/python/mlir/dialects/memref.py
index c80a1b1a89358..7922d5f45d908 100644
--- a/mlir/python/mlir/dialects/memref.py
+++ b/mlir/python/mlir/dialects/memref.py
@@ -7,15 +7,22 @@
 
 from ._memref_ops_gen import *
 from ._ods_common import _dispatch_mixed_values, MixedValues
-from .arith import ConstantOp, _is_integer_like_type
-from ..ir import Value, MemRefType, StridedLayoutAttr, ShapedType, Operation
+from ..ir import (
+    IndexType,
+    IntegerType,
+    MemRefType,
+    ShapedType,
+    StridedLayoutAttr,
+    Value,
+)
+from . import arith
 
 
 def _is_constant_int_like(i):
     return (
         isinstance(i, Value)
-        and isinstance(i.owner, ConstantOp)
-        and _is_integer_like_type(i.type)
+        and isinstance(i.owner, arith.ConstantOp)
+        and isinstance(i.type, (IntegerType, IndexType))
     )
 
 
diff --git a/mlir/test/python/dialects/arith_dialect.py b/mlir/test/python/dialects/arith_dialect.py
index c9af5e7b46db8..a4cfb30240231 100644
--- a/mlir/test/python/dialects/arith_dialect.py
+++ b/mlir/test/python/dialects/arith_dialect.py
@@ -42,10 +42,10 @@ def testFastMathFlags():
 def testArithValue():
     def _binary_op(lhs, rhs, op: str) -> "ArithValue":
         op = op.capitalize()
-        if arith._is_float_type(lhs.type) and arith._is_float_type(rhs.type):
+        if isinstance(lhs.type, FloatType) and isinstance(rhs.type, FloatType):
             op += "F"
-        elif arith._is_integer_like_type(lhs.type) and arith._is_integer_like_type(
-            lhs.type
+        elif isinstance(lhs.type, (IntegerType, IndexType)) and isinstance(
+            lhs.type, (IntegerType, IndexType)
         ):
             op += "I"
         else:
diff --git a/mlir/test/python/ir/auto_location.py b/mlir/test/python/ir/auto_location.py
index 1747c66aa6639..6448a88dc1775 100644
--- a/mlir/test/python/ir/auto_location.py
+++ b/mlir/test/python/ir/auto_location.py
@@ -34,7 +34,7 @@ def testInferLocations():
         # Test nesting of loc_tracebacks().
         with loc_tracebacks():
             # fmt: off
-            # CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]arith.py":65:12 to :76) at callsite("constant"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]arith.py":110:40 to :81) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:14 to :48) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:1 to :4))))))
+            # CHECK: loc(callsite("ConstantOp.__init__"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]arith.py":45:12 to :76) at callsite("constant"("{{.*}}[[SEP]]mlir[[SEP]]dialects[[SEP]]arith.py":90:40 to :81) at callsite("testInferLocations"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:14 to :48) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":{{[0-9]+}}:1 to :4))))))
             # fmt: on
             print(one.location)
 

>From 55b942524ef69dcf193432152b0df365b999390f Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Fri, 19 Dec 2025 11:23:29 -0800
Subject: [PATCH 2/2] remove "isinstance" from core bindings

---
 mlir/lib/Bindings/Python/IRAffine.cpp         |  6 ------
 mlir/lib/Bindings/Python/IRCore.cpp           |  6 ------
 mlir/lib/Bindings/Python/IRModule.h           | 12 ------------
 mlir/test/mlir-tblgen/op-python-bindings.td   |  2 +-
 mlir/test/python/ir/affine_expr.py            | 16 ++++++++--------
 mlir/test/python/ir/attributes.py             |  8 ++++----
 mlir/test/python/ir/builtin_types.py          | 10 +++++-----
 mlir/test/python/ir/value.py                  |  8 ++++----
 mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp |  2 +-
 9 files changed, 23 insertions(+), 47 deletions(-)

diff --git a/mlir/lib/Bindings/Python/IRAffine.cpp b/mlir/lib/Bindings/Python/IRAffine.cpp
index 7147f2cbad149..8d56a962fc5ae 100644
--- a/mlir/lib/Bindings/Python/IRAffine.cpp
+++ b/mlir/lib/Bindings/Python/IRAffine.cpp
@@ -114,12 +114,6 @@ class PyConcreteAffineExpr : public BaseTy {
   static void bind(nb::module_ &m) {
     auto cls = ClassTy(m, DerivedTy::pyClassName);
     cls.def(nb::init<PyAffineExpr &>(), nb::arg("expr"));
-    cls.def_static(
-        "isinstance",
-        [](PyAffineExpr &otherAffineExpr) -> bool {
-          return DerivedTy::isaFunction(otherAffineExpr);
-        },
-        nb::arg("other"));
     DerivedTy::bindDerived(cls);
   }
 
diff --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index 168c57955af07..c58073cfb70ee 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1490,12 +1490,6 @@ class PyConcreteValue : public PyValue {
                     .str()
                     .c_str()));
     cls.def(nb::init<PyValue &>(), nb::keep_alive<0, 1>(), nb::arg("value"));
-    cls.def_static(
-        "isinstance",
-        [](PyValue &otherValue) -> bool {
-          return DerivedTy::isaFunction(otherValue);
-        },
-        nb::arg("other_value"));
     cls.def(MLIR_PYTHON_MAYBE_DOWNCAST_ATTR,
             [](DerivedTy &self) -> nb::typed<nb::object, DerivedTy> {
               return self.maybeDownCast();
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index e706be3b4d32a..44ce196ce1398 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -960,12 +960,6 @@ class PyConcreteType : public BaseTy {
     auto cls = ClassTy(m, DerivedTy::pyClassName);
     cls.def(nanobind::init<PyType &>(), nanobind::keep_alive<0, 1>(),
             nanobind::arg("cast_from_type"));
-    cls.def_static(
-        "isinstance",
-        [](PyType &otherType) -> bool {
-          return DerivedTy::isaFunction(otherType);
-        },
-        nanobind::arg("other"));
     cls.def_prop_ro_static(
         "static_typeid",
         [](nanobind::object & /*class*/) {
@@ -1095,12 +1089,6 @@ class PyConcreteAttribute : public BaseTy {
     }
     cls.def(nanobind::init<PyAttribute &>(), nanobind::keep_alive<0, 1>(),
             nanobind::arg("cast_from_attr"));
-    cls.def_static(
-        "isinstance",
-        [](PyAttribute &otherAttr) -> bool {
-          return DerivedTy::isaFunction(otherAttr);
-        },
-        nanobind::arg("other"));
     cls.def_prop_ro(
         "type",
         [](PyAttribute &attr) -> nanobind::typed<nanobind::object, PyType> {
diff --git a/mlir/test/mlir-tblgen/op-python-bindings.td b/mlir/test/mlir-tblgen/op-python-bindings.td
index ff16ad8ca0cdd..929851724ba71 100644
--- a/mlir/test/mlir-tblgen/op-python-bindings.td
+++ b/mlir/test/mlir-tblgen/op-python-bindings.td
@@ -233,7 +233,7 @@ def DeriveResultTypesOp : TestOp<"derive_result_types_op", [FirstAttrDerivedResu
   // CHECK:     _ods_result_type_source_attr = attributes["type"]
   // CHECK:     _ods_derived_result_type = (
   // CHECK:       _ods_ir.TypeAttr(_ods_result_type_source_attr).value
-  // CHECK:       if _ods_ir.TypeAttr.isinstance(_ods_result_type_source_attr) else
+  // CHECK:       if isinstance(_ods_result_type_source_attr, _ods_ir.TypeAttr) else
   // CHECK:       _ods_result_type_source_attr.type)
   // CHECK:     results = [_ods_derived_result_type] * 2
   let arguments = (ins TypeAttr:$type);
diff --git a/mlir/test/python/ir/affine_expr.py b/mlir/test/python/ir/affine_expr.py
index c2a2ab3509ca6..82c509efdf8fc 100644
--- a/mlir/test/python/ir/affine_expr.py
+++ b/mlir/test/python/ir/affine_expr.py
@@ -354,21 +354,21 @@ def testIsInstance():
         mul = AffineMulExpr.get(d1, c2)
 
         # CHECK: True
-        print(AffineDimExpr.isinstance(d1))
+        print(isinstance(d1, AffineDimExpr))
         # CHECK: False
-        print(AffineConstantExpr.isinstance(d1))
+        print(isinstance(d1, AffineConstantExpr))
         # CHECK: True
-        print(AffineConstantExpr.isinstance(c2))
+        print(isinstance(c2, AffineConstantExpr))
         # CHECK: False
-        print(AffineMulExpr.isinstance(c2))
+        print(isinstance(c2, AffineMulExpr))
         # CHECK: True
-        print(AffineAddExpr.isinstance(add))
+        print(isinstance(add, AffineAddExpr))
         # CHECK: False
-        print(AffineMulExpr.isinstance(add))
+        print(isinstance(add, AffineMulExpr))
         # CHECK: True
-        print(AffineMulExpr.isinstance(mul))
+        print(isinstance(mul, AffineMulExpr))
         # CHECK: False
-        print(AffineAddExpr.isinstance(mul))
+        print(isinstance(mul, AffineAddExpr))
 
 
 # CHECK-LABEL: TEST: testCompose
diff --git a/mlir/test/python/ir/attributes.py b/mlir/test/python/ir/attributes.py
index 2f3c4460d3f59..5ab671bd4d298 100644
--- a/mlir/test/python/ir/attributes.py
+++ b/mlir/test/python/ir/attributes.py
@@ -94,10 +94,10 @@ def testAttrIsInstance():
     with Context():
         a1 = Attribute.parse("42")
         a2 = Attribute.parse("[42]")
-        assert IntegerAttr.isinstance(a1)
-        assert not IntegerAttr.isinstance(a2)
-        assert not ArrayAttr.isinstance(a1)
-        assert ArrayAttr.isinstance(a2)
+        assert isinstance(a1, IntegerAttr)
+        assert not isinstance(a2, IntegerAttr)
+        assert not isinstance(a1, ArrayAttr)
+        assert isinstance(a2, ArrayAttr)
 
 
 # CHECK-LABEL: TEST: testAttrEqDoesNotRaise
diff --git a/mlir/test/python/ir/builtin_types.py b/mlir/test/python/ir/builtin_types.py
index 54863253fc770..aa1665a4020fc 100644
--- a/mlir/test/python/ir/builtin_types.py
+++ b/mlir/test/python/ir/builtin_types.py
@@ -97,15 +97,15 @@ def testTypeIsInstance():
     t1 = Type.parse("i32", ctx)
     t2 = Type.parse("f32", ctx)
     # CHECK: True
-    print(IntegerType.isinstance(t1))
+    print(isinstance(t1, IntegerType))
     # CHECK: False
-    print(F32Type.isinstance(t1))
+    print(isinstance(t1, F32Type))
     # CHECK: False
-    print(FloatType.isinstance(t1))
+    print(isinstance(t1, FloatType))
     # CHECK: True
-    print(F32Type.isinstance(t2))
+    print(isinstance(t2, F32Type))
     # CHECK: True
-    print(FloatType.isinstance(t2))
+    print(isinstance(t2, FloatType))
 
 
 # CHECK-LABEL: TEST: testFloatTypeSubclasses
diff --git a/mlir/test/python/ir/value.py b/mlir/test/python/ir/value.py
index 4a241afb8e89d..45efb880bab44 100644
--- a/mlir/test/python/ir/value.py
+++ b/mlir/test/python/ir/value.py
@@ -69,12 +69,12 @@ def testValueIsInstance():
         ctx,
     )
     func = module.body.operations[0]
-    assert BlockArgument.isinstance(func.regions[0].blocks[0].arguments[0])
-    assert not OpResult.isinstance(func.regions[0].blocks[0].arguments[0])
+    assert isinstance(func.regions[0].blocks[0].arguments[0], BlockArgument)
+    assert not isinstance(func.regions[0].blocks[0].arguments[0], OpResult)
 
     op = func.regions[0].blocks[0].operations[0]
-    assert not BlockArgument.isinstance(op.results[0])
-    assert OpResult.isinstance(op.results[0])
+    assert not isinstance(op.results[0], BlockArgument)
+    assert isinstance(op.results[0], OpResult)
 
 
 # CHECK-LABEL: TEST: testValueHash
diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
index 2c33f4efac3ac..6545559ff1b10 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
@@ -886,7 +886,7 @@ constexpr const char *firstAttrDerivedResultTypeTemplate =
   _ods_result_type_source_attr = attributes["{0}"]
   _ods_derived_result_type = (
     _ods_ir.TypeAttr(_ods_result_type_source_attr).value
-    if _ods_ir.TypeAttr.isinstance(_ods_result_type_source_attr) else
+    if isinstance(_ods_result_type_source_attr, _ods_ir.TypeAttr) else
     _ods_result_type_source_attr.type)
   results = [_ods_derived_result_type] * {1})Py";
 



More information about the Mlir-commits mailing list