|
23 | 23 |
|
24 | 24 | #include "mlir-c/Diagnostics.h" |
25 | 25 | #include "mlir-c/IR.h" |
| 26 | +// clang-format off |
26 | 27 | #include "mlir/Bindings/Python/Nanobind.h" |
27 | 28 | #include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind. |
| 29 | +// clang-format on |
28 | 30 | #include "llvm/ADT/Twine.h" |
29 | 31 |
|
30 | 32 | // Raw CAPI type casters need to be declared before use, so always include them |
@@ -349,6 +351,7 @@ class pure_subclass { |
349 | 351 | thisClass = metaclass(derivedClassName, nanobind::make_tuple(superClass), |
350 | 352 | attributes); |
351 | 353 | scope.attr(derivedClassName) = thisClass; |
| 354 | + thisClass.attr("__module__") = scope.attr("__name__"); |
352 | 355 | } |
353 | 356 |
|
354 | 357 | template <typename Func, typename... Extra> |
@@ -434,7 +437,7 @@ class mlir_attribute_subclass : public pure_subclass { |
434 | 437 | const nanobind::object &superCls, |
435 | 438 | GetTypeIDFunctionTy getTypeIDFunction = nullptr) |
436 | 439 | : pure_subclass(scope, typeClassName, superCls) { |
437 | | - // Casting constructor. Note that it hard, if not impossible, to properly |
| 440 | + // Casting constructor. Note that it is hard, if not impossible, to properly |
438 | 441 | // call chain to parent `__init__` in nanobind due to its special handling |
439 | 442 | // for init functions that don't have a fully constructed self-reference, |
440 | 443 | // which makes it impossible to forward it to `__init__` of a superclass. |
@@ -465,10 +468,13 @@ class mlir_attribute_subclass : public pure_subclass { |
465 | 468 | thisClass.attr("__new__") = newCf; |
466 | 469 |
|
467 | 470 | // 'isinstance' method. |
| 471 | + static const char kIsinstanceSig[] = |
| 472 | + "def isinstance(other_attribute: " MAKE_MLIR_PYTHON_QUALNAME( |
| 473 | + "ir") ".Attribute) -> bool"; |
468 | 474 | def_staticmethod( |
469 | 475 | "isinstance", |
470 | 476 | [isaFunction](MlirAttribute other) { return isaFunction(other); }, |
471 | | - nanobind::arg("other_attribute")); |
| 477 | + nanobind::arg("other_attribute"), nanobind::sig(kIsinstanceSig)); |
472 | 478 | def("__repr__", [superCls, captureTypeName](nanobind::object self) { |
473 | 479 | return nanobind::repr(superCls(self)) |
474 | 480 | .attr("replace")(superCls.attr("__name__"), captureTypeName); |
@@ -512,7 +518,7 @@ class mlir_type_subclass : public pure_subclass { |
512 | 518 | const nanobind::object &superCls, |
513 | 519 | GetTypeIDFunctionTy getTypeIDFunction = nullptr) |
514 | 520 | : pure_subclass(scope, typeClassName, superCls) { |
515 | | - // Casting constructor. Note that it hard, if not impossible, to properly |
| 521 | + // Casting constructor. Note that it is hard, if not impossible, to properly |
516 | 522 | // call chain to parent `__init__` in nanobind due to its special handling |
517 | 523 | // for init functions that don't have a fully constructed self-reference, |
518 | 524 | // which makes it impossible to forward it to `__init__` of a superclass. |
@@ -542,13 +548,17 @@ class mlir_type_subclass : public pure_subclass { |
542 | 548 | thisClass.attr("__new__") = newCf; |
543 | 549 |
|
544 | 550 | // 'isinstance' method. |
| 551 | + static const char kIsinstanceSig[] = |
| 552 | + "def isinstance(other_type: " MAKE_MLIR_PYTHON_QUALNAME( |
| 553 | + "ir") ".Type) -> bool"; |
545 | 554 | def_staticmethod( |
546 | 555 | "isinstance", |
547 | 556 | [isaFunction](MlirType other) { return isaFunction(other); }, |
548 | | - nanobind::arg("other_type")); |
| 557 | + nanobind::arg("other_type"), nanobind::sig(kIsinstanceSig)); |
549 | 558 | def("__repr__", [superCls, captureTypeName](nanobind::object self) { |
550 | | - return nanobind::repr(superCls(self)) |
551 | | - .attr("replace")(superCls.attr("__name__"), captureTypeName); |
| 559 | + return nanobind::cast<std::string>( |
| 560 | + nanobind::repr(superCls(self)) |
| 561 | + .attr("replace")(superCls.attr("__name__"), captureTypeName)); |
552 | 562 | }); |
553 | 563 | if (getTypeIDFunction) { |
554 | 564 | // 'get_static_typeid' method. |
@@ -590,7 +600,7 @@ class mlir_value_subclass : public pure_subclass { |
590 | 600 | IsAFunctionTy isaFunction, |
591 | 601 | const nanobind::object &superCls) |
592 | 602 | : pure_subclass(scope, valueClassName, superCls) { |
593 | | - // Casting constructor. Note that it hard, if not impossible, to properly |
| 603 | + // Casting constructor. Note that it is hard, if not impossible, to properly |
594 | 604 | // call chain to parent `__init__` in nanobind due to its special handling |
595 | 605 | // for init functions that don't have a fully constructed self-reference, |
596 | 606 | // which makes it impossible to forward it to `__init__` of a superclass. |
@@ -620,10 +630,13 @@ class mlir_value_subclass : public pure_subclass { |
620 | 630 | thisClass.attr("__new__") = newCf; |
621 | 631 |
|
622 | 632 | // 'isinstance' method. |
| 633 | + static const char kIsinstanceSig[] = |
| 634 | + "def isinstance(other_value: " MAKE_MLIR_PYTHON_QUALNAME( |
| 635 | + "ir") ".Value) -> bool"; |
623 | 636 | def_staticmethod( |
624 | 637 | "isinstance", |
625 | 638 | [isaFunction](MlirValue other) { return isaFunction(other); }, |
626 | | - nanobind::arg("other_value")); |
| 639 | + nanobind::arg("other_value"), nanobind::sig(kIsinstanceSig)); |
627 | 640 | } |
628 | 641 | }; |
629 | 642 |
|
|
0 commit comments