Skip to content

Commit 0709cfb

Browse files
Implement PyStubType for either::Either (with minor revise) (#212)
Minor revise for #207 Summary by Copilot ------------------- This pull request introduces support for the `either` crate in the `pyo3-stub-gen` project, enabling the use of `either::Either` types in Python stub generation. It also includes minor fixes to existing test cases for improved clarity and correctness. ### Integration of the `either` crate: * [`Cargo.toml`](diffhunk://#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R22): Added `either` as a dependency in the workspace and included it as an optional feature in `pyo3-stub-gen`. The default features now include `either`. [[1]](diffhunk://#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R22) [[2]](diffhunk://#diff-d2fe0506288d6d1bb990e8185c26e0cdf07ee85787eb78c2bb3d60b70bf88eb0R21) [[3]](diffhunk://#diff-d2fe0506288d6d1bb990e8185c26e0cdf07ee85787eb78c2bb3d60b70bf88eb0L33-R36) * [`pyo3-stub-gen/src/stub_type.rs`](diffhunk://#diff-bec31605b52ecbf7a424c6bcc8dfcf155d211d25316e27ba0945b49284470ae8R8-R10): Added a new module `either` gated behind the `either` feature flag. * [`pyo3-stub-gen/src/stub_type/either.rs`](diffhunk://#diff-5d12c671f041e17061b398055bb9cb24b0360e7af721e1985f863a49b0e446dbR1-R44): Implemented support for `either::Either<L, R>` in stub generation by defining `type_input` and `type_output` methods that map `Either` to Python's `typing.Union`. ### Test case fixes: * [`pyo3-stub-gen/src/util.rs`](diffhunk://#diff-1c01740ca518ff53dff74391ba7e3427fba3221cc3c19de143bcf2568f253e87L114-R116): Updated test cases to pass string literals directly instead of references, improving readability and correctness. --------- Co-authored-by: YuantianDing <[email protected]>
1 parent 10808fa commit 0709cfb

File tree

10 files changed

+59
-4
lines changed

10 files changed

+59
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ license = "MIT OR Apache-2.0"
1919
readme = "README.md"
2020

2121
[workspace.dependencies]
22+
either = "1.15.0"
2223
ahash = "0.8.11"
2324
anyhow = "1.0.98"
2425
chrono = "0.4.40"

examples/mixed/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/mixed_sub/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/mixed_sub_multiple/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/pure/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyo3-stub-gen/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ log.workspace = true
1818
maplit.workspace = true
1919
num-complex.workspace = true
2020
numpy = { workspace = true, optional = true }
21+
either = { workspace = true, optional = true }
2122
pyo3.workspace = true
2223
serde.workspace = true
2324
toml.workspace = true
@@ -30,5 +31,6 @@ path = "../pyo3-stub-gen-derive"
3031
test-case.workspace = true
3132

3233
[features]
33-
default = ["numpy"]
34+
default = ["numpy", "either"]
3435
numpy = ["dep:numpy"]
36+
either = ["dep:either"]

pyo3-stub-gen/src/stub_type.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ mod pyo3;
55
#[cfg(feature = "numpy")]
66
mod numpy;
77

8+
#[cfg(feature = "either")]
9+
mod either;
10+
811
use maplit::hashset;
912
use std::{collections::HashSet, fmt, ops};
1013

pyo3-stub-gen/src/stub_type/either.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::collections::HashSet;
2+
3+
use super::{ModuleRef, PyStubType, TypeInfo};
4+
5+
impl<L: PyStubType, R: PyStubType> PyStubType for either::Either<L, R> {
6+
fn type_input() -> TypeInfo {
7+
let TypeInfo {
8+
name: name_l,
9+
import: import_l,
10+
} = L::type_input();
11+
let TypeInfo {
12+
name: name_r,
13+
import: import_r,
14+
} = R::type_input();
15+
16+
let mut import: HashSet<ModuleRef> = import_l.into_iter().chain(import_r).collect();
17+
18+
import.insert("typing".into());
19+
20+
TypeInfo {
21+
name: format!("typing.Union[{name_l}, {name_r}]"),
22+
import,
23+
}
24+
}
25+
fn type_output() -> TypeInfo {
26+
let TypeInfo {
27+
name: name_l,
28+
import: import_l,
29+
} = L::type_output();
30+
let TypeInfo {
31+
name: name_r,
32+
import: import_r,
33+
} = R::type_output();
34+
35+
let mut import: HashSet<ModuleRef> = import_l.into_iter().chain(import_r).collect();
36+
37+
import.insert("typing".into());
38+
39+
TypeInfo {
40+
name: format!("typing.Union[{name_l}, {name_r}]"),
41+
import,
42+
}
43+
}
44+
}

pyo3-stub-gen/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ mod test {
111111
pyo3::prepare_freethreaded_python();
112112
Python::with_gil(|py| {
113113
// str
114-
assert_eq!("'123'", fmt_py_obj(py, &"123"));
115-
assert_eq!("\"don't\"", fmt_py_obj(py, &"don't"));
116-
assert_eq!("'str\\\\'", fmt_py_obj(py, &"str\\"));
114+
assert_eq!("'123'", fmt_py_obj(py, "123"));
115+
assert_eq!("\"don't\"", fmt_py_obj(py, "don't"));
116+
assert_eq!("'str\\\\'", fmt_py_obj(py, "str\\"));
117117
// bool
118118
assert_eq!("True", fmt_py_obj(py, true));
119119
assert_eq!("False", fmt_py_obj(py, false));

0 commit comments

Comments
 (0)