Skip to content

Commit 21b83cf

Browse files
Fix handling of nested types (#20)
Nested type (such as in Unions) were not being processed. This is a better implementation of #18 from sizmailov#263
1 parent 6eae4b2 commit 21b83cf

File tree

10 files changed

+21
-1
lines changed
  • pybind11_stubgen/parser/mixins
  • tests
    • py-demo/bindings/src/modules
    • stubs
      • python-3.12
        • pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings
        • pybind11-v2.12/numpy-array-wrap-with-annotated/demo/_bindings
        • pybind11-v2.13
        • pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings
        • pybind11-v3.0
      • python-3.9/pybind11-v2.13/numpy-array-wrap-with-annotated/demo/_bindings

10 files changed

+21
-1
lines changed

pybind11_stubgen/parser/mixins/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ def parse_annotation_str(
588588
assert isinstance(union_t, ResolvedType)
589589
return ResolvedType(
590590
name=union_t.name,
591-
parameters=[self.parse_type_str(variant) for variant in variants],
591+
parameters=[self.parse_annotation_str(variant) for variant in variants],
592592
)
593593

594594
def parse_type_str(

tests/py-demo/bindings/src/modules/functions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <pybind11/stl.h>
1010
#include <pybind11/functional.h>
1111

12+
#include <variant>
13+
#include <list>
14+
1215
#include <demo/sublibA/add.h>
1316

1417
namespace {
@@ -97,6 +100,7 @@ void bind_functions_module(py::module &&m) {
97100
pyFoo.def(py::init<int>());
98101
m.def("default_custom_arg", [](Foo &foo) {}, py::arg_v("foo", Foo(5), "Foo(5)"));
99102
m.def("pass_callback", [](std::function<Foo(Foo &)> &callback) { return Foo(13); });
103+
m.def("nested_types", [](std::variant<std::list<Foo>, Foo> arg){ return arg; });
100104

101105
#if PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 12)
102106
py::options options;

tests/stubs/python-3.12/pybind11-v2.11/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ __all__: list[str] = [
1818
"func_w_named_pos_args",
1919
"generic",
2020
"mul",
21+
"nested_types",
2122
"pass_callback",
2223
"passthrough1",
2324
"passthrough2",
@@ -54,6 +55,7 @@ def mul(p: float, q: float) -> float:
5455
Multiply p and q (double)
5556
"""
5657

58+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5759
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
5860
def passthrough1[T](obj: T) -> T: ...
5961
@typing.overload

tests/stubs/python-3.12/pybind11-v2.12/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ __all__: list[str] = [
1919
"func_w_named_pos_args",
2020
"generic",
2121
"mul",
22+
"nested_types",
2223
"pass_callback",
2324
"passthrough1",
2425
"passthrough2",
@@ -56,6 +57,7 @@ def mul(p: float, q: float) -> float:
5657
Multiply p and q (double)
5758
"""
5859

60+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5961
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
6062
def passthrough1[T](obj: T) -> T: ...
6163
@typing.overload

tests/stubs/python-3.12/pybind11-v2.13/numpy-array-use-type-var/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ __all__: list[str] = [
1919
"func_w_named_pos_args",
2020
"generic",
2121
"mul",
22+
"nested_types",
2223
"pass_callback",
2324
"passthrough1",
2425
"passthrough2",
@@ -56,6 +57,7 @@ def mul(p: float, q: float) -> float:
5657
Multiply p and q (double)
5758
"""
5859

60+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5961
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
6062
def passthrough1[T](obj: T) -> T: ...
6163
@typing.overload

tests/stubs/python-3.12/pybind11-v2.13/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ __all__: list[str] = [
1919
"func_w_named_pos_args",
2020
"generic",
2121
"mul",
22+
"nested_types",
2223
"pass_callback",
2324
"passthrough1",
2425
"passthrough2",
@@ -56,6 +57,7 @@ def mul(p: float, q: float) -> float:
5657
Multiply p and q (double)
5758
"""
5859

60+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5961
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
6062
def passthrough1[T](obj: T) -> T: ...
6163
@typing.overload

tests/stubs/python-3.12/pybind11-v2.9/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ __all__: list[str] = [
1717
"func_w_named_pos_args",
1818
"generic",
1919
"mul",
20+
"nested_types",
2021
"pass_callback",
2122
"passthrough1",
2223
"passthrough2",
@@ -52,6 +53,7 @@ def mul(p: float, q: float) -> float:
5253
Multiply p and q (double)
5354
"""
5455

56+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5557
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
5658
def passthrough1[T](obj: T) -> T: ...
5759
@typing.overload

tests/stubs/python-3.12/pybind11-v3.0/numpy-array-use-type-var/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __all__: list[str] = [
2020
"func_w_named_pos_args",
2121
"generic",
2222
"mul",
23+
"nested_types",
2324
"pass_callback",
2425
"passthrough1",
2526
"passthrough2",
@@ -63,6 +64,7 @@ def mul(p: typing.SupportsFloat, q: typing.SupportsFloat) -> float:
6364
Multiply p and q (double)
6465
"""
6566

67+
def nested_types(arg0: collections.abc.Sequence[Foo] | Foo) -> list[Foo] | Foo: ...
6668
def pass_callback(arg0: collections.abc.Callable[[Foo], Foo]) -> Foo: ...
6769
def passthrough1[T](obj: T) -> T: ...
6870
@typing.overload

tests/stubs/python-3.12/pybind11-v3.0/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ __all__: list[str] = [
2020
"func_w_named_pos_args",
2121
"generic",
2222
"mul",
23+
"nested_types",
2324
"pass_callback",
2425
"passthrough1",
2526
"passthrough2",
@@ -63,6 +64,7 @@ def mul(p: typing.SupportsFloat, q: typing.SupportsFloat) -> float:
6364
Multiply p and q (double)
6465
"""
6566

67+
def nested_types(arg0: collections.abc.Sequence[Foo] | Foo) -> list[Foo] | Foo: ...
6668
def pass_callback(arg0: collections.abc.Callable[[Foo], Foo]) -> Foo: ...
6769
def passthrough1[T](obj: T) -> T: ...
6870
@typing.overload

tests/stubs/python-3.9/pybind11-v2.13/numpy-array-wrap-with-annotated/demo/_bindings/functions.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ __all__: list[str] = [
1919
"func_w_named_pos_args",
2020
"generic",
2121
"mul",
22+
"nested_types",
2223
"pass_callback",
2324
"pos_kw_only_mix",
2425
"pos_kw_only_variadic_mix",
@@ -53,6 +54,7 @@ def mul(p: float, q: float) -> float:
5354
Multiply p and q (double)
5455
"""
5556

57+
def nested_types(arg0: list[Foo] | Foo) -> list[Foo] | Foo: ...
5658
def pass_callback(arg0: typing.Callable[[Foo], Foo]) -> Foo: ...
5759
def pos_kw_only_mix(i: int, /, j: int, *, k: int) -> tuple: ...
5860
def pos_kw_only_variadic_mix(i: int, /, j: int, *args, k: int, **kwargs) -> tuple: ...

0 commit comments

Comments
 (0)