Skip to content

Commit 015b1db

Browse files
initial implementation
1 parent 956e0ae commit 015b1db

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

python/cppyy/_cpython_cppyy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'load_reflection_info',
1010
'addressof',
1111
'bind_object',
12+
'generate_typehints',
1213
'nullptr',
1314
'default',
1415
'_backend',
@@ -188,6 +189,7 @@ def add_default_paths():
188189
bind_object = _backend.bind_object
189190
nullptr = _backend.nullptr
190191
default = _backend.default
192+
generate_typehints = _backend.generate_typehints
191193

192194
def load_reflection_info(name):
193195
# with _stderr_capture() as err:

python/cppyy/_pypy_cppyy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'gbl',
1111
'addressof',
1212
'bind_object',
13+
'generate_typehints',
1314
'nullptr',
1415
]
1516

test/test_typehints.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from pytest import raises
2+
3+
4+
class TestTypeHints:
5+
def setup_class(cls):
6+
import cppyy
7+
8+
cppyy.cppdef(
9+
r"""
10+
namespace TypeHints {
11+
int x = 10;
12+
template <typename T>
13+
struct MyTKlass{
14+
T obj;
15+
};
16+
struct MyKlass {
17+
std::string name = "MyKlass";
18+
bool flag = false;
19+
std::vector<int> array = {};
20+
float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; }
21+
template <typename T>
22+
T callme(T v) { return v; }
23+
};
24+
typedef MyKlass Klass;
25+
typedef MyTKlass<float> KlassFloat;
26+
float fn(std::string a, bool b, std::vector<int> c) { return 0.0f; }
27+
template <typename T>
28+
T tmpl_fn(T v) { return v; }
29+
} // namespace TypeHints
30+
int x = 10;
31+
double y = 10;
32+
unsigned short z = 10;
33+
char a = 'a';
34+
signed char sa = 'a';
35+
unsigned char usa = 'a';
36+
template <typename T>
37+
struct MyTKlass{
38+
T obj;
39+
};
40+
struct MyKlass {
41+
std::string name = "MyKlass";
42+
bool flag = false;
43+
std::vector<int> array = {};
44+
float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; }
45+
template <typename T>
46+
T callme(T v) { return v; }
47+
};
48+
float callme(std::string a, bool b, std::vector<int> c) { return 0.0f; }
49+
template <typename T>
50+
T tmpl_fn(T v) { return v; }
51+
typedef MyKlass Klass;
52+
typedef MyTKlass<float> KlassFloat;
53+
"""
54+
)
55+
56+
def test_invalids(self):
57+
from cppyy import gbl, generate_typehints
58+
59+
typehint = generate_typehints("x")
60+
assert "x: int\n" == typehint
61+
typehint = generate_typehints("y")
62+
assert "y: float\n" == typehint
63+
typehint = generate_typehints("z")
64+
assert "z: int\n" == typehint
65+
typehint = generate_typehints("a")
66+
assert "a: str\n" == typehint
67+
typehint = generate_typehints("sa")
68+
assert "sa: int\n" == typehint
69+
typehint = generate_typehints("usa")
70+
assert "usa: int\n" == typehint
71+
72+
with raises(NotImplementedError) as err:
73+
generate_typehints("MyKlass")
74+
assert "class" in str(err)
75+
76+
typehint = generate_typehints("callme")
77+
assert (
78+
'@overload\ndef callme (a: "std.string", b: bool, c: "std.vector[int]") -> float: ...\n'
79+
== typehint
80+
)
81+
82+
typehint = generate_typehints("Klass")
83+
assert "Klass = MyKlass\n" == typehint
84+
85+
typehint = generate_typehints("KlassFloat")
86+
assert "KlassFloat = \"MyTKlass[float]\"\n" == typehint
87+
88+
with raises(NotImplementedError) as err:
89+
generate_typehints("MyTKlass")
90+
91+
with raises(NotImplementedError) as err:
92+
generate_typehints("TypeHints")
93+
assert "namespace" in str(err)
94+
95+
with raises(TypeError) as err:
96+
generate_typehints("unknown")
97+
assert "Unknown Type" in str(err)
98+
99+
with raises(TypeError) as err:
100+
generate_typehints("TypeHints::x")

0 commit comments

Comments
 (0)