Skip to content

Commit 1a4bd94

Browse files
committed
IMPROVEMENT: Added more parameter type hints
1 parent c603a92 commit 1a4bd94

12 files changed

+140
-119
lines changed

MethodicConfigurator/frontend_tkinter_component_editor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ def get_combobox_values(param_name: str) -> list:
562562
entry.insert(0, str(value))
563563
return entry
564564

565-
def get_validate_function(self, entry, path) -> Union[Callable[[tk.Event[tk.Entry]], object], None]: # pylint: disable=unsubscriptable-object
565+
def get_validate_function(self, entry, path) -> Union[Callable[[tk.Event], object], None]:
566566
validate_functions = {
567567
("Frame", "Specifications", "TOW min Kg"): lambda event, entry=entry, path=path: self.validate_entry_limits(
568568
event, entry, float, (0.01, 600), "Takeoff Weight", path

MethodicConfigurator/frontend_tkinter_connection_selection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from logging import warning as logging_warning
1717
from sys import exit as sys_exit
1818
from tkinter import simpledialog, ttk
19+
from typing import Union
1920

2021
from MethodicConfigurator import _
2122
from MethodicConfigurator.backend_flightcontroller import FlightController
@@ -44,7 +45,7 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen
4445
self.flight_controller = flight_controller
4546
self.destroy_parent_on_connect = destroy_parent_on_connect
4647
self.download_params_on_connect = download_params_on_connect
47-
self.previous_selection = (
48+
self.previous_selection: Union[None, str] = (
4849
flight_controller.comport.device
4950
if flight_controller.comport and hasattr(flight_controller.comport, "device")
5051
else None

MethodicConfigurator/frontend_tkinter_entry_dynamic.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class EntryWithDynamicalyFilteredListbox(Entry): # pylint: disable=too-many-anc
3636
def __init__( # pylint: disable=too-many-arguments, too-many-positional-arguments
3737
self,
3838
master,
39-
list_of_items=None,
39+
list_of_items: Union[None, list[str]] = None,
4040
custom_filter_function=None,
41-
listbox_width=None,
42-
listbox_height=12,
43-
ignorecase_match=False,
44-
startswith_match=True,
45-
vscrollbar=True,
46-
hscrollbar=True,
41+
listbox_width: Union[None, int] = None,
42+
listbox_height: int = 12,
43+
ignorecase_match: bool = False,
44+
startswith_match: bool = True,
45+
vscrollbar: bool = True,
46+
hscrollbar: bool = True,
4747
**kwargs,
4848
) -> None:
4949
if list_of_items is None:
@@ -176,7 +176,7 @@ def unpost_listbox(self) -> None:
176176
def get_value(self) -> str:
177177
return self._entry_var.get() # type: ignore[no-any-return] # mypy bug
178178

179-
def set_value(self, text, close_dialog=False) -> None:
179+
def set_value(self, text: str, close_dialog: bool = False) -> None:
180180
self._set_var(text)
181181

182182
if close_dialog:
@@ -185,12 +185,12 @@ def set_value(self, text, close_dialog=False) -> None:
185185
self.icursor(END)
186186
self.xview_moveto(1.0)
187187

188-
def _set_var(self, text) -> None:
188+
def _set_var(self, text: str) -> None:
189189
self._entry_var.trace_remove("write", self._trace_id)
190190
self._entry_var.set(text)
191191
self._trace_id = self._entry_var.trace_add("write", self._on_change_entry_var)
192192

193-
def update_entry_from_listbox(self, _event) -> str:
193+
def update_entry_from_listbox(self, _event: Union[None, tk.Event]) -> str:
194194
if self._listbox is not None:
195195
current_selection = self._listbox.curselection()
196196

@@ -207,7 +207,7 @@ def update_entry_from_listbox(self, _event) -> str:
207207

208208
return "break"
209209

210-
def _previous(self, _event) -> str:
210+
def _previous(self, _event: Union[None, tk.Event]) -> str:
211211
if self._listbox is not None:
212212
current_selection = self._listbox.curselection()
213213

@@ -229,7 +229,7 @@ def _previous(self, _event) -> str:
229229

230230
return "break"
231231

232-
def _next(self, _event) -> str:
232+
def _next(self, _event: Union[None, tk.Event]) -> str:
233233
if self._listbox is not None:
234234
current_selection = self._listbox.curselection()
235235
if len(current_selection) == 0:

MethodicConfigurator/frontend_tkinter_pair_tuple_combobox.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ class PairTupleCombobox(ttk.Combobox): # pylint: disable=too-many-ancestors
3535
of a tuple based on its key.
3636
"""
3737

38-
def __init__(self, container, list_pair_tuple, selected_element, cb_name, *args, **kwargs) -> None:
39-
super().__init__(container, *args, **kwargs)
38+
def __init__(
39+
self, master, list_pair_tuple: list[tuple[str, str]], selected_element: Union[None, str], cb_name: str, *args, **kwargs
40+
) -> None:
41+
super().__init__(master, *args, **kwargs)
4042
self.cb_name = cb_name
4143
self.list_keys: list[str] = []
4244
self.list_shows: list[str] = []
4345
self.set_entries_tupple(list_pair_tuple, selected_element)
4446
self.bind("<Configure>", self.on_combo_configure, add="+")
4547

46-
def set_entries_tupple(self, list_pair_tuple, selected_element) -> None:
48+
def set_entries_tupple(self, list_pair_tuple, selected_element: Union[None, str]) -> None:
4749
if isinstance(list_pair_tuple, list):
4850
for tpl in list_pair_tuple:
4951
self.list_keys.append(tpl[0])
@@ -83,7 +85,7 @@ def get_selected_key(self) -> Union[str, None]:
8385
return None
8486

8587
# https://stackoverflow.com/questions/39915275/change-width-of-dropdown-listbox-of-a-ttk-combobox
86-
def on_combo_configure(self, event) -> None:
88+
def on_combo_configure(self, event: tk.Event) -> None:
8789
combo = event.widget
8890
style = ttk.Style()
8991
# check if the combobox already has the "postoffset" property
@@ -132,8 +134,10 @@ class PairTupleComboboxTooltip(PairTupleCombobox): # pylint: disable=too-many-a
132134
b) The dropdown is closed (either by selection or pressing Esc)
133135
"""
134136

135-
def __init__(self, container, list_pair_tuple, selected_element, cb_name, *args, **kwargs) -> None:
136-
super().__init__(container, list_pair_tuple, selected_element, cb_name, *args, **kwargs)
137+
def __init__(
138+
self, master, list_pair_tuple: list[tuple[str, str]], selected_element: Union[None, str], cb_name: str, *args, **kwargs
139+
) -> None:
140+
super().__init__(master, list_pair_tuple, selected_element, cb_name, *args, **kwargs)
137141
self.tooltip: Union[None, Toplevel] = None
138142

139143
# Bind events related to the dropdown
@@ -144,26 +148,26 @@ def __init__(self, container, list_pair_tuple, selected_element, cb_name, *args,
144148
self._bind(("bind", lb), "<Escape>", self.on_escape_press, None) # type: ignore
145149
self.bind("<<ComboboxSelected>>", self.on_combobox_selected, None)
146150

147-
def on_key_release(self, _event) -> None:
151+
def on_key_release(self, _event: Union[None, tk.Event]) -> None:
148152
"""Get the keyboard highlighted index and create a tooltip for it"""
149153
pd = self.tk.call("ttk::combobox::PopdownWindow", self)
150154
lb = pd + ".f.l"
151155
if self.tk.call(lb, "curselection"):
152156
highlighted_index = int(self.tk.call(lb, "curselection")[0])
153157
self.create_tooltip_from_index(highlighted_index)
154158

155-
def on_motion(self, event) -> None:
159+
def on_motion(self, event: tk.Event) -> None:
156160
"""Get the mouse highlighted index and create a tooltip for it"""
157161
pd = self.tk.call("ttk::combobox::PopdownWindow", self)
158162
lb = pd + ".f.l"
159163
index = self.tk.call(lb, "index", f"@{event.x},{event.y}")
160164
self.create_tooltip_from_index(int(index))
161165

162-
def create_tooltip_from_index(self, index) -> None:
166+
def create_tooltip_from_index(self, index: int) -> None:
163167
with contextlib.suppress(IndexError):
164168
self.create_tooltip(f"{self.list_keys[index]}: {self.list_shows[index]}")
165169

166-
def create_tooltip(self, text) -> None:
170+
def create_tooltip(self, text: str) -> None:
167171
self.destroy_tooltip()
168172
try:
169173
if self.tooltip is None or self.tooltip.winfo_exists():
@@ -178,10 +182,10 @@ def create_tooltip(self, text) -> None:
178182
# If there's no active item, we don't need to update the tooltip
179183
pass
180184

181-
def on_combobox_selected(self, _event) -> None:
185+
def on_combobox_selected(self, _event: Union[None, tk.Event]) -> None:
182186
self.destroy_tooltip()
183187

184-
def on_escape_press(self, _event) -> None:
188+
def on_escape_press(self, _event: Union[None, tk.Event]) -> None:
185189
self.destroy_tooltip()
186190

187191
def destroy_tooltip(self) -> None:

MethodicConfigurator/frontend_tkinter_parameter_editor.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from logging import info as logging_info
1919
from logging import warning as logging_warning
2020
from tkinter import filedialog, messagebox, ttk
21+
from typing import Union
2122

2223
# from logging import critical as logging_critical
2324
from webbrowser import open as webbrowser_open # to open the blog post documentation
@@ -43,7 +44,7 @@
4344
from MethodicConfigurator.tempcal_imu import IMUfit
4445

4546

46-
def show_about_window(root, _version: str) -> None: # pylint: disable=too-many-locals
47+
def show_about_window(root: ttk.Frame, _version: str) -> None: # pylint: disable=too-many-locals
4748
# Create a new window for the custom "About" message
4849
about_window = tk.Toplevel(root)
4950
about_window.title(_("About"))
@@ -484,7 +485,7 @@ def __should_upload_file_to_fc(self, selected_file: str) -> None:
484485
logging_warning(_("No flight controller connection, will not upload any file"))
485486
messagebox.showwarning(_("Will not upload any file"), _("No flight controller connection"))
486487

487-
def on_param_file_combobox_change(self, _event, forced: bool = False) -> None:
488+
def on_param_file_combobox_change(self, _event: Union[None, tk.Event], forced: bool = False) -> None:
488489
if not self.file_selection_combobox["values"]:
489490
return
490491
self.parameter_editor_table.generate_edit_widgets_focus_out()
@@ -518,7 +519,7 @@ def download_flight_controller_parameters(self, redownload: bool = False) -> Non
518519
if not redownload:
519520
self.on_param_file_combobox_change(None, True) # the initial param read will trigger a table update
520521

521-
def repopulate_parameter_table(self, selected_file) -> None:
522+
def repopulate_parameter_table(self, selected_file: Union[None, str]) -> None:
522523
if not selected_file:
523524
return # no file was yet selected, so skip it
524525
if hasattr(self.flight_controller, "fc_parameters") and self.flight_controller.fc_parameters:
@@ -582,7 +583,7 @@ def upload_params_that_require_reset(self, selected_params: dict) -> None:
582583

583584
self.__reset_and_reconnect(fc_reset_required, fc_reset_unsure)
584585

585-
def __reset_and_reconnect(self, fc_reset_required, fc_reset_unsure) -> None:
586+
def __reset_and_reconnect(self, fc_reset_required: bool, fc_reset_unsure: list[str]) -> None:
586587
if not fc_reset_required and fc_reset_unsure:
587588
# Ask the user if they want to reset the ArduPilot
588589
_param_list_str = (", ").join(fc_reset_unsure)
@@ -625,7 +626,7 @@ def on_upload_selected_click(self) -> None:
625626
self.on_skip_click(force_focus_out_event=False)
626627

627628
# This function can recurse multiple times if there is an upload error
628-
def upload_selected_params(self, selected_params) -> None:
629+
def upload_selected_params(self, selected_params: dict) -> None:
629630
logging_info(_("Uploading %d selected %s parameters to flight controller..."), len(selected_params), self.current_file)
630631

631632
self.upload_params_that_require_reset(selected_params)
@@ -683,7 +684,7 @@ def upload_selected_params(self, selected_params) -> None:
683684
logging_info(_("All parameters uploaded to the flight controller successfully"))
684685
self.local_filesystem.write_last_uploaded_filename(self.current_file)
685686

686-
def on_skip_click(self, _event=None, force_focus_out_event=True) -> None:
687+
def on_skip_click(self, _event: Union[None, tk.Event] = None, force_focus_out_event: bool = True) -> None:
687688
if force_focus_out_event:
688689
self.parameter_editor_table.generate_edit_widgets_focus_out()
689690
self.write_changes_to_intermediate_parameter_file()

MethodicConfigurator/frontend_tkinter_parameter_editor_documentation_frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def update_documentation_labels(self, current_file: str) -> None:
124124
if blog_url:
125125
webbrowser_open(url=blog_url, new=0, autoraise=True)
126126

127-
def __update_documentation_label(self, label_key, text, url, url_expected=True) -> None:
127+
def __update_documentation_label(self, label_key: str, text: str, url: str, url_expected: bool = True) -> None:
128128
label = self.documentation_labels[label_key]
129129
if url:
130130
label.config(text=text, foreground="blue", cursor="hand2", underline=True)

0 commit comments

Comments
 (0)