-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir:python] Improve mlir_(attribute|type|value)_subclass
for nanobind
s stubgen
#127584
[mlir:python] Improve mlir_(attribute|type|value)_subclass
for nanobind
s stubgen
#127584
Conversation
This PR sets the `__module__` attribute of the classes generated by the nanobind adaptors for attributes, types, and values (via `mlir_(attribute|type|value)_subclass`). By default, the `__module__` property is set to `importlib._bootstrap`, which isn't where we want the new class to live. The new logic sets the property to the name of the module provided as `scope` instead. This also makes nanobind's `stubgen` generate stubs for those classes properly, which ignores classes whose `__module__` does not correspond to the module it is generating stubs for. This resolves llvm#127518. Signed-off-by: Ingo Müller <[email protected]>
@hawkinsp: You might also be interested. |
@llvm/pr-subscribers-mlir Author: Ingo Müller (ingomueller-net) ChangesThis PR sets the Full diff: https://github.com/llvm/llvm-project/pull/127584.diff 1 Files Affected:
diff --git a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
index 517351cac6dbc..11338b2d49308 100644
--- a/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
+++ b/mlir/include/mlir/Bindings/Python/NanobindAdaptors.h
@@ -349,6 +349,7 @@ class pure_subclass {
thisClass = metaclass(derivedClassName, nanobind::make_tuple(superClass),
attributes);
scope.attr(derivedClassName) = thisClass;
+ thisClass.attr("__module__") = scope.attr("__name__");
}
template <typename Func, typename... Extra>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, for what that's worth.
This commit overwrites the signature of the `isinstance` member functions created by `mlir_(attribute|type|value)_subclass` by adding a `nanobind::signature` argument to the `def` call that creates the functions. This improves the typing information that `stubgen` creates for such classes. Signed-off-by: Ingo Müller <[email protected]>
This commit fixes three occurrences of the same grammar issue in some comment and adds a clang-format suppression for a specific ordering of includes. Signed-off-by: Ingo Müller <[email protected]>
__module__
classes generated by nanobind adaptors.mlir_(attribute|type|value)_subclass
for nanobind
s stubgen
Thanks, @hawkinsp, for the review! I realized that some more tweaks are required to generate fully correct stubs and expanded the scope of this PR to include them. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I don't think we have tests for type hints but I trust you verified this works.
…obind`s `stubgen` (llvm#127584) This PR makes several improvements to the stubs that are created by `mlir_(attribute|type|value)_subclass`. First, the PR sets the `__module__` attribute of the classes generated by the nanobind adaptors for attributes, types, and values (via `mlir_(attribute|type|value)_subclass`). By default, the `__module__` property is set to `importlib._bootstrap`, which isn't where we want the new class to live. The new logic sets the property to the name of the module provided as `scope` instead. This also makes nanobind's `stubgen` generate stubs for those classes properly, which ignores classes whose `__module__` does not correspond to the module it is generating stubs for. This resolves llvm#127518. Second, the PR overwrites the function signatures generated by `stubgen` to a format that uses the desired type names (e.g., `mlir.ir.Attribute` instead of `MlirAttribute`). Finally, the PR piggy-backs some minor doc and style improvements to `PythonAdaptors.h`. --------- Signed-off-by: Ingo Müller <[email protected]>
This PR makes several improvements to the stubs that are created by
mlir_(attribute|type|value)_subclass
.First, the PR sets the
__module__
attribute of the classes generated by the nanobind adaptors for attributes, types, and values (viamlir_(attribute|type|value)_subclass
). By default, the__module__
property is set toimportlib._bootstrap
, which isn't where we want the new class to live. The new logic sets the property to the name of the module provided asscope
instead. This also makes nanobind'sstubgen
generate stubs for those classes properly, which ignores classes whose__module__
does not correspond to the module it is generating stubs for. This resolves #127518.Second, the PR overwrites the function signatures generated by
stubgen
to a format that uses the desired type names (e.g.,mlir.ir.Attribute
instead ofMlirAttribute
).Finally, the PR piggy-backs some minor doc and style improvements to
PythonAdaptors.h
.