-
Notifications
You must be signed in to change notification settings - Fork 1k
experimental-inspect: no-parameter pymodule_init keeps module complete
#6271
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
Merged
davidhewitt
merged 10 commits into
PyO3:main
from
jonasdedden:experimental-inspect-pymodule-init-keeps-module-complete
Aug 27, 2026
+348
−6
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
81e64e2
feat(inspect): let a no-argument #[pymodule_init] keep the module com…
jonasdedden f921e37
Rename
jonasdedden 5679497
test: cover the no-argument `#[pymodule_init]` paths
jonasdedden 9001564
Review
jonasdedden f2b01cd
Allow `pymodule_init` also to be unfallible
jonasdedden 36ee47d
Address review
jonasdedden dd18e95
Update guide/src/module.md
jonasdedden e6f0128
Merge branch 'PyO3:main' into experimental-inspect-pymodule-init-keep…
jonasdedden db5d07b
Address review: optional `Python` argument and better diagnostics
jonasdedden 39f99f5
Address review: keep `split_off_python_arg` in `pymethod`, span init …
jonasdedden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| A `#[pymodule_init]` function may now return `()` instead of `PyResult<()>`, which is useful for initialisers which cannot fail. | ||
| The return type is now checked: an initialiser returning a `Result` with a non-unit `Ok` value used to compile, with that value silently discarded, and is now rejected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `#[pymodule_init]` may now be written without the module argument, and may take a `Python<'_>` marker in front of it or instead of it. An initialiser which is not handed the module leaves it complete, so with `experimental-inspect` the module is no longer tagged incomplete and its stubs no longer get a `def __getattr__(name: str) -> Incomplete: ...` catch-all. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pymodule] | ||
| mod module { | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pymodule_init] | ||
| fn init(_m: &Bound<'_, PyModule>, _extra: usize) -> PyResult<()> { | ||
| //~^ ERROR: `#[pymodule_init]` takes an optional `Python` argument followed by an optional module argument | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[pymodule] | ||
| mod module_with_python_last { | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pymodule_init] | ||
| fn init(_m: &Bound<'_, PyModule>, _py: Python<'_>) -> PyResult<()> { | ||
| //~^ ERROR: `#[pymodule_init]` takes an optional `Python` argument followed by an optional module argument | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| error: `#[pymodule_init]` takes an optional `Python` argument followed by an optional module argument | ||
| --> tests/ui/invalid_pymodule_init_args.rs:8:5 | ||
| | | ||
| 8 | fn init(_m: &Bound<'_, PyModule>, _extra: usize) -> PyResult<()> { | ||
| | ^^ | ||
|
|
||
| error: `#[pymodule_init]` takes an optional `Python` argument followed by an optional module argument | ||
| --> tests/ui/invalid_pymodule_init_args.rs:19:5 | ||
| | | ||
| 19 | fn init(_m: &Bound<'_, PyModule>, _py: Python<'_>) -> PyResult<()> { | ||
| | ^^ | ||
|
|
||
| error: aborting due to 2 previous errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pymodule] | ||
| mod module { | ||
| use pyo3::prelude::*; | ||
|
|
||
| #[pymodule_init] | ||
| #[pyfunction] | ||
| //~^ ERROR: `#[pyfunction]` cannot be used alongside `#[pymodule_init]` | ||
| fn init(_m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| error: `#[pyfunction]` cannot be used alongside `#[pymodule_init]` | ||
| --> tests/ui/invalid_pymodule_init_pyfunction.rs:8:5 | ||
| | | ||
| 8 | #[pyfunction] | ||
| | ^ | ||
|
|
||
| error: aborting due to 1 previous error |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.