Skip to content

Commit 3044797

Browse files
autogenerate type hints for pyray
1 parent 84fd32b commit 3044797

File tree

3 files changed

+874
-1
lines changed

3 files changed

+874
-1
lines changed

create_stub.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from raylib.static import rl, ffi
2+
3+
from inspect import ismethod,getmembers,isbuiltin
4+
import inflection
5+
6+
7+
print("""from typing import Any
8+
9+
class PyRay:
10+
def pointer(self, struct):
11+
return ffi.addressof(struct)
12+
""")
13+
14+
15+
def ctype_to_python_type(t):
16+
if t == '_Bool':
17+
return "bool"
18+
elif t == 'void':
19+
return 'None'
20+
elif t == "long":
21+
return "int"
22+
elif t == "double":
23+
return "float"
24+
elif "char *" in t:
25+
return "str"
26+
elif "*" in t:
27+
return "Any"
28+
elif t.startswith("struct"):
29+
return "Any" # This should be CData but cant get PyCharm to understand that - it just shows up as None
30+
elif t.startswith("unsigned"):
31+
return t.replace("unsigned ","")
32+
else:
33+
return t
34+
35+
for name, attr in getmembers(rl):
36+
uname = inflection.underscore(name).replace('3_d', '_3d').replace('2_d', '_2d')
37+
if isbuiltin(attr) or str(type(attr)) == "<class '_cffi_backend.__FFIFunctionWrapper'>":
38+
39+
40+
sig = ""
41+
for i, arg in enumerate(ffi.typeof(attr).args):
42+
param_name = arg.cname.replace("struct","").replace("char *","str").replace("*","_pointer").replace(" ","")
43+
param_type = ctype_to_python_type(arg.cname)
44+
sig += f", {param_name}_{i}: {param_type}"
45+
46+
47+
return_type = ffi.typeof(attr).result.cname
48+
49+
50+
print(f" def {uname}(self{sig}) -> {ctype_to_python_type(return_type)}: ...")
51+
52+
53+
else:
54+
print(f" {name}: {str(type(attr))[8:-2]}") # this isolates the type
55+
56+
57+
for struct in ffi.list_types()[0]:
58+
print(f" {struct}: Any") # This should be CData but cant get PyCharm to understand that - it just shows up as None

0 commit comments

Comments
 (0)