From a78b3fd166cee13b3c1c43e73a03a434ab1c8e68 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 29 Apr 2022 12:54:06 +0200 Subject: [PATCH 1/4] Re-export `__all__` in `__init__.py` for pure Rust project This is useful for Python documentation generators which inspect `__all__`. For example, pdoc then correctly displays `module_name` instead of `module_name.module_name` --- guide/src/project_layout.md | 3 ++- src/module_writer.rs | 2 +- src/templates/__init__.py.j2 | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/guide/src/project_layout.md b/guide/src/project_layout.md index 6d0e36cfc..d8767fef5 100644 --- a/guide/src/project_layout.md +++ b/guide/src/project_layout.md @@ -22,7 +22,8 @@ wheel. For convenience, this file includes the following: ```python from .my_project import * -__doc__ = .my_project.__doc__ +__doc__ = my_project.__doc__ +__all__ = my_project.__all__ ``` such that the module functions may be called directly with: diff --git a/src/module_writer.rs b/src/module_writer.rs index 267486e95..db5378ff4 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -656,7 +656,7 @@ pub fn write_bindings_module( writer.add_bytes( &module.join("__init__.py"), format!( - "from .{module_name} import *\n\n__doc__ = {module_name}.__doc__\n", + "from .{module_name} import *\n\n__doc__ = {module_name}.__doc__\n__all__ = {module_name}.__all__\n", module_name = module_name ) .as_bytes(), diff --git a/src/templates/__init__.py.j2 b/src/templates/__init__.py.j2 index 145182845..17b58b0e1 100644 --- a/src/templates/__init__.py.j2 +++ b/src/templates/__init__.py.j2 @@ -2,3 +2,4 @@ from .{{ crate_name }} import * __doc__ = {{ crate_name }}.__doc__ +__all__ = {{ crate_name }}.__all__ From c06742fbc3eabff37f2e0bcc9b97552b35f4a1fa Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 29 Apr 2022 13:24:28 +0200 Subject: [PATCH 2/4] only re-export `__all__` if it's set --- guide/src/project_layout.md | 3 ++- src/module_writer.rs | 6 +++++- src/templates/__init__.py.j2 | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/guide/src/project_layout.md b/guide/src/project_layout.md index d8767fef5..d1f17fd98 100644 --- a/guide/src/project_layout.md +++ b/guide/src/project_layout.md @@ -23,7 +23,8 @@ wheel. For convenience, this file includes the following: from .my_project import * __doc__ = my_project.__doc__ -__all__ = my_project.__all__ +if hasattr(my_project, "__all__"): + __all__ = my_project.__all__ ``` such that the module functions may be called directly with: diff --git a/src/module_writer.rs b/src/module_writer.rs index db5378ff4..9205618ad 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -656,7 +656,11 @@ pub fn write_bindings_module( writer.add_bytes( &module.join("__init__.py"), format!( - "from .{module_name} import *\n\n__doc__ = {module_name}.__doc__\n__all__ = {module_name}.__all__\n", + "from .{module_name} import *\n\ + \n\ + __doc__ = {module_name}.__doc__\n\ + if hasattr({module_name}, \"__all__\"):\n\ + \t__all__ = {module_name}.__all__\n", module_name = module_name ) .as_bytes(), diff --git a/src/templates/__init__.py.j2 b/src/templates/__init__.py.j2 index 17b58b0e1..a64034451 100644 --- a/src/templates/__init__.py.j2 +++ b/src/templates/__init__.py.j2 @@ -2,4 +2,5 @@ from .{{ crate_name }} import * __doc__ = {{ crate_name }}.__doc__ -__all__ = {{ crate_name }}.__all__ +if hasattr({{ crate_name }}, "__all__"): + __all__ = {{ crate_name }}.__all__ From 14907bbe3a4b312ba55c986b23bbb1866d913a87 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 29 Apr 2022 13:39:19 +0200 Subject: [PATCH 3/4] use a raw string literal Co-authored-by: messense --- src/module_writer.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/module_writer.rs b/src/module_writer.rs index 9205618ad..8cc8f3b83 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -656,11 +656,11 @@ pub fn write_bindings_module( writer.add_bytes( &module.join("__init__.py"), format!( - "from .{module_name} import *\n\ - \n\ - __doc__ = {module_name}.__doc__\n\ - if hasattr({module_name}, \"__all__\"):\n\ - \t__all__ = {module_name}.__all__\n", + r#"from .{module_name} import * + +__doc__ = {module_name}.__doc__ +if hasattr({module_name}, "__all__"): + __all__ = {module_name}.__all__"#, module_name = module_name ) .as_bytes(), From 86a6588c6d103a6d61dea4dd3783579aea32cca1 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Fri, 29 Apr 2022 14:12:53 +0200 Subject: [PATCH 4/4] add changelog entry --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index f7e2c38f4..d56ea103c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +* Re-export `__all__` for pure Rust projects in [#886](https://github.com/PyO3/maturin/pull/886) + ## [0.12.14] - 2022-04-25 * Fix PyPy pep517 build when abi3 feature is enabled in [#883](https://github.com/PyO3/maturin/pull/883)