Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mypy Pre-commit Hook and Fix tools folder Pt.1 #659

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ repos:
hooks:
- id: numpydoc-validation
exclude: '^imap_processing/tests/|.*test.*'

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.0'
hooks:
- id: mypy
pass_filenames: false
args: [., --strict, --explicit-package-bases,
--disable-error-code, import-untyped,
--disable-error-code, import-not-found,
--disable-error-code, no-untyped-call,
--disable-error-code, type-arg]
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ checks = ["all", #report on all checks, except the following
"ES01", # Ignore No extended summary found
"RT02" ] # Ignore The first line of the Returns section
exclude = ['__init__' ] # don't report on objects that match any of these regex

[tool.mypy]
follow_imports = 'skip'
exclude = ["tests", "docs", "imap_processing"]
13 changes: 8 additions & 5 deletions tools/metadata_generation/metadata_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""SWAPI metadata generator."""

from pathlib import Path
from typing import Optional, Union

import pandas as pd
import yaml


def get_global_attributes(sheet):
def get_global_attributes(sheet: pd.DataFrame) -> pd.DataFrame:
"""
Get the global attributes from a metadata spreadsheet.

Expand All @@ -17,7 +18,7 @@ def get_global_attributes(sheet):

Returns
-------
dict
pd.DataFrame
A dictionary of the global attributes, where the keys are the attribute names
and the values are the attribute descriptions.
"""
Expand All @@ -27,15 +28,17 @@ def get_global_attributes(sheet):
return sheet.to_dict()["CATDESC"]


def get_dataset_attributes(sheet, global_attrs=None):
def get_dataset_attributes(
sheet: pd.DataFrame, global_attrs: Optional[dict] = None
) -> dict:
"""
Get the dataset attributes from a metadata spreadsheet.

Parameters
----------
sheet : pandas.DataFrame
A sheet/tab from an Excel file represented as a pandas DataFrame.
global_attrs : list todo check
global_attrs : None
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
List of global attributes.

Returns
Expand Down Expand Up @@ -64,7 +67,7 @@ def get_dataset_attributes(sheet, global_attrs=None):


# Load all sheets
def process_file(excel_path, output_folder):
def process_file(excel_path: Union[str, Path], output_folder: Path) -> None:
"""
Will process the metadata file and output the metadata to a JSON file.

Expand Down
11 changes: 7 additions & 4 deletions tools/spice/spice_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

import logging

import numpy as np
import spiceypy as spice

# Logger setup
logger = logging.getLogger(__name__)


def get_attitude_timerange(ck_kernel, id):
def get_attitude_timerange(ck_kernel: str, id: int) -> tuple:
"""
Get attitude timerange using the ck kernel.

Expand Down Expand Up @@ -65,7 +66,9 @@ def get_attitude_timerange(ck_kernel, id):
return start, end


def _get_particle_velocity(direct_events):
def _get_particle_velocity(
direct_events: dict,
) -> np.ndarray:
"""
Get the particle velocity in the heliosphere frame.

Expand All @@ -76,7 +79,7 @@ def _get_particle_velocity(direct_events):

Returns
-------
vultra_heliosphere_frame : np.ndarray
vultra_heliosphere_frame : numpy.ndarray
Particle velocity in the heliosphere frame.

Notes
Expand Down Expand Up @@ -108,7 +111,7 @@ def _get_particle_velocity(direct_events):
return ultra_velocity_heliosphere_frame


def build_annotated_events(direct_events, kernels):
def build_annotated_events(direct_events: dict, kernels: list) -> None:
"""
Build annotated events.

Expand Down
7 changes: 5 additions & 2 deletions tools/spice/spice_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import logging
import os
from typing import Optional

import spiceypy as spice

logger = logging.getLogger(__name__)


def list_files_with_extensions(directory: str, extensions=None) -> list[str]:
def list_files_with_extensions(
directory: str, extensions: Optional[list[str]] = None
) -> list[str]:
"""
List all files in a given directory that have the specified extensions.

Expand Down Expand Up @@ -41,7 +44,7 @@ def list_files_with_extensions(directory: str, extensions=None) -> list[str]:
return matching_files


def list_loaded_kernels(extensions=None) -> list:
def list_loaded_kernels(extensions: Optional[list[str]] = None) -> list:
"""
List furnished spice kernels, optionally filtered by specific extensions.

Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/ccsds_header_xtce_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CCSDSParameters:
"""The defined class for CCSDS parameters. Contains the object itself."""

def __init__(self):
def __init__(self) -> None:
self.parameters = [
{
"name": "VERSION",
Expand Down
61 changes: 45 additions & 16 deletions tools/xtce_generation/telemetry_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import xml.etree.ElementTree as Et
from datetime import datetime
from typing import Optional

import pandas as pd

Expand Down Expand Up @@ -39,7 +40,13 @@ class TelemetryGenerator:
Default set to None.
"""

def __init__(self, packet_name, path_to_excel_file, apid, pkt=None):
def __init__(
self,
packet_name: str,
path_to_excel_file: str,
apid: int,
pkt: Optional[str] = None,
) -> None:
"""Initialize TelemetryGenerator."""
self.packet_name = packet_name
self.apid = apid
Expand All @@ -52,7 +59,7 @@ def __init__(self, packet_name, path_to_excel_file, apid, pkt=None):
else:
self.pkt = pkt

def create_telemetry_xml(self):
def create_telemetry_xml(self) -> tuple:
"""
Create an XML representation of telemetry data based on input parameters.

Expand Down Expand Up @@ -97,7 +104,7 @@ def create_telemetry_xml(self):

return root, parameter_type_set, parameter_set, telemetry_metadata

def get_unique_bits_length(self):
def get_unique_bits_length(self) -> dict:
"""
Create dictionary.

Expand Down Expand Up @@ -135,7 +142,11 @@ def get_unique_bits_length(self):
unique_lengths = dict(sorted(unique_lengths.items(), key=lambda item: item[1]))
return unique_lengths

def create_parameter_types(self, parameter_type_set, unique_lengths):
def create_parameter_types(
self,
parameter_type_set: list,
unique_lengths: dict,
) -> list:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
"""
Create parameter types based on 'dataType' for the unique 'lengthInBits' values.

Expand All @@ -157,7 +168,9 @@ def create_parameter_types(self, parameter_type_set, unique_lengths):
for parameter_type_ref_name, size in unique_lengths.items():
if "UINT" in parameter_type_ref_name:
parameter_type = Et.SubElement(
parameter_type_set, "xtce:IntegerParameterType"
parameter_type_set, # type: ignore[arg-type]
# ToDo Change, expected Element type for argument 1
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
"xtce:IntegerParameterType",
)
parameter_type.attrib["name"] = parameter_type_ref_name
parameter_type.attrib["signed"] = "false"
Expand All @@ -168,7 +181,9 @@ def create_parameter_types(self, parameter_type_set, unique_lengths):

elif any(x in parameter_type_ref_name for x in ["SINT", "INT"]):
parameter_type = Et.SubElement(
parameter_type_set, "xtce:IntegerParameterType"
parameter_type_set, # type: ignore[arg-type]
# TODO change. Expected Element type for argument 1
"xtce:IntegerParameterType",
)
parameter_type.attrib["name"] = parameter_type_ref_name
parameter_type.attrib["signed"] = "true"
Expand All @@ -178,7 +193,9 @@ def create_parameter_types(self, parameter_type_set, unique_lengths):

elif "BYTE" in parameter_type_ref_name:
binary_parameter_type = Et.SubElement(
parameter_type_set, "xtce:BinaryParameterType"
parameter_type_set, # type: ignore[arg-type]
"xtce:BinaryParameterType",
# Todo Change, expected Element type for first argument.
)
binary_parameter_type.attrib["name"] = parameter_type_ref_name

Expand All @@ -195,7 +212,9 @@ def create_parameter_types(self, parameter_type_set, unique_lengths):

return parameter_type_set

def create_ccsds_packet_parameters(self, parameter_set, ccsds_parameters):
def create_ccsds_packet_parameters(
self, parameter_set: list, ccsds_parameters: list
) -> list:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
"""
Create XML elements to define CCSDS packet parameters based on the given data.

Expand All @@ -212,7 +231,9 @@ def create_ccsds_packet_parameters(self, parameter_set, ccsds_parameters):
The updated ParameterSet element.
"""
for parameter_data in ccsds_parameters:
parameter = Et.SubElement(parameter_set, "xtce:Parameter")
parameter = Et.SubElement(parameter_set, "xtce:Parameter") # type: ignore[arg-type]
# Argument 1 to "SubElement" has incompatible type
# "list[str]"; expected "Element"
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
parameter.attrib["name"] = parameter_data["name"]
parameter.attrib["parameterTypeRef"] = parameter_data["parameterTypeRef"]

Expand All @@ -222,8 +243,11 @@ def create_ccsds_packet_parameters(self, parameter_set, ccsds_parameters):
return parameter_set

def create_container_set(
self, telemetry_metadata, ccsds_parameters, container_name
):
self,
telemetry_metadata: dict,
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
ccsds_parameters: list,
container_name: str,
) -> dict:
"""
Create XML elements.

Expand All @@ -245,7 +269,9 @@ def create_container_set(
The updated TelemetryMetaData element.
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
"""
# Create ContainerSet element
container_set = Et.SubElement(telemetry_metadata, "xtce:ContainerSet")
container_set = Et.SubElement(telemetry_metadata, "xtce:ContainerSet") # type: ignore[arg-type]
# Argument 1 to "SubElement" has incompatible type
# "dict[Any, Any]"; expected "Element"
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved

# Create CCSDSPacket SequenceContainer
ccsds_packet_container = Et.SubElement(container_set, "xtce:SequenceContainer")
Expand Down Expand Up @@ -288,15 +314,15 @@ def create_container_set(

return telemetry_metadata

def create_remaining_parameters(self, parameter_set):
def create_remaining_parameters(self, parameter_set: list) -> list:
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
"""
Create XML elements for parameters.

These are based on DataFrame rows starting from SHCOARSE (also known as MET).

Parameters
----------
parameter_set : list todo check
parameter_set : list
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
The ParameterSet element where parameters will be added.

Returns
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -311,7 +337,10 @@ def create_remaining_parameters(self, parameter_set):
# not part of CCSDS header
continue

parameter = Et.SubElement(parameter_set, "xtce:Parameter")
parameter = Et.SubElement(parameter_set, "xtce:Parameter") # type: ignore[arg-type]
# todo Change
# Argument 1 to "SubElement" has incompatible type
# "list[Any]"; expected "Element"
daralynnrhode marked this conversation as resolved.
Show resolved Hide resolved
parameter.attrib["name"] = row["mnemonic"]
parameter_type_ref = f"{row['dataType']}{row['lengthInBits']}"

Expand All @@ -326,7 +355,7 @@ def create_remaining_parameters(self, parameter_set):

return parameter_set

def generate_telemetry_xml(self, output_xml_path, container_name):
def generate_telemetry_xml(self, output_xml_path: str, container_name: str) -> None:
"""
Create and output an XTCE file based on the data within the class.

Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/xtce_generator_codice.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tools.xtce_generation.telemetry_generator import TelemetryGenerator


def main():
def main() -> None:
"""Function used by instrument to generate XTCE."""
instrument_name = "codice"
current_directory = Path(__file__).parent
Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/xtce_generator_glows.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tools.xtce_generation.telemetry_generator import TelemetryGenerator


def main():
def main() -> None:
"""
Function is used to generate the GLOWS XTCE files for packet processing.

Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/xtce_generator_hi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tools.xtce_generation.telemetry_generator import TelemetryGenerator


def main():
def main() -> None:
"""Function used by hi to generate XTCE."""

instrument_name = "hi"
Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/xtce_generator_hit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from imap_processing import imap_module_directory


def main():
def main() -> None:
"""Function used by hit to generate XTCE."""
instrument_name = "hit"
current_directory = Path(__file__).parent
Expand Down
2 changes: 1 addition & 1 deletion tools/xtce_generation/xtce_generator_lo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# P_ILO_SCI_DE


def main():
def main() -> None:
"""Function used by lo to generate XTCE."""
instrument_name = "lo"
current_directory = Path(__file__).parent
Expand Down
6 changes: 4 additions & 2 deletions tools/xtce_generation/xtce_generator_mag.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tools.xtce_generation.telemetry_generator import TelemetryGenerator


def main():
def main() -> None:
"""Function used by mag to generate XTCE."""

instrument_name = "mag"
Expand All @@ -36,7 +36,9 @@ def main():
for packet_name, app_id in packets.items():
print(packet_name)
telemetry_generator = TelemetryGenerator(
packet_name=packet_name, path_to_excel_file=path_to_excel_file, apid=app_id
packet_name=packet_name,
path_to_excel_file=path_to_excel_file,
apid=int(app_id),
)
telemetry_generator.generate_telemetry_xml(
f"{packet_definition_path}/{packet_name}.xml", packet_name
Expand Down
Loading
Loading