Skip to content

Commit e6f2c18

Browse files
generate type stubs which are sufficiently self-consistent to run mypy on all the examples
1 parent a33f4fc commit e6f2c18

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

create_define_consts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def process(filename):
4545
strval = str(e['value']).strip()
4646
if strval.startswith("__"):
4747
continue
48-
if strval in known_enum:
49-
print(e['name'] + " = raylib." + strval)
48+
# if strval in known_enum:
49+
# print(e['name'] + " = raylib." + strval)
5050
elif strval in known_define:
5151
print(e['name'] + " = " + strval)
5252
else:

create_stub_pyray.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
for st in js["structs"]:
3030
if st["name"] not in known_structs:
3131
known_structs[st["name"]] = st
32+
for e in js['enums']:
33+
if e['name'] and e['values']:
34+
print ("class "+e['name']+"(int):")
35+
for value in e['values']:
36+
print(" "+value['name']+" = "+str(value['value']))
37+
print("")
3238

3339

3440
def ctype_to_python_type(t):
@@ -42,29 +48,39 @@ def ctype_to_python_type(t):
4248
return "int"
4349
elif t == "uint64_t":
4450
return "int"
51+
elif t == "short":
52+
return "int"
53+
elif t == "unsigned short":
54+
return "int"
4555
elif t == "double":
4656
return "float"
4757
elif "char * *" in t:
4858
return "list[str]"
4959
elif "char *" in t:
5060
return "str"
51-
elif "char" in t:
61+
elif t == "char":
5262
return "str" # not sure about this one
63+
elif t == "unsigned char":
64+
return "int"
5365
elif "*" in t:
5466
return "Any"
67+
elif "[" in t:
68+
return "list" # TODO FIXME type of items in the list
5569
elif t.startswith("struct"):
5670
return t.replace("struct ", "")
5771
elif t.startswith("unsigned"):
5872
return t.replace("unsigned ", "")
73+
elif t.startswith("enum"):
74+
return t.replace("enum ", "")
5975
else:
6076
return t
6177

6278

6379
print("""from typing import Any
6480
81+
import _cffi_backend # type: ignore
6582
66-
def pointer(struct):
67-
...
83+
ffi: _cffi_backend.FFI
6884
""")
6985

7086
# These words can be used for c arg names, but not in python
@@ -90,6 +106,8 @@ def pointer(struct):
90106
if param_name in reserved_words:
91107
param_name = param_name + "_" + str(i)
92108
param_type = ctype_to_python_type(arg.cname)
109+
if "struct" in arg.cname:
110+
param_type += "|list|tuple"
93111
sig += f"{param_name}: {param_type},"
94112

95113
return_type = ffi.typeof(attr).result.cname
@@ -128,11 +146,14 @@ def pointer(struct):
128146
print(f' """ struct """')
129147
sig = ""
130148
for arg in ffi.typeof(struct).fields:
131-
sig += ", " + arg[0]
149+
ptype = ctype_to_python_type(arg[1].type.cname)
150+
if arg[1].type.kind == "struct":
151+
ptype += "|list|tuple"
152+
sig += f", {arg[0]}: {ptype}|None = None"
132153
print(f" def __init__(self{sig}):")
133154

134155
for arg in ffi.typeof(struct).fields:
135-
print(f" self.{arg[0]}={arg[0]}")
156+
print(f" self.{arg[0]}:{ctype_to_python_type(arg[1].type.cname)} = {arg[0]} # type: ignore")
136157

137158
# elif ffi.typeof(struct).kind == "enum":
138159
# print(f"{struct}: int")

create_stub_static.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,35 @@ def ctype_to_python_type(t):
4242
return "int"
4343
elif t == "uint64_t":
4444
return "int"
45+
elif t == "short":
46+
return "int"
47+
elif t == "unsigned short":
48+
return "int"
4549
elif t == "double":
4650
return "float"
4751
elif "char * *" in t:
48-
return "list[str]"
52+
return "list[bytes]"
4953
elif "char *" in t:
50-
return "str"
54+
return "bytes"
5155
elif "char" in t:
52-
return "str" # not sure about this one
56+
return "bytes" # not sure about this one
5357
elif "*" in t:
5458
return "Any"
59+
elif "[" in t:
60+
return "list" # TODO FIXME type of items in the list
5561
elif t.startswith("struct"):
5662
return t.replace("struct ", "")
5763
elif t.startswith("unsigned"):
5864
return t.replace("unsigned ", "")
65+
elif t.startswith("enum"):
66+
return t.replace("enum ", "")
5967
else:
6068
return t
6169

6270

6371
print("""from typing import Any
6472
65-
import _cffi_backend
73+
import _cffi_backend # type: ignore
6674
6775
ffi: _cffi_backend.FFI
6876
rl: _cffi_backend.Lib
@@ -96,6 +104,8 @@ class struct: ...
96104
if param_name in reserved_words:
97105
param_name = param_name + "_" + str(i)
98106
param_type = ctype_to_python_type(arg.cname)
107+
if "struct" in arg.cname:
108+
param_type += "|list|tuple"
99109
sig += f"{param_name}: {param_type},"
100110

101111
return_type = ffi.typeof(attr).result.cname
@@ -121,17 +131,22 @@ class struct: ...
121131
# if ffi.typeof(struct).fields is None:
122132
# print("weird empty struct, skipping", file=sys.stderr)
123133
# continue
124-
print(f"{struct}: struct")
134+
print(f"class {struct}:")
125135
# sig = ""
126-
# for arg in ffi.typeof(struct).fields:
127-
# sig += ", " + arg[0]
136+
fields = ffi.typeof(struct).fields
137+
if fields is not None:
138+
#print(ffi.typeof(struct).fields)
139+
#print(f" {arg}: {arg}")
128140
# print(f" def __init__(self{sig}):")
129141
#
130-
# for arg in ffi.typeof(struct).fields:
142+
for arg in ffi.typeof(struct).fields:
143+
print(f" {arg[0]}: {ctype_to_python_type(arg[1].type.cname)}")
144+
else:
145+
print(" ...")
131146
# print(f" self.{arg[0]}={arg[0]}")
132147

133148
elif ffi.typeof(struct).kind == "enum":
134-
print(f"{struct}: int")
149+
print(f"{struct} = int")
135150
else:
136151
print("ERROR UNKNOWN TYPE", ffi.typeof(struct), file=sys.stderr)
137152

make_docs.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ python3 create_enums.py > dynamic/raylib/enums.py
4242

4343
echo "creating defines.py"
4444

45-
python3 create_define_consts.py > raylib/defines.py
46-
python3 create_define_consts.py > dynamic/raylib/defines.py
45+
python3 create_define_consts.py | awk '!seen[$0]++' > raylib/defines.py
46+
python3 create_define_consts.py | awk '!seen[$0]++' > dynamic/raylib/defines.py
4747

4848

4949
echo "creating pyi files"
5050

5151
python3 create_stub_pyray.py > pyray/__init__.pyi
52-
python3 create_enums.py >> pyray/__init__.pyi
5352

5453
python3 create_stub_static.py >raylib/__init__.pyi
5554

0 commit comments

Comments
 (0)