Skip to content

Commit 1eaa51e

Browse files
committed
handle tuple/dict when creating board stubs
1 parent 1cfedcb commit 1eaa51e

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

tools/board_stubs/build_board_specific_stubs/board_stub_builder.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def get_board_pins(pin_filename):
2727
continue
2828

2929
board_member = search.group(1)
30+
extra_typing = None
3031

3132
board_type_search = re.search(r"MP_ROM_PTR\(&pin_(.*?)\)", line)
3233
if board_type_search:
@@ -38,8 +39,18 @@ def get_board_pins(pin_filename):
3839
board_type_search = re.search(
3940
r"MP_ROM_PTR\(&(.*?)\[0\].[display|epaper_display]", line
4041
)
42+
43+
if board_type_search is None:
44+
board_type_search = re.search(r"MP_ROM_PTR\(&(.*?)_tuple", line)
45+
if board_type_search is not None:
46+
extra_typing = "Tuple[Any]"
47+
if board_type_search is None:
48+
board_type_search = re.search(r"MP_ROM_PTR\(&(.*?)_dict", line)
49+
if board_type_search is not None:
50+
extra_typing = "Dict[str, Any]"
51+
4152
if board_type_search is None:
42-
records.append(["unmapped", None, line])
53+
records.append(["unmapped", None, line, extra_typing])
4354
continue
4455

4556
board_type = board_type_search.group(1)
@@ -56,7 +67,7 @@ def get_board_pins(pin_filename):
5667
if extra_search:
5768
extra = extra_search.group(1)
5869

59-
records.append([board_type, board_member, extra])
70+
records.append([board_type, board_member, extra, extra_typing])
6071

6172
return records
6273

@@ -69,8 +80,10 @@ def create_board_stubs(board_id, records, mappings, board_filename):
6980
needs_busio = False
7081
needs_displayio = False
7182
needs_microcontroller = False
83+
needs_dict = False
84+
needs_tuple = False
7285

73-
for board_type, board_member, extra in records:
86+
for board_type, board_member, extra, extra_typing in records:
7487
if board_type == "pin":
7588
needs_microcontroller = True
7689
comment = f" # {extra}"
@@ -121,6 +134,13 @@ def create_board_stubs(board_id, records, mappings, board_filename):
121134
member_data += f"{board_member}: {class_name}\n"
122135
members.append(member_data)
123136

137+
elif extra_typing is not None:
138+
if "Dict" in extra_typing:
139+
needs_dict = True
140+
elif "Tuple" in extra_typing:
141+
needs_tuple = True
142+
members.append(f"{board_member}: {extra_typing}\n")
143+
124144
elif board_type == "unmapped":
125145
unmapped.append(extra)
126146

@@ -152,6 +172,14 @@ def create_board_stubs(board_id, records, mappings, board_filename):
152172
if needs_microcontroller:
153173
boards_file.write("import microcontroller\n")
154174

175+
if needs_dict:
176+
if needs_tuple:
177+
boards_file.write("from typing import Any, Dict, Tuple\n")
178+
else:
179+
boards_file.write("from typing import Any, Dict\n")
180+
elif needs_tuple:
181+
boards_file.write("from typing import Any, Tuple\n")
182+
155183
boards_file.write("\n\n")
156184
boards_file.write("# Board Info:\n")
157185
boards_file.write("board_id: str\n")
@@ -200,7 +228,7 @@ def process(board_mappings, export_dir):
200228
records = get_board_pins(pin_filename)
201229
create_board_stubs(board_id, records, mappings, f"{sub_dir}/__init__.pyi")
202230

203-
for board_type, board_member, extra in records:
231+
for board_type, board_member, extra, extra_typing in records:
204232
if board_type == "pin":
205233
total_pins += 1
206234
elif board_type == "unmapped":

0 commit comments

Comments
 (0)