Skip to content

Commit 494a072

Browse files
committed
improve stubgen tests
1 parent fbb738a commit 494a072

File tree

12 files changed

+452
-17
lines changed

12 files changed

+452
-17
lines changed

misc/test-stubgenc.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,24 @@ function stubgenc_test() {
2121
rm -rf "${STUBGEN_OUTPUT_FOLDER:?}/*"
2222
stubgen -o "$STUBGEN_OUTPUT_FOLDER" "${@:2}"
2323

24+
# Check if generated stubs can actually be type checked by mypy
25+
if ! mypy "$STUBGEN_OUTPUT_FOLDER";
26+
then
27+
echo "Stubgen test failed, because generated stubs failed to type check."
28+
EXIT=1
29+
fi
30+
2431
# Compare generated stubs to expected ones
2532
if ! git diff --exit-code "$STUBGEN_OUTPUT_FOLDER";
2633
then
34+
echo "Stubgen test failed, because generated stubs differ from expected outputs."
2735
EXIT=1
2836
fi
2937
}
3038

3139
# create stubs without docstrings
32-
stubgenc_test stubgen -p pybind11_mypy_demo
40+
stubgenc_test expected_stubs_no_docs -p pybind11_mypy_demo
3341
# create stubs with docstrings
34-
stubgenc_test stubgen-include-docs -p pybind11_mypy_demo --include-docstrings
42+
stubgenc_test expected_stubs_with_docs -p pybind11_mypy_demo --include-docstrings
43+
3544
exit $EXIT
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
from . import demo as demo
3+
from typing import List, Optional, Tuple
4+
5+
class TestStruct:
6+
field_readwrite: int
7+
field_readwrite_docstring: int
8+
def __init__(self, *args, **kwargs) -> None: ...
9+
@property
10+
def field_readonly(self) -> int: ...
11+
12+
def func_incomplete_signature(*args, **kwargs): ...
13+
def func_returning_optional() -> Optional[int]: ...
14+
def func_returning_pair() -> Tuple[int, float]: ...
15+
def func_returning_path() -> os.PathLike: ...
16+
def func_returning_vector() -> List[float]: ...
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import ClassVar, List, overload
2+
3+
PI: float
4+
__version__: str
5+
6+
class Point:
7+
class AngleUnit:
8+
__members__: ClassVar[dict] = ... # read-only
9+
__entries: ClassVar[dict] = ...
10+
degree: ClassVar[Point.AngleUnit] = ...
11+
radian: ClassVar[Point.AngleUnit] = ...
12+
def __init__(self, value: int) -> None: ...
13+
def __eq__(self, other: object) -> bool: ...
14+
def __hash__(self) -> int: ...
15+
def __index__(self) -> int: ...
16+
def __int__(self) -> int: ...
17+
def __ne__(self, other: object) -> bool: ...
18+
@property
19+
def name(self) -> str: ...
20+
@property
21+
def value(self) -> int: ...
22+
23+
class LengthUnit:
24+
__members__: ClassVar[dict] = ... # read-only
25+
__entries: ClassVar[dict] = ...
26+
inch: ClassVar[Point.LengthUnit] = ...
27+
mm: ClassVar[Point.LengthUnit] = ...
28+
pixel: ClassVar[Point.LengthUnit] = ...
29+
def __init__(self, value: int) -> None: ...
30+
def __eq__(self, other: object) -> bool: ...
31+
def __hash__(self) -> int: ...
32+
def __index__(self) -> int: ...
33+
def __int__(self) -> int: ...
34+
def __ne__(self, other: object) -> bool: ...
35+
@property
36+
def name(self) -> str: ...
37+
@property
38+
def value(self) -> int: ...
39+
angle_unit: ClassVar[Point.AngleUnit] = ...
40+
length_unit: ClassVar[Point.LengthUnit] = ...
41+
x_axis: ClassVar[Point] = ... # read-only
42+
y_axis: ClassVar[Point] = ... # read-only
43+
origin: ClassVar[Point] = ...
44+
x: float
45+
y: float
46+
@overload
47+
def __init__(self) -> None: ...
48+
@overload
49+
def __init__(self, x: float, y: float) -> None: ...
50+
def as_list(self) -> List[float]: ...
51+
@overload
52+
def distance_to(self, x: float, y: float) -> float: ...
53+
@overload
54+
def distance_to(self, other: Point) -> float: ...
55+
@property
56+
def length(self) -> float: ...
57+
58+
def answer() -> int: ...
59+
def midpoint(left: float, right: float) -> float: ...
60+
def sum(arg0: int, arg1: int) -> int: ...
61+
def weighted_midpoint(left: float, right: float, alpha: float = ...) -> float: ...
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
from . import demo as demo
3+
from typing import List, Optional, Tuple
4+
5+
class TestStruct:
6+
field_readwrite: int
7+
field_readwrite_docstring: int
8+
def __init__(self, *args, **kwargs) -> None:
9+
"""Initialize self. See help(type(self)) for accurate signature."""
10+
@property
11+
def field_readonly(self) -> int: ...
12+
13+
def func_incomplete_signature(*args, **kwargs):
14+
"""func_incomplete_signature() -> dummy_sub_namespace::HasNoBinding"""
15+
def func_returning_optional() -> Optional[int]:
16+
"""func_returning_optional() -> Optional[int]"""
17+
def func_returning_pair() -> Tuple[int, float]:
18+
"""func_returning_pair() -> Tuple[int, float]"""
19+
def func_returning_path() -> os.PathLike:
20+
"""func_returning_path() -> os.PathLike"""
21+
def func_returning_vector() -> List[float]:
22+
"""func_returning_vector() -> List[float]"""
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from typing import ClassVar, List, overload
2+
3+
PI: float
4+
__version__: str
5+
6+
class Point:
7+
class AngleUnit:
8+
__members__: ClassVar[dict] = ... # read-only
9+
__entries: ClassVar[dict] = ...
10+
degree: ClassVar[Point.AngleUnit] = ...
11+
radian: ClassVar[Point.AngleUnit] = ...
12+
def __init__(self, value: int) -> None:
13+
"""__init__(self: pybind11_mypy_demo.demo.Point.AngleUnit, value: int) -> None"""
14+
def __eq__(self, other: object) -> bool:
15+
"""__eq__(self: object, other: object) -> bool"""
16+
def __hash__(self) -> int:
17+
"""__hash__(self: object) -> int"""
18+
def __index__(self) -> int:
19+
"""__index__(self: pybind11_mypy_demo.demo.Point.AngleUnit) -> int"""
20+
def __int__(self) -> int:
21+
"""__int__(self: pybind11_mypy_demo.demo.Point.AngleUnit) -> int"""
22+
def __ne__(self, other: object) -> bool:
23+
"""__ne__(self: object, other: object) -> bool"""
24+
@property
25+
def name(self) -> str: ...
26+
@property
27+
def value(self) -> int: ...
28+
29+
class LengthUnit:
30+
__members__: ClassVar[dict] = ... # read-only
31+
__entries: ClassVar[dict] = ...
32+
inch: ClassVar[Point.LengthUnit] = ...
33+
mm: ClassVar[Point.LengthUnit] = ...
34+
pixel: ClassVar[Point.LengthUnit] = ...
35+
def __init__(self, value: int) -> None:
36+
"""__init__(self: pybind11_mypy_demo.demo.Point.LengthUnit, value: int) -> None"""
37+
def __eq__(self, other: object) -> bool:
38+
"""__eq__(self: object, other: object) -> bool"""
39+
def __hash__(self) -> int:
40+
"""__hash__(self: object) -> int"""
41+
def __index__(self) -> int:
42+
"""__index__(self: pybind11_mypy_demo.demo.Point.LengthUnit) -> int"""
43+
def __int__(self) -> int:
44+
"""__int__(self: pybind11_mypy_demo.demo.Point.LengthUnit) -> int"""
45+
def __ne__(self, other: object) -> bool:
46+
"""__ne__(self: object, other: object) -> bool"""
47+
@property
48+
def name(self) -> str: ...
49+
@property
50+
def value(self) -> int: ...
51+
angle_unit: ClassVar[Point.AngleUnit] = ...
52+
length_unit: ClassVar[Point.LengthUnit] = ...
53+
x_axis: ClassVar[Point] = ... # read-only
54+
y_axis: ClassVar[Point] = ... # read-only
55+
origin: ClassVar[Point] = ...
56+
x: float
57+
y: float
58+
@overload
59+
def __init__(self) -> None:
60+
"""__init__(*args, **kwargs)
61+
Overloaded function.
62+
63+
1. __init__(self: pybind11_mypy_demo.demo.Point) -> None
64+
65+
2. __init__(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> None
66+
"""
67+
@overload
68+
def __init__(self, x: float, y: float) -> None:
69+
"""__init__(*args, **kwargs)
70+
Overloaded function.
71+
72+
1. __init__(self: pybind11_mypy_demo.demo.Point) -> None
73+
74+
2. __init__(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> None
75+
"""
76+
def as_list(self) -> List[float]:
77+
"""as_list(self: pybind11_mypy_demo.demo.Point) -> List[float]"""
78+
@overload
79+
def distance_to(self, x: float, y: float) -> float:
80+
"""distance_to(*args, **kwargs)
81+
Overloaded function.
82+
83+
1. distance_to(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> float
84+
85+
2. distance_to(self: pybind11_mypy_demo.demo.Point, other: pybind11_mypy_demo.demo.Point) -> float
86+
"""
87+
@overload
88+
def distance_to(self, other: Point) -> float:
89+
"""distance_to(*args, **kwargs)
90+
Overloaded function.
91+
92+
1. distance_to(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> float
93+
94+
2. distance_to(self: pybind11_mypy_demo.demo.Point, other: pybind11_mypy_demo.demo.Point) -> float
95+
"""
96+
@property
97+
def length(self) -> float: ...
98+
99+
def answer() -> int:
100+
'''answer() -> int
101+
102+
answer docstring, with end quote"
103+
'''
104+
def midpoint(left: float, right: float) -> float:
105+
"""midpoint(left: float, right: float) -> float"""
106+
def sum(arg0: int, arg1: int) -> int:
107+
'''sum(arg0: int, arg1: int) -> int
108+
109+
multiline docstring test, edge case quotes """\'\'\'
110+
'''
111+
def weighted_midpoint(left: float, right: float, alpha: float = ...) -> float:
112+
"""weighted_midpoint(left: float, right: float, alpha: float = 0.5) -> float"""
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
from . import demo as demo
3+
from typing import List, Optional, Tuple
4+
5+
class TestStruct:
6+
field_readwrite: int
7+
field_readwrite_docstring: int
8+
def __init__(self, *args, **kwargs) -> None:
9+
"""Initialize self. See help(type(self)) for accurate signature."""
10+
@property
11+
def field_readonly(self) -> int: ...
12+
13+
def func_incomplete_signature(*args, **kwargs):
14+
"""func_incomplete_signature() -> dummy_sub_namespace::HasNoBinding"""
15+
def func_returning_optional() -> Optional[int]:
16+
"""func_returning_optional() -> Optional[int]"""
17+
def func_returning_pair() -> Tuple[int, float]:
18+
"""func_returning_pair() -> Tuple[int, float]"""
19+
def func_returning_path() -> os.PathLike:
20+
"""func_returning_path() -> os.PathLike"""
21+
def func_returning_vector() -> List[float]:
22+
"""func_returning_vector() -> List[float]"""
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from typing import ClassVar, List, overload
2+
3+
PI: float
4+
__version__: str
5+
6+
class Point:
7+
class AngleUnit:
8+
__members__: ClassVar[dict] = ... # read-only
9+
__entries: ClassVar[dict] = ...
10+
degree: ClassVar[Point.AngleUnit] = ...
11+
radian: ClassVar[Point.AngleUnit] = ...
12+
def __init__(self, value: int) -> None:
13+
"""__init__(self: pybind11_mypy_demo.demo.Point.AngleUnit, value: int) -> None"""
14+
def __eq__(self, other: object) -> bool:
15+
"""__eq__(self: object, other: object) -> bool"""
16+
def __hash__(self) -> int:
17+
"""__hash__(self: object) -> int"""
18+
def __index__(self) -> int:
19+
"""__index__(self: pybind11_mypy_demo.demo.Point.AngleUnit) -> int"""
20+
def __int__(self) -> int:
21+
"""__int__(self: pybind11_mypy_demo.demo.Point.AngleUnit) -> int"""
22+
def __ne__(self, other: object) -> bool:
23+
"""__ne__(self: object, other: object) -> bool"""
24+
@property
25+
def name(self) -> str: ...
26+
@property
27+
def value(self) -> int: ...
28+
29+
class LengthUnit:
30+
__members__: ClassVar[dict] = ... # read-only
31+
__entries: ClassVar[dict] = ...
32+
inch: ClassVar[Point.LengthUnit] = ...
33+
mm: ClassVar[Point.LengthUnit] = ...
34+
pixel: ClassVar[Point.LengthUnit] = ...
35+
def __init__(self, value: int) -> None:
36+
"""__init__(self: pybind11_mypy_demo.demo.Point.LengthUnit, value: int) -> None"""
37+
def __eq__(self, other: object) -> bool:
38+
"""__eq__(self: object, other: object) -> bool"""
39+
def __hash__(self) -> int:
40+
"""__hash__(self: object) -> int"""
41+
def __index__(self) -> int:
42+
"""__index__(self: pybind11_mypy_demo.demo.Point.LengthUnit) -> int"""
43+
def __int__(self) -> int:
44+
"""__int__(self: pybind11_mypy_demo.demo.Point.LengthUnit) -> int"""
45+
def __ne__(self, other: object) -> bool:
46+
"""__ne__(self: object, other: object) -> bool"""
47+
@property
48+
def name(self) -> str: ...
49+
@property
50+
def value(self) -> int: ...
51+
angle_unit: ClassVar[Point.AngleUnit] = ...
52+
length_unit: ClassVar[Point.LengthUnit] = ...
53+
x_axis: ClassVar[Point] = ... # read-only
54+
y_axis: ClassVar[Point] = ... # read-only
55+
origin: ClassVar[Point] = ...
56+
x: float
57+
y: float
58+
@overload
59+
def __init__(self) -> None:
60+
"""__init__(*args, **kwargs)
61+
Overloaded function.
62+
63+
1. __init__(self: pybind11_mypy_demo.demo.Point) -> None
64+
65+
2. __init__(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> None
66+
"""
67+
@overload
68+
def __init__(self, x: float, y: float) -> None:
69+
"""__init__(*args, **kwargs)
70+
Overloaded function.
71+
72+
1. __init__(self: pybind11_mypy_demo.demo.Point) -> None
73+
74+
2. __init__(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> None
75+
"""
76+
def as_list(self) -> List[float]:
77+
"""as_list(self: pybind11_mypy_demo.demo.Point) -> List[float]"""
78+
@overload
79+
def distance_to(self, x: float, y: float) -> float:
80+
"""distance_to(*args, **kwargs)
81+
Overloaded function.
82+
83+
1. distance_to(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> float
84+
85+
2. distance_to(self: pybind11_mypy_demo.demo.Point, other: pybind11_mypy_demo.demo.Point) -> float
86+
"""
87+
@overload
88+
def distance_to(self, other: Point) -> float:
89+
"""distance_to(*args, **kwargs)
90+
Overloaded function.
91+
92+
1. distance_to(self: pybind11_mypy_demo.demo.Point, x: float, y: float) -> float
93+
94+
2. distance_to(self: pybind11_mypy_demo.demo.Point, other: pybind11_mypy_demo.demo.Point) -> float
95+
"""
96+
@property
97+
def length(self) -> float: ...
98+
99+
def answer() -> int:
100+
'''answer() -> int
101+
102+
answer docstring, with end quote"
103+
'''
104+
def midpoint(left: float, right: float) -> float:
105+
"""midpoint(left: float, right: float) -> float"""
106+
def sum(arg0: int, arg1: int) -> int:
107+
'''sum(arg0: int, arg1: int) -> int
108+
109+
multiline docstring test, edge case quotes """\'\'\'
110+
'''
111+
def weighted_midpoint(left: float, right: float, alpha: float = ...) -> float:
112+
"""weighted_midpoint(left: float, right: float, alpha: float = 0.5) -> float"""

0 commit comments

Comments
 (0)