Skip to content

Commit 70b6cdc

Browse files
committed
enhance code builder so get_vertex_codes() has the best return type
1 parent 68bb298 commit 70b6cdc

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

generator/codebuilder.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ def snake_case(name):
6363

6464

6565
@lru_cache(128)
66-
def _convert_type(jtype: str) -> str:
66+
def _convert_type(jtype: str, parameter=True) -> str:
6767
if jtype == "void":
6868
return "None"
69-
elif jtype in ref.TYPE_OVERRIDES:
70-
return ref.TYPE_OVERRIDES[jtype]
69+
elif parameter and jtype in ref.PARAM_TYPE_OVERRIDES:
70+
return ref.PARAM_TYPE_OVERRIDES[jtype]
71+
elif not parameter and jtype in ref.RETURN_TYPE_OVERRIDES:
72+
return ref.RETURN_TYPE_OVERRIDES[jtype]
7173
elif jtype.endswith("[][]"):
7274
return f"JArray({ref.JPYPE_CONVERSIONS[jtype[:-4]]}, 2)"
7375
elif jtype.endswith("[]"):
@@ -148,7 +150,7 @@ def _make_param_rettype_strs(self, fname, first_param, params, paramnames, retty
148150
paramstrs = [first_param] + [
149151
_param_annotation(pn, p) for pn, p in zip(parameter_names, params)
150152
]
151-
rettypestr = _convert_type(rettype)
153+
rettypestr = _convert_type(rettype, parameter=False)
152154

153155
return paramstrs, rettypestr
154156

generator/reference.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@
2525

2626
PY5_SKIP_RETURN_TYPES = set()
2727

28-
TYPE_OVERRIDES = {
29-
# this is correct, see _return_list_py5shapes
30-
"processing.core.PShape[]": "list[Py5Shape]",
28+
PARAM_TYPE_OVERRIDES = {
3129
"char[]": "list[chr]",
3230
"java.lang.String[]": "list[str]",
3331
"float[]": "Iterator[float]",
3432
"float[][]": "Iterator[Iterator[float]]",
3533
"int[]": "Iterator[int]",
3634
}
3735

36+
RETURN_TYPE_OVERRIDES = {
37+
# this is correct, see _return_list_py5shapes
38+
"processing.core.PShape[]": "list[Py5Shape]",
39+
"java.lang.String[]": "list[str]",
40+
"int[]": "npt.NDArray[np.int32]",
41+
}
42+
3843
JPYPE_CONVERSIONS = {
3944
"boolean": "JBoolean",
4045
"int": "JInt",

py5_docs/Reference/api_en/Py5Shape_get_vertex_codes.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ category = vertices
77
subcategory = None
88

99
@@ signatures
10-
get_vertex_codes() -> Iterator[int]
10+
get_vertex_codes() -> npt.NDArray[np.int32]
1111

1212
@@ description
1313
Get the vertex codes for a `Py5Shape` object. The vertex codes can be used to inspect a shape's geometry to determine what kind of vertices it has. Each can be one of `BREAK`, `VERTEX`, `BEZIER_VERTEX`, `QUADRATIC_VERTEX` or `CURVE_VERTEX`.

0 commit comments

Comments
 (0)