Skip to content

Commit

Permalink
FEATURE: Add mypy linter github action
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Nov 15, 2024
1 parent 85955cc commit 0f3ebae
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 25 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: mypy

on: [push, workflow_dispatch]

jobs:
mypy:
name: runner / mypy
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install dependencies
run: |
python -m pip install -U mypy
- name: lint the code with mypy
uses: tsuyoshicho/action-mypy@v4
with:
github_token: ${{ secrets.github_token }}
# Change reviewdog reporter if you need [github-pr-check,github-check,github-pr-review].
reporter: github-pr-review
8 changes: 6 additions & 2 deletions MethodicConfigurator/annotate_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
PARAM_NAME_MAX_LEN = 16
VERSION = "1.0"

# mypy: disable-error-code="unused-ignore"


def arg_parser():
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -349,8 +351,10 @@ def get_xml_data(base_url: str, directory: str, filename: str, vehicle_type: str
else:
# No locally cached file exists, get it from the internet
try:
from requests import exceptions as requests_exceptions # pylint: disable=import-outside-toplevel
from requests import get as requests_get # pylint: disable=import-outside-toplevel
# pylint: disable=import-outside-toplevel
from requests import exceptions as requests_exceptions # type: ignore[import-untyped]
from requests import get as requests_get
# pylint: enable=import-outside-toplevel
except ImportError as exc:
logging.critical("The requests package was not found")
logging.critical("Please install it by running 'pip install requests' in your terminal.")
Expand Down
2 changes: 1 addition & 1 deletion MethodicConfigurator/backend_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from typing import Any
from zipfile import ZipFile

from requests import get as requests_get
from requests import get as requests_get # type: ignore[import-untyped]

from MethodicConfigurator import _
from MethodicConfigurator.annotate_params import (
Expand Down
4 changes: 3 additions & 1 deletion MethodicConfigurator/backend_mavftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

# pylint: disable=too-many-lines
# pylint: disable=invalid-name
# mypy: disable-error-code="union-attr,operator, arg-type"

# opcodes
OP_None = 0
OP_TerminateSession = 1
Expand Down Expand Up @@ -323,7 +325,7 @@ def __init__(
self.total_size = 0
self.read_gaps = []
self.read_gap_times = {}
self.last_gap_send = 0
self.last_gap_send = 0.0
self.read_retries = 0
self.read_total = 0
self.remote_file_size = None
Expand Down
8 changes: 4 additions & 4 deletions MethodicConfigurator/frontend_tkinter_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ def __init__(self, *args, **kwargs):
default_font = tkFont.nametofont(self.cget("font"))
default_size = default_font.cget("size")

bold_font = tkFont.Font(**default_font.configure())
italic_font = tkFont.Font(**default_font.configure())
h1_font = tkFont.Font(**default_font.configure())
bold_font = tkFont.Font(**default_font.configure()) # type: ignore[arg-type]
italic_font = tkFont.Font(**default_font.configure()) # type: ignore[arg-type]
h1_font = tkFont.Font(**default_font.configure()) # type: ignore[arg-type]

bold_font.configure(weight="bold")
italic_font.configure(slant="italic")
Expand Down Expand Up @@ -340,7 +340,7 @@ def __init__(self, root_tk: Optional[tk.Toplevel] = None):
if root_tk:
self.root = tk.Toplevel(root_tk)
else:
self.root = tk.Tk()
self.root = tk.Tk() # type: ignore[assignment]
# Set the application icon for the window and all child windows
# https://pythonassets.com/posts/window-icon-in-tk-tkinter/
self.root.iconphoto(True, tk.PhotoImage(file=LocalFilesystem.application_icon_filepath()))
Expand Down
5 changes: 4 additions & 1 deletion MethodicConfigurator/frontend_tkinter_component_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ def get_combobox_values(param_name: str) -> list:
cb.bind("<KeyRelease>", lambda event, path=path: self.validate_combobox(event, path)) # type: ignore

if path == ("ESC", "FC Connection", "Type"): # immediate update of ESC Protocol upon ESC Type selection
cb.bind("<<ComboboxSelected>>", lambda event, path=path: self.update_esc_protocol_combobox_entries(cb.get()))
cb.bind(
"<<ComboboxSelected>>",
lambda event, path=path: self.update_esc_protocol_combobox_entries(cb.get()), # type: ignore[misc]
)

cb.set(value)
return cb
Expand Down
4 changes: 3 additions & 1 deletion MethodicConfigurator/frontend_tkinter_directory_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,9 @@ def create_option3_widgets(self, last_vehicle_dir: str):
open_last_vehicle_directory_button = ttk.Button(
option3_label_frame,
text=_("Open Last Used Vehicle Configuration Directory"),
command=lambda last_vehicle_dir=last_vehicle_dir: self.open_last_vehicle_directory(last_vehicle_dir),
command=lambda last_vehicle_dir=last_vehicle_dir: self.open_last_vehicle_directory( # type: ignore[misc]
last_vehicle_dir
),
state=button_state,
)
open_last_vehicle_directory_button.pack(expand=False, fill=tk.X, padx=20, pady=5, anchor=tk.CENTER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,14 @@ def __create_new_value_entry( # pylint: disable=too-many-arguments
"<FocusIn>", lambda event: self.__open_bitmask_selection_window(event, param_name, bitmask_dict, old_value)
)
else:
# pylint: disable=line-too-long
new_value_entry.bind(
"<FocusOut>",
lambda event, current_file=self.current_file, param_name=param_name: self.__on_parameter_value_change(
lambda event, current_file=self.current_file, param_name=param_name: self.__on_parameter_value_change( # type: ignore[misc]
event, current_file, param_name
),
)
# pylint: enable=line-too-long
if doc_tooltip:
show_tooltip(new_value_entry, doc_tooltip)
return new_value_entry
Expand Down Expand Up @@ -490,12 +492,14 @@ def __create_change_reason_entry(self, param_name, param, new_value_entry):
if present_as_forced:
change_reason_entry.config(state="disabled", background="light grey")
else:
# pylint: disable=line-too-long
change_reason_entry.bind(
"<FocusOut>",
lambda event, current_file=self.current_file, param_name=param_name: self.__on_parameter_change_reason_change(
lambda event, current_file=self.current_file, param_name=param_name: self.__on_parameter_change_reason_change( # type: ignore[misc]
event, current_file, param_name
),
)
# pylint: enable=line-too-long
_value = new_value_entry.get()
msg = _("Reason why {param_name} should change to {_value}")
show_tooltip(change_reason_entry, msg.format(**locals()))
Expand Down
6 changes: 5 additions & 1 deletion MethodicConfigurator/frontend_tkinter_template_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def __init__(self, parent: Optional[tk.Toplevel] = None):

for col in self.tree["columns"]:
col_str = str(col)
self.tree.heading(col_str, text=col_str, command=lambda col=col_str: self.__sort_by_column(col, False))
self.tree.heading(
col_str,
text=col_str,
command=lambda col=col_str: self.__sort_by_column(col, False), # type: ignore[misc]
)

if isinstance(self.root, tk.Toplevel):
try:
Expand Down
5 changes: 3 additions & 2 deletions MethodicConfigurator/mavftp_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
from logging import error as logging_error
from logging import getLevelName as logging_getLevelName
from logging import info as logging_info
from typing import Any

import backend_mavftp as mavftp

# import time
import requests
import requests # type: ignore[import-untyped]
from pymavlink import mavutil

old_mavftp_member_variable_values = {}
old_mavftp_member_variable_values: dict[str, Any] = {}


# pylint: disable=duplicate-code
Expand Down
16 changes: 8 additions & 8 deletions MethodicConfigurator/tempcal_imu.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ def param_string(self, imu):
return params


class OnlineIMUfit:
class OnlineIMUfit: # pylint: disable=too-few-public-methods
"""implement the online learning used in ArduPilot"""

def __init__(self):
self.porder = None
self.mat = None
self.vec = None
self.porder: int = 0
self.mat = np.zeros((1, 1))
self.vec = np.zeros(1)

def update(self, x, y):
def __update(self, x, y):
temp = 1.0

for i in range(2 * (self.porder - 1), -1, -1):
Expand All @@ -172,7 +172,7 @@ def update(self, x, y):
self.vec[i] += y * temp
temp *= x

def get_polynomial(self):
def __get_polynomial(self):
inv_mat = np.linalg.inv(self.mat)
res = np.zeros(self.porder)
for i in range(self.porder):
Expand All @@ -185,8 +185,8 @@ def polyfit(self, x, y, order):
self.mat = np.zeros((self.porder, self.porder))
self.vec = np.zeros(self.porder)
for i, value in enumerate(x):
self.update(value, y[i])
return self.get_polynomial()
self.__update(value, y[i])
return self.__get_polynomial()


# pylint: disable=invalid-name
Expand Down
2 changes: 1 addition & 1 deletion credits/update_credits_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
SPDX-License-Identifier: GPL-3.0-or-later
"""

import requests
import requests # type: ignore[import-untyped]

# List of direct_dependencies and their license URLs
direct_dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion unittests/annotate_params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from unittest.mock import Mock, mock_open, patch
from xml.etree import ElementTree as ET # no parsing, just data-structure manipulation

import requests
import requests # type: ignore[import-untyped]
from defusedxml import ElementTree as DET # just parsing, no data-structure manipulation

from MethodicConfigurator.annotate_params import (
Expand Down

0 comments on commit 0f3ebae

Please sign in to comment.