Skip to content

Commit

Permalink
Merge pull request #1 from Jij-Inc/testing
Browse files Browse the repository at this point in the history
Testing crate
  • Loading branch information
termoshtt authored Jan 4, 2024
2 parents 3d98cd4 + 6334589 commit 9269b47
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 25 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Python

on:
push:
branches:
- "main"
pull_request:
workflow_dispatch:

jobs:
pytest:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: 3.9

- name: Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
profile: minimal

- name: Build and install
run: |
pip install -v ./pyo3-stub-gen-testing[test]
- name: Python Test
run: pytest
22 changes: 22 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,25 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test

stub-gen:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: clippy

- name: Cache dependencies
uses: Swatinem/rust-cache@v2

- name: Generate stub file
uses: actions-rs/cargo@v1
with:
command: run
args: --bin stub_gen --features stub_gen
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# PyO3 development
.venv*/
__pycache__/
*.so
*.pyc
dist/
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[workspace]
members = ["pyo3-stub-gen", "pyo3-stub-gen-derive"]
members = [
"pyo3-stub-gen",
"pyo3-stub-gen-derive",
"pyo3-stub-gen-testing"
]
resolver = "2"

[workspace.dependencies]
Expand All @@ -12,6 +16,6 @@ inventory = "0.3.14"
itertools = "0.12.0"
prettyplease = "0.2.16"
proc-macro2 = "1.0.74"
pyo3 = "0.20.1"
pyo3 = { version = "0.20.1", features = ["experimental-inspect"] }
quote = "1.0.35"
syn = "2.0.46"
2 changes: 1 addition & 1 deletion pyo3-stub-gen-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ pyo3-stub-gen.workspace = true
insta.workspace = true
inventory.workspace = true
prettyplease.workspace = true
pyo3 = { workspace = true, features = ["experimental-inspect"] }
pyo3.workspace = true
4 changes: 0 additions & 4 deletions pyo3-stub-gen-derive/src/gen_stub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ pub fn pyclass(item: TokenStream2) -> Result<TokenStream2> {
let inner = PyClassInfo::try_from(parse2::<ItemStruct>(item.clone())?)?;
Ok(quote! {
#item
#[cfg(feature = "gen_stub")]
inventory::submit! {
#inner
}
Expand All @@ -102,7 +101,6 @@ pub fn pyclass_enum(item: TokenStream2) -> Result<TokenStream2> {
let inner = PyEnumInfo::try_from(parse2::<ItemEnum>(item.clone())?)?;
Ok(quote! {
#item
#[cfg(feature = "gen_stub")]
inventory::submit! {
#inner
}
Expand All @@ -113,7 +111,6 @@ pub fn pymethods(item: TokenStream2) -> Result<TokenStream2> {
let inner = PyMethodsInfo::try_from(parse2::<ItemImpl>(item.clone())?)?;
Ok(quote! {
#item
#[cfg(feature = "gen_stub")]
inventory::submit! {
#inner
}
Expand All @@ -125,7 +122,6 @@ pub fn pyfunction(attr: TokenStream2, item: TokenStream2) -> Result<TokenStream2
inner.parse_attr(attr)?;
Ok(quote! {
#item
#[cfg(feature = "gen_stub")]
inventory::submit! {
#inner
}
Expand Down
4 changes: 2 additions & 2 deletions pyo3-stub-gen-derive/src/gen_stub/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn type_to_token(ty: &Type) -> TokenStream2 {
// PyO3 reference: https://docs.rs/pyo3/latest/pyo3/pyclass/enum.CompareOp.html
// PEP: https://peps.python.org/pep-0207/
if last_seg.ident == "CompareOp" {
quote! { crate::stub::compare_op_type_input }
quote! { ::pyo3_stub_gen::type_info::compare_op_type_input }
} else {
quote! { <#ty as FromPyObject>::type_input }
}
Expand Down Expand Up @@ -105,7 +105,7 @@ impl ToTokens for ArgInfo {
let Self { name, r#type: ty } = self;
let type_tt = type_to_token(ty);
tokens.append_all(quote! {
crate::stub::ArgInfo { name: #name, r#type: #type_tt }
::pyo3_stub_gen::type_info::ArgInfo { name: #name, r#type: #type_tt }
});
}
}
2 changes: 1 addition & 1 deletion pyo3-stub-gen-derive/src/gen_stub/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ToTokens for MemberInfo {
let Self { name, r#type: ty } = self;
let name = name.strip_prefix("get_").unwrap_or(name);
tokens.append_all(quote! {
crate::stub::MemberInfo {
::pyo3_stub_gen::type_info::MemberInfo {
name: #name,
r#type: <#ty as IntoPy<PyObject>>::type_output
}
Expand Down
4 changes: 2 additions & 2 deletions pyo3-stub-gen-derive/src/gen_stub/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ impl ToTokens for MethodInfo {
let ret_tt = if let Some(ret) = ret {
quote! { <#ret as IntoPy<::pyo3::PyObject>>::type_output }
} else {
quote! { crate::stub::no_return_type_output }
quote! { ::pyo3_stub_gen::type_info::no_return_type_output }
};
tokens.append_all(quote! {
crate::stub::MethodInfo {
::pyo3_stub_gen::type_info::MethodInfo {
name: #name,
args: &[ #(#args),* ],
r#return: #ret_tt,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-stub-gen-derive/src/gen_stub/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ToTokens for NewInfo {
let Self { args, sig } = self;
let sig_tt = quote_option(sig);
tokens.append_all(quote! {
crate::stub::NewInfo {
::pyo3_stub_gen::type_info::NewInfo {
args: &[ #(#args),* ],
signature: #sig_tt,
}
Expand Down
10 changes: 5 additions & 5 deletions pyo3-stub-gen-derive/src/gen_stub/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ToTokens for PyClassInfo {
} = self;
let module = quote_option(module);
tokens.append_all(quote! {
crate::stub::PyClassInfo {
::pyo3_stub_gen::type_info::PyClassInfo {
pyclass_name: #pyclass_name,
struct_id: std::any::TypeId::of::<#struct_type>,
members: &[ #( #members),* ],
Expand Down Expand Up @@ -101,19 +101,19 @@ mod test {
)?;
let out = PyClassInfo::try_from(input)?.to_token_stream();
insta::assert_snapshot!(format_as_value(out), @r###"
crate::stub::PyClassInfo {
::pyo3_stub_gen::type_info::PyClassInfo {
pyclass_name: "Placeholder",
struct_id: std::any::TypeId::of::<PyPlaceholder>,
members: &[
crate::stub::MemberInfo {
::pyo3_stub_gen::type_info::MemberInfo {
name: "name",
r#type: <String as IntoPy<PyObject>>::type_output,
},
crate::stub::MemberInfo {
::pyo3_stub_gen::type_info::MemberInfo {
name: "ndim",
r#type: <usize as IntoPy<PyObject>>::type_output,
},
crate::stub::MemberInfo {
::pyo3_stub_gen::type_info::MemberInfo {
name: "description",
r#type: <Option<String> as IntoPy<PyObject>>::type_output,
},
Expand Down
2 changes: 1 addition & 1 deletion pyo3-stub-gen-derive/src/gen_stub/pyclass_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ToTokens for PyEnumInfo {
} = self;
let module = quote_option(module);
tokens.append_all(quote! {
crate::stub::PyEnumInfo {
::pyo3_stub_gen::type_info::PyEnumInfo {
pyclass_name: #pyclass_name,
enum_id: std::any::TypeId::of::<#enum_type>,
variants: &[ #(#variants),* ],
Expand Down
4 changes: 2 additions & 2 deletions pyo3-stub-gen-derive/src/gen_stub/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ impl ToTokens for PyFunctionInfo {
let ret_tt = if let Some(ret) = ret {
quote! { <#ret as IntoPy<::pyo3::PyObject>>::type_output }
} else {
quote! { crate::stub::no_return_type_output }
quote! { ::pyo3_stub_gen::type_info::no_return_type_output }
};
let sig_tt = quote_option(sig);
let module_tt = quote_option(module);
tokens.append_all(quote! {
crate::stub::PyFunctionInfo {
::pyo3_stub_gen::type_info::PyFunctionInfo {
name: #name,
args: &[ #(#args),* ],
r#return: #ret_tt,
Expand Down
2 changes: 1 addition & 1 deletion pyo3-stub-gen-derive/src/gen_stub/pymethods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ToTokens for PyMethodsInfo {
} = self;
let new_tt = quote_option(new);
tokens.append_all(quote! {
crate::stub::PyMethodsInfo {
::pyo3_stub_gen::type_info::PyMethodsInfo {
struct_id: std::any::TypeId::of::<#struct_id>,
new: #new_tt,
getters: &[ #(#getters),* ],
Expand Down
20 changes: 20 additions & 0 deletions pyo3-stub-gen-testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "pyo3-stub-gen-testing"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
inventory.workspace = true
pyo3-stub-gen = { workspace = true, optional = true }
pyo3.workspace = true

[features]
stub_gen = ["pyo3-stub-gen"]

[[bin]]
name = "stub_gen"
path = "src/bin/stub_gen.rs"
required-features = ["stub_gen"]
10 changes: 10 additions & 0 deletions pyo3-stub-gen-testing/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"
requires-python = ">=3.9"

[project.optional-dependencies]
test = ["pytest", "pyright"]
6 changes: 6 additions & 0 deletions pyo3-stub-gen-testing/src/bin/stub_gen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use pyo3_stub_gen::*;

fn main() {
let modules = generate::gather().unwrap();
dbg!(modules);
}
31 changes: 31 additions & 0 deletions pyo3-stub-gen-testing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use pyo3::prelude::*;

#[cfg(feature = "stub_gen")]
use pyo3_stub_gen::derive::*;

/// Returns the sum of two numbers as a string.
///
/// Test of running doc-test
///
/// ```rust
/// assert_eq!(2 + 2, 4);
/// ```
#[cfg_attr(feature = "stub_gen", gen_stub_pyfunction)]
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

#[pymodule]
fn pyo3_stub_gen_testing(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}

#[cfg(test)]
mod test {
#[test]
fn test() {
assert_eq!(2 + 2, 4);
}
}
5 changes: 5 additions & 0 deletions pyo3-stub-gen-testing/tests/test_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pyo3_stub_gen_testing


def test_sum_as_string():
assert pyo3_stub_gen_testing.sum_as_string(1, 2) == "3"
2 changes: 1 addition & 1 deletion pyo3-stub-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ pyo3-stub-gen-derive.workspace = true
anyhow.workspace = true
itertools.workspace = true
inventory.workspace = true
pyo3 = { workspace = true, features = ["experimental-inspect"]}
pyo3.workspace = true
4 changes: 2 additions & 2 deletions pyo3-stub-gen/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ impl fmt::Display for FunctionDef {
}

#[derive(Debug, Clone, PartialEq, Default)]
struct Module {
pub struct Module {
class: BTreeMap<TypeId, ClassDef>,
enum_: BTreeMap<TypeId, EnumDef>,
function: BTreeMap<&'static str, FunctionDef>,
Expand All @@ -295,7 +295,7 @@ impl fmt::Display for Module {
}

/// Gather metadata generated by proc-macros
fn gather() -> Result<BTreeMap<String, Module>> {
pub fn gather() -> Result<BTreeMap<String, Module>> {
let mut modules: BTreeMap<String, Module> = BTreeMap::new();

for info in inventory::iter::<PyClassInfo> {
Expand Down

0 comments on commit 9269b47

Please sign in to comment.