Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ license = "MIT OR Apache-2.0"
readme = "README.md"

[workspace.dependencies]
either = "1.15.0"
ahash = "0.8.11"
anyhow = "1.0.98"
chrono = "0.4.40"
Expand Down
1 change: 1 addition & 0 deletions examples/mixed/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/mixed_sub/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/mixed_sub_multiple/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/pure/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyo3-stub-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ log.workspace = true
maplit.workspace = true
num-complex.workspace = true
numpy = { workspace = true, optional = true }
either = { workspace = true, optional = true }
pyo3.workspace = true
serde.workspace = true
toml.workspace = true
Expand All @@ -30,5 +31,6 @@ path = "../pyo3-stub-gen-derive"
test-case.workspace = true

[features]
default = ["numpy"]
default = ["numpy", "either"]
numpy = ["dep:numpy"]
either = ["dep:either"]
3 changes: 3 additions & 0 deletions pyo3-stub-gen/src/stub_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ mod pyo3;
#[cfg(feature = "numpy")]
mod numpy;

#[cfg(feature = "either")]
mod either;

use maplit::hashset;
use std::{collections::HashSet, fmt, ops};

Expand Down
44 changes: 44 additions & 0 deletions pyo3-stub-gen/src/stub_type/either.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::collections::HashSet;

use super::{ModuleRef, PyStubType, TypeInfo};

impl<L: PyStubType, R: PyStubType> PyStubType for either::Either<L, R> {
fn type_input() -> TypeInfo {
let TypeInfo {
name: name_l,
import: import_l,
} = L::type_input();
let TypeInfo {
name: name_r,
import: import_r,
} = R::type_input();

let mut import: HashSet<ModuleRef> = import_l.into_iter().chain(import_r).collect();

import.insert("typing".into());

TypeInfo {
name: format!("typing.Union[{name_l}, {name_r}]"),
import,
}
}
fn type_output() -> TypeInfo {
let TypeInfo {
name: name_l,
import: import_l,
} = L::type_output();
let TypeInfo {
name: name_r,
import: import_r,
} = R::type_output();

let mut import: HashSet<ModuleRef> = import_l.into_iter().chain(import_r).collect();

import.insert("typing".into());

TypeInfo {
name: format!("typing.Union[{name_l}, {name_r}]"),
import,
}
}
}
6 changes: 3 additions & 3 deletions pyo3-stub-gen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ mod test {
pyo3::prepare_freethreaded_python();
Python::with_gil(|py| {
// str
assert_eq!("'123'", fmt_py_obj(py, &"123"));
assert_eq!("\"don't\"", fmt_py_obj(py, &"don't"));
assert_eq!("'str\\\\'", fmt_py_obj(py, &"str\\"));
assert_eq!("'123'", fmt_py_obj(py, "123"));
assert_eq!("\"don't\"", fmt_py_obj(py, "don't"));
assert_eq!("'str\\\\'", fmt_py_obj(py, "str\\"));
// bool
assert_eq!("True", fmt_py_obj(py, true));
assert_eq!("False", fmt_py_obj(py, false));
Expand Down
Loading