Skip to content

Commit

Permalink
Create pyo3-stub-gen-testing-mixed
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Jan 5, 2024
1 parent 86572a3 commit 6164c73
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 8 deletions.
15 changes: 10 additions & 5 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
8 changes: 7 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
15 changes: 15 additions & 0 deletions pyo3-stub-gen-testing-mixed/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions pyo3-stub-gen-testing-mixed/pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"]
7 changes: 7 additions & 0 deletions pyo3-stub-gen-testing-mixed/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
32 changes: 32 additions & 0 deletions pyo3-stub-gen-testing-mixed/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<StubInfo> {
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<String> {
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);
}
}
5 changes: 5 additions & 0 deletions pyo3-stub-gen-testing-mixed/tests/test_python.py
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions pyo3-stub-gen-testing-pure/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
pyo3-stub-gen.workspace = true
pyo3.workspace = true

[[bin]]
name = "stub_gen"
doc = false

0 comments on commit 6164c73

Please sign in to comment.