Skip to content

Commit

Permalink
add experimental-async feature
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Mar 4, 2024
1 parent 4114dcb commit a59c45f
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pyo3-build-config = { path = "pyo3-build-config", version = "=0.21.0-dev", featu
[features]
default = ["macros"]

# Enables support for `async fn` for `#[pyfunction]` and `#[pymethods]`.
experimental-async = ["macros", "pyo3-macros/experimental-async"]

# Enables pyo3::inspect module and additional type information on FromPyObject
# and IntoPy traits
experimental-inspect = []
Expand Down Expand Up @@ -116,8 +119,9 @@ full = [
"chrono",
"chrono-tz",
"either",
"experimental-inspect",
"experimental-async",
"experimental-declarative-modules",
"experimental-inspect",
"eyre",
"hashbrown",
"indexmap",
Expand Down
6 changes: 6 additions & 0 deletions guide/src/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ If you do not enable this feature, you should call `pyo3::prepare_freethreaded_p

## Advanced Features

### `experimental-async`

This feature adds support for `async fn` in `#[pyfunction]` and `#[pymethods]`.

The feature has some unfinished refinements and performance improvements. To help finish this off, see [issue #1632](https://github.com/PyO3/pyo3/issues/1632) and its associated draft PRs.

### `experimental-inspect`

This feature adds the `pyo3::inspect` module, as well as `IntoPy::type_output` and `FromPyObject::type_input` APIs to produce Python type "annotations" for Rust types.
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3931.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `experimental-async` feature.
3 changes: 3 additions & 0 deletions pyo3-macros-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ features = ["derive", "parsing", "printing", "clone-impls", "full", "extra-trait

[lints]
workspace = true

[features]
experimental-async = []
7 changes: 7 additions & 0 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,13 @@ impl<'a> FnSpec<'a> {
}
}

if self.asyncness.is_some() {
ensure_spanned!(
cfg!(feature = "experimental-async"),
self.asyncness.span() => "async functions are only supported with the `experimental-async` feature"
);
}

let rust_call = |args: Vec<TokenStream>, holders: &mut Vec<TokenStream>| {
let self_arg = self.tp.self_arg(cls, ExtractErrorMode::Raise, holders, ctx);

Expand Down
1 change: 1 addition & 0 deletions pyo3-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ proc-macro = true

[features]
multiple-pymethods = []
experimental-async = ["pyo3-macros-backend/experimental-async"]
experimental-declarative-modules = []

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! APIs may may change at any time without documentation in the CHANGELOG and without
//! breaking semver guarantees.
#[cfg(feature = "macros")]
#[cfg(feature = "experimental-async")]
pub mod coroutine;
pub mod deprecations;
pub mod extract_argument;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ pub mod buffer;
pub mod callback;
pub mod conversion;
mod conversions;
#[cfg(feature = "macros")]
#[cfg(feature = "experimental-async")]
pub mod coroutine;
#[macro_use]
#[doc(hidden)]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_coroutine.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature = "macros")]
#![cfg(feature = "experimental-async")]
#![cfg(not(target_arch = "wasm32"))]
use std::{task::Poll, thread, time::Duration};

Expand Down

0 comments on commit a59c45f

Please sign in to comment.