[Mlir-commits] [mlir] 6a12b7c - [mlir][python] Fix Python binding cast diagnostics for nanobind 2.13 (#206391)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Jun 28 21:05:59 PDT 2026


Author: Maksim Levental
Date: 2026-06-28T21:05:54-07:00
New Revision: 6a12b7c74b179fbd1b40466f57d94dd8ed53cf16

URL: https://github.com/llvm/llvm-project/commit/6a12b7c74b179fbd1b40466f57d94dd8ed53cf16
DIFF: https://github.com/llvm/llvm-project/commit/6a12b7c74b179fbd1b40466f57d94dd8ed53cf16.diff

LOG: [mlir][python] Fix Python binding cast diagnostics for nanobind 2.13 (#206391)

nanobind 2.13.0 changed failing `nb::cast<T>()` to raise `cast_error`
(an alias for `std::bad_cast`) for all failures, including `None`,
instead of the previous distinct exception types. The bindings relied on
catching `std::runtime_error` separately from `cast_error` to emit a
helpful "(None?)" hint, so that branch became dead code and the `None`
diagnostics regressed (e.g. "(std::bad_cast)" instead of "(None?)"),
breaking check-mlir Python tests.

Fix: Decide the `None` hint from the value via `is_none()` rather than
the exception type. This is robust across nanobind versions (2.10-2.13).

Fixes #205329 

Assisted by: Claude

Added: 
    

Modified: 
    mlir/include/mlir/Bindings/Python/IRAttributes.h
    mlir/lib/Bindings/Python/IRAffine.cpp
    mlir/lib/Bindings/Python/IRCore.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Bindings/Python/IRAttributes.h b/mlir/include/mlir/Bindings/Python/IRAttributes.h
index 7f2f88b552453..fcc003ab365fa 100644
--- a/mlir/include/mlir/Bindings/Python/IRAttributes.h
+++ b/mlir/include/mlir/Bindings/Python/IRAttributes.h
@@ -100,16 +100,17 @@ template <typename T>
 static T pyTryCast(nanobind::handle object) {
   try {
     return nanobind::cast<T>(object);
-  } catch (nanobind::cast_error &err) {
+  } catch (std::exception &err) {
+    if (object.is_none()) {
+      std::string msg = std::string("Invalid attribute (None?) when attempting "
+                                    "to create an ArrayAttribute (") +
+                        err.what() + ")";
+      throw std::runtime_error(msg.c_str());
+    }
     std::string msg = std::string("Invalid attribute when attempting to "
                                   "create an ArrayAttribute (") +
                       err.what() + ")";
     throw std::runtime_error(msg.c_str());
-  } catch (std::runtime_error &err) {
-    std::string msg = std::string("Invalid attribute (None?) when attempting "
-                                  "to create an ArrayAttribute (") +
-                      err.what() + ")";
-    throw std::runtime_error(msg.c_str());
   }
 }
 

diff  --git a/mlir/lib/Bindings/Python/IRAffine.cpp b/mlir/lib/Bindings/Python/IRAffine.cpp
index 07aa4759bae5f..85c6a849fdb1e 100644
--- a/mlir/lib/Bindings/Python/IRAffine.cpp
+++ b/mlir/lib/Bindings/Python/IRAffine.cpp
@@ -43,14 +43,15 @@ static void pyListToVector(const nb::sequence &list, std::vector<CType> &result,
   for (nb::handle item : list) {
     try {
       result.push_back(nb::cast<PyType>(item));
-    } catch (nb::cast_error &err) {
+    } catch (std::exception &err) {
+      if (item.is_none()) {
+        std::string msg = nanobind::detail::join(
+            "Invalid expression (None?) when ", action, " (", err.what(), ")");
+        throw std::runtime_error(msg.c_str());
+      }
       std::string msg = nanobind::detail::join("Invalid expression when ",
                                                action, " (", err.what(), ")");
       throw std::runtime_error(msg.c_str());
-    } catch (std::runtime_error &err) {
-      std::string msg = nanobind::detail::join(
-          "Invalid expression (None?) when ", action, " (", err.what(), ")");
-      throw std::runtime_error(msg.c_str());
     }
   }
 }

diff  --git a/mlir/lib/Bindings/Python/IRCore.cpp b/mlir/lib/Bindings/Python/IRCore.cpp
index fc69ef8884466..ab75bc20e123e 100644
--- a/mlir/lib/Bindings/Python/IRCore.cpp
+++ b/mlir/lib/Bindings/Python/IRCore.cpp
@@ -1272,17 +1272,17 @@ nb::object PyOperation::create(std::string_view name,
         auto &attribute = nb::cast<PyAttribute &>(it.second);
         // TODO: Verify attribute originates from the same context.
         mlirAttributes.emplace_back(std::move(key), attribute);
-      } catch (nb::cast_error &err) {
+      } catch (std::exception &err) {
+        if (it.second.is_none()) {
+          std::string msg = join(
+              "Found an invalid (`None`?) attribute value for the key \"", key,
+              "\" when attempting to create the operation \"", name, "\"");
+          throw std::runtime_error(msg);
+        }
         std::string msg = join("Invalid attribute value for the key \"", key,
                                "\" when attempting to create the operation \"",
                                name, "\" (", err.what(), ")");
         throw nb::type_error(msg.c_str());
-      } catch (std::runtime_error &) {
-        // This exception seems thrown when the value is "None".
-        std::string msg = join(
-            "Found an invalid (`None`?) attribute value for the key \"", key,
-            "\" when attempting to create the operation \"", name, "\"");
-        throw std::runtime_error(msg);
       }
     }
   }


        


More information about the Mlir-commits mailing list