Skip to content
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 nanobinds stubgen #127584

Merged
merged 3 commits into from
Feb 19, 2025

Conversation

ingomueller-net
Copy link
Contributor

@ingomueller-net ingomueller-net commented Feb 18, 2025

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 #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.

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]>
@ingomueller-net
Copy link
Contributor Author

@hawkinsp: You might also be interested.

@llvmbot
Copy link
Member

llvmbot commented Feb 18, 2025

@llvm/pr-subscribers-mlir

Author: Ingo Müller (ingomueller-net)

Changes

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 #127518.


Full diff: https://github.com/llvm/llvm-project/pull/127584.diff

1 Files Affected:

  • (modified) mlir/include/mlir/Bindings/Python/NanobindAdaptors.h (+1)
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>

Copy link
Contributor

@hawkinsp hawkinsp left a 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]>
@ingomueller-net ingomueller-net changed the title [mlir:python] Set __module__ classes generated by nanobind adaptors. [mlir:python] Improve mlir_(attribute|type|value)_subclass for nanobinds stubgen Feb 18, 2025
@ingomueller-net
Copy link
Contributor Author

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.

Copy link
Contributor

@makslevental makslevental left a 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.

@ingomueller-net ingomueller-net merged commit 6b67aac into llvm:main Feb 19, 2025
8 checks passed
@ingomueller-net ingomueller-net deleted the fix-nanobind-adaptors branch February 19, 2025 07:07
Prakhar-Dixit pushed a commit to Prakhar-Dixit/llvm-project that referenced this pull request Feb 19, 2025
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[mlir:python] Types created by mlir_type_subclass are ignored by nanobind's stubgen
4 participants