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

clean up hygiene tests #4328

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4328.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix compile failure in declarative `#[pymodule]` under presence of `#![no_implicit_prelude]`.
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub fn pymodule_module_impl(
#module_items::_PYO3_DEF.add_to_module(module)?;
)*
#pymodule_init
Ok(())
::std::result::Result::Ok(())
}
}
))
Expand Down
18 changes: 3 additions & 15 deletions src/tests/hygiene/misc.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#![no_implicit_prelude]

#[derive(crate::FromPyObject)]
#[pyo3(crate = "crate")]
struct Derive1(#[allow(dead_code)] i32); // newtype case
struct Derive1(i32); // newtype case

#[derive(crate::FromPyObject)]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
struct Derive2(i32, i32); // tuple case

#[derive(crate::FromPyObject)]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
struct Derive3 {
f: i32,
#[pyo3(item(42))]
Expand All @@ -20,7 +16,6 @@ struct Derive3 {

#[derive(crate::FromPyObject)]
#[pyo3(crate = "crate")]
#[allow(dead_code)]
enum Derive4 {
A(i32),
B { f: i32 },
Expand All @@ -29,23 +24,16 @@ enum Derive4 {
crate::create_exception!(mymodule, CustomError, crate::exceptions::PyException);
crate::import_exception!(socket, gaierror);

#[allow(dead_code)]
fn intern(py: crate::Python<'_>) {
let _foo = crate::intern!(py, "foo");
let _bar = crate::intern!(py, stringify!(bar));
}

#[allow(dead_code)]
#[cfg(not(PyPy))]
fn append_to_inittab() {
#[crate::pymodule]
#[pyo3(crate = "crate")]
#[allow(clippy::unnecessary_wraps)]
fn module_for_inittab(
_: crate::Python<'_>,
_: &crate::Bound<'_, crate::types::PyModule>,
) -> crate::PyResult<()> {
::std::result::Result::Ok(())
}
mod module_for_inittab {}

crate::append_to_inittab!(module_for_inittab);
}
3 changes: 3 additions & 0 deletions src/tests/hygiene/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![no_implicit_prelude]
#![allow(dead_code, unused_variables, clippy::unnecessary_wraps)]

// The modules in this test are used to check PyO3 macro expansion is hygienic. By locating the test
// inside the crate the global `::pyo3` namespace is not available, so in combination with
// #[pyo3(crate = "crate")] this validates that all macro expansion respects the setting.
Expand Down
3 changes: 0 additions & 3 deletions src/tests/hygiene/pyclass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![no_implicit_prelude]
#![allow(unused_variables)]

#[crate::pyclass]
#[pyo3(crate = "crate")]
#[derive(::std::clone::Clone)]
Expand Down
15 changes: 1 addition & 14 deletions src/tests/hygiene/pyfunction.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
#![no_implicit_prelude]
#![allow(unused_variables, clippy::unnecessary_wraps)]

#[crate::pyfunction]
#[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x)
}

#[test]
#[cfg(feature = "gil-refs")]
fn invoke_wrap_pyfunction() {
crate::Python::with_gil(|py| {
#[allow(deprecated)]
let func = crate::wrap_pyfunction!(do_something)(py).unwrap();
crate::py_run!(py, func, r#"func(5)"#);
});
}

#[test]
fn invoke_wrap_pyfunction_bound() {
crate::Python::with_gil(|py| {
let func = crate::wrap_pyfunction_bound!(do_something, py).unwrap();
let func = crate::wrap_pyfunction!(do_something, py).unwrap();
crate::py_run!(py, func, r#"func(5)"#);
});
}
3 changes: 0 additions & 3 deletions src/tests/hygiene/pymethods.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#![no_implicit_prelude]
#![allow(unused_variables, clippy::unnecessary_wraps)]

#[crate::pyclass]
#[pyo3(crate = "crate")]
pub struct Dummy;
Expand Down
35 changes: 10 additions & 25 deletions src/tests/hygiene/pymodule.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
#![no_implicit_prelude]
#![allow(unused_variables, clippy::unnecessary_wraps)]

#[crate::pyfunction]
#[pyo3(crate = "crate")]
fn do_something(x: i32) -> crate::PyResult<i32> {
::std::result::Result::Ok(x)
}

#[cfg(feature = "gil-refs")]
#[allow(deprecated)]
#[crate::pymodule]
#[pyo3(crate = "crate")]
fn foo(_py: crate::Python<'_>, _m: &crate::types::PyModule) -> crate::PyResult<()> {
::std::result::Result::Ok(())
}

#[crate::pymodule]
#[pyo3(crate = "crate")]
fn foo_bound(
fn foo(
_py: crate::Python<'_>,
_m: &crate::Bound<'_, crate::types::PyModule>,
) -> crate::PyResult<()> {
::std::result::Result::Ok(())
}

#[cfg(feature = "gil-refs")]
#[allow(deprecated)]
#[crate::pymodule]
#[pyo3(crate = "crate")]
fn my_module(_py: crate::Python<'_>, m: &crate::types::PyModule) -> crate::PyResult<()> {
m.add_function(crate::wrap_pyfunction!(do_something, m)?)?;
m.add_wrapped(crate::wrap_pymodule!(foo))?;

::std::result::Result::Ok(())
}

#[crate::pymodule]
#[pyo3(crate = "crate")]
fn my_module_bound(m: &crate::Bound<'_, crate::types::PyModule>) -> crate::PyResult<()> {
fn my_module(m: &crate::Bound<'_, crate::types::PyModule>) -> crate::PyResult<()> {
<crate::Bound<'_, crate::types::PyModule> as crate::types::PyModuleMethods>::add_function(
m,
crate::wrap_pyfunction_bound!(do_something, m)?,
)?;
<crate::Bound<'_, crate::types::PyModule> as crate::types::PyModuleMethods>::add_wrapped(
m,
crate::wrap_pymodule!(foo_bound),
crate::wrap_pymodule!(foo),
)?;

::std::result::Result::Ok(())
}

#[crate::pymodule(submodule)]
#[pyo3(crate = "crate")]
mod my_module_declarative {
#[pymodule_export]
use super::{do_something, foo};
}
Loading