From 6164c734cc47d67d74ec807ea52013d698c04ddd Mon Sep 17 00:00:00 2001 From: Toshiki Teramura Date: Fri, 5 Jan 2024 18:55:04 +0900 Subject: [PATCH] Create pyo3-stub-gen-testing-mixed --- .github/workflows/python.yml | 15 ++++++--- .github/workflows/rust.yml | 8 ++++- Cargo.toml | 3 +- README.md | 2 +- pyo3-stub-gen-testing-mixed/Cargo.toml | 15 +++++++++ pyo3-stub-gen-testing-mixed/pyproject.toml | 10 ++++++ .../src/bin/stub_gen.rs | 7 ++++ pyo3-stub-gen-testing-mixed/src/lib.rs | 32 +++++++++++++++++++ .../tests/test_python.py | 5 +++ pyo3-stub-gen-testing-pure/Cargo.toml | 4 +++ 10 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 pyo3-stub-gen-testing-mixed/Cargo.toml create mode 100644 pyo3-stub-gen-testing-mixed/pyproject.toml create mode 100644 pyo3-stub-gen-testing-mixed/src/bin/stub_gen.rs create mode 100644 pyo3-stub-gen-testing-mixed/src/lib.rs create mode 100644 pyo3-stub-gen-testing-mixed/tests/test_python.py diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml index 3b71944..9201416 100644 --- a/.github/workflows/python.yml +++ b/.github/workflows/python.yml @@ -8,8 +8,14 @@ on: workflow_dispatch: jobs: - pytest: + test: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + crate: + - pyo3-stub-gen-testing-pure + - pyo3-stub-gen-testing-mixed steps: - name: Checkout uses: actions/checkout@v4 @@ -30,11 +36,10 @@ jobs: profile: minimal - name: Build and install - run: | - pip install -v ./pyo3-stub-gen-testing[test] + run: pip install -v "./${{ matrix.crate }}[test]" - name: Python Test - run: pytest + run: pytest ./${{ matrix.crate }} - name: Type check - run: pyright + run: pyright ./${{ matrix.crate }} diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d9e084e..f3e43ca 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -95,6 +95,12 @@ jobs: stub-gen: runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + crate: + - pyo3-stub-gen-testing-pure + - pyo3-stub-gen-testing-mixed steps: - name: Checkout uses: actions/checkout@v4 @@ -113,7 +119,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: run - args: --bin stub_gen + args: --bin stub_gen -p ${{ matrix.crate }} - name: Check if stub file is up to date run: git diff --exit-code diff --git a/Cargo.toml b/Cargo.toml index b15d4f7..c2d751c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,8 @@ members = [ "pyo3-stub-gen", "pyo3-stub-gen-derive", - "pyo3-stub-gen-testing-pure" + "pyo3-stub-gen-testing-pure", + "pyo3-stub-gen-testing-mixed", ] resolver = "2" diff --git a/README.md b/README.md index b175218..8950311 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ And then, create an executable target in `src/bin/stub_gen.rs`: use pyo3_stub_gen::Result; fn main() -> Result<()> { - let stub = pyo3_stub_gen_testing::stub_info()?; + let stub = pyo3_stub_gen_testing_pure::stub_info()?; stub.generate_single_stub_file(env!("CARGO_MANIFEST_DIR"))?; Ok(()) } diff --git a/pyo3-stub-gen-testing-mixed/Cargo.toml b/pyo3-stub-gen-testing-mixed/Cargo.toml new file mode 100644 index 0000000..1fbb01f --- /dev/null +++ b/pyo3-stub-gen-testing-mixed/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "pyo3-stub-gen-testing-mixed" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +pyo3-stub-gen.workspace = true +pyo3.workspace = true + +[[bin]] +name = "stub_gen" +doc = false diff --git a/pyo3-stub-gen-testing-mixed/pyproject.toml b/pyo3-stub-gen-testing-mixed/pyproject.toml new file mode 100644 index 0000000..24fa51a --- /dev/null +++ b/pyo3-stub-gen-testing-mixed/pyproject.toml @@ -0,0 +1,10 @@ +[build-system] +requires = ["maturin>=1.1,<2.0"] +build-backend = "maturin" + +[project] +name = "pyo3_stub_gen_testing_mixed" +requires-python = ">=3.9" + +[project.optional-dependencies] +test = ["pytest", "pyright"] diff --git a/pyo3-stub-gen-testing-mixed/src/bin/stub_gen.rs b/pyo3-stub-gen-testing-mixed/src/bin/stub_gen.rs new file mode 100644 index 0000000..8b09860 --- /dev/null +++ b/pyo3-stub-gen-testing-mixed/src/bin/stub_gen.rs @@ -0,0 +1,7 @@ +use pyo3_stub_gen::Result; + +fn main() -> Result<()> { + let stub = pyo3_stub_gen_testing_mixed::stub_info()?; + stub.generate_single_stub_file(env!("CARGO_MANIFEST_DIR"))?; + Ok(()) +} diff --git a/pyo3-stub-gen-testing-mixed/src/lib.rs b/pyo3-stub-gen-testing-mixed/src/lib.rs new file mode 100644 index 0000000..af1d9fb --- /dev/null +++ b/pyo3-stub-gen-testing-mixed/src/lib.rs @@ -0,0 +1,32 @@ +use pyo3::prelude::*; +use pyo3_stub_gen::{derive::gen_stub_pyfunction, StubInfo}; +use std::{env, path::*}; + +/// Gather information to generate stub files +pub fn stub_info() -> pyo3_stub_gen::Result { + let manifest_dir: &Path = env!("CARGO_MANIFEST_DIR").as_ref(); + StubInfo::from_pyproject_toml(manifest_dir.join("pyproject.toml")) +} + +/// Returns the sum of two numbers as a string. +#[gen_stub_pyfunction] +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +/// Initializes the Python module +#[pymodule] +fn pyo3_stub_gen_testing_mixed(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + Ok(()) +} + +/// Test of unit test for testing link problem +#[cfg(test)] +mod test { + #[test] + fn test() { + assert_eq!(2 + 2, 4); + } +} diff --git a/pyo3-stub-gen-testing-mixed/tests/test_python.py b/pyo3-stub-gen-testing-mixed/tests/test_python.py new file mode 100644 index 0000000..a27c772 --- /dev/null +++ b/pyo3-stub-gen-testing-mixed/tests/test_python.py @@ -0,0 +1,5 @@ +import pyo3_stub_gen_testing_pure + + +def test_sum_as_string(): + assert pyo3_stub_gen_testing_pure.sum_as_string(1, 2) == "3" diff --git a/pyo3-stub-gen-testing-pure/Cargo.toml b/pyo3-stub-gen-testing-pure/Cargo.toml index 825af34..954f45a 100644 --- a/pyo3-stub-gen-testing-pure/Cargo.toml +++ b/pyo3-stub-gen-testing-pure/Cargo.toml @@ -9,3 +9,7 @@ crate-type = ["cdylib", "rlib"] [dependencies] pyo3-stub-gen.workspace = true pyo3.workspace = true + +[[bin]] +name = "stub_gen" +doc = false