Skip to content

Commit d556849

Browse files
Add mypy Pre-Commit For hi & hit Pt.10 (IMAP-Science-Operations-Center#674)
* mypy set up for hit and hi * mypy set up for hit and hi * mypy for hi * mypy for hit * fixing test failures * g * changes * inline ignore comments for merge conflict * mypyfixes for merge
1 parent c6cdd97 commit d556849

15 files changed

Lines changed: 81 additions & 50 deletions

File tree

imap_processing/decom.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
to decommutate CCSDS packet data using a given XTCE packet definition.
66
"""
77

8+
from pathlib import Path
9+
from typing import Union
10+
811
from space_packet_parser import parser, xtcedef
912

1013

11-
def decom_packets(packet_file: str, xtce_packet_definition: str) -> list:
14+
def decom_packets(
15+
packet_file: Union[str, Path], xtce_packet_definition: Union[str, Path]
16+
) -> list:
1217
"""
1318
Unpack CCSDS data packet.
1419

imap_processing/hi/l0/decom_hi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from imap_processing import decom, imap_module_directory
44

55

6-
def decom_packets(packet_file_path: str):
6+
def decom_packets(packet_file_path: str) -> list:
77
"""
88
Decom IMAP-Hi data using its packet definition.
99
@@ -20,4 +20,5 @@ def decom_packets(packet_file_path: str):
2020
packet_def_file = (
2121
imap_module_directory / "hi/packet_definitions/hi_packet_definition.xml"
2222
)
23-
return decom.decom_packets(packet_file_path, packet_def_file)
23+
decom_file_packets: list = decom.decom_packets(packet_file_path, packet_def_file)
24+
return decom_file_packets

imap_processing/hi/l1a/hi_l1a.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""IMAP-HI L1A processing module."""
22

33
import logging
4-
from pathlib import Path
5-
from typing import Union
64

75
from imap_processing.hi.l0 import decom_hi
86
from imap_processing.hi.l1a.histogram import create_dataset as hist_create_dataset
@@ -14,7 +12,7 @@
1412
logger = logging.getLogger(__name__)
1513

1614

17-
def hi_l1a(packet_file_path: Union[str, Path], data_version: str):
15+
def hi_l1a(packet_file_path: str, data_version: str) -> list:
1816
"""
1917
Will process IMAP raw data to l1a.
2018

imap_processing/hi/l1a/science_direct_event.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,17 @@ def parse_direct_event(event_data: str) -> dict:
102102
dict
103103
Parsed event data.
104104
"""
105-
event_type = int(event_data[:2])
105+
event_type = int(event_data[:2], 2)
106106
metaevent = 0
107107
if event_type == metaevent:
108108
# parse metaevent
109-
event_type = event_data[:2]
110109
esa_step = event_data[2:6]
111110
subseconds = event_data[6:16]
112111
seconds = event_data[16:]
113112

114113
# return parsed metaevent data
115114
return {
116-
"start_bitmask_data": int(event_type, 2),
115+
"start_bitmask_data": event_type,
117116
"esa_step": int(esa_step, 2),
118117
"subseconds": int(subseconds, 2),
119118
"seconds": int(seconds, 2),
@@ -176,7 +175,7 @@ def create_dataset(de_data_list: list, packet_met_time: list) -> xr.Dataset:
176175
Xarray dataset.
177176
"""
178177
# These are the variables that we will store in the dataset
179-
data_dict = {
178+
data_dict: dict = {
180179
"epoch": list(),
181180
"event_met": list(),
182181
"ccsds_met": list(),

imap_processing/hi/l1b/hi_l1b.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
CDF_MANAGER.load_variable_attributes("imap_hi_variable_attrs.yaml")
1717

1818

19-
def hi_l1b(l1a_dataset: xr.Dataset, data_version: str):
19+
def hi_l1b(l1a_dataset: xr.Dataset, data_version: str) -> xr.Dataset:
2020
"""
2121
High level IMAP-HI L1B processing function.
2222
@@ -43,14 +43,16 @@ def hi_l1b(l1a_dataset: xr.Dataset, data_version: str):
4343
if logical_source_parts[-1].endswith("hk"):
4444
# if packet_enum in (HIAPID.H45_APP_NHK, HIAPID.H90_APP_NHK):
4545
packet_enum = HIAPID(l1a_dataset["pkt_apid"].data[0])
46-
conversion_table_path = (
46+
conversion_table_path = str(
4747
imap_module_directory / "hi" / "l1b" / "hi_eng_unit_convert_table.csv"
4848
)
4949
l1b_dataset = convert_raw_to_eu(
5050
l1a_dataset,
5151
conversion_table_path=conversion_table_path,
5252
packet_name=packet_enum.name,
53-
comment="#",
53+
comment="#", # type: ignore[arg-type]
54+
# Todo error, Argument "comment" to "convert_raw_to_eu" has incompatible
55+
# type "str"; expected "dict[Any, Any]"
5456
converters={"mnemonic": str.lower},
5557
)
5658

@@ -77,7 +79,7 @@ def hi_l1b(l1a_dataset: xr.Dataset, data_version: str):
7779
return l1b_dataset
7880

7981

80-
def annotate_direct_events(l1a_dataset):
82+
def annotate_direct_events(l1a_dataset: xr.Dataset) -> xr.Dataset:
8183
"""
8284
Perform Hi L1B processing on direct event data.
8385

imap_processing/hi/l1c/hi_l1c.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""IMAP-HI l1c processing module."""
22

33
import logging
4-
from pathlib import Path
5-
from typing import Union
4+
from collections.abc import Sized
5+
from typing import Optional
66

77
import numpy as np
88
import xarray as xr
@@ -13,7 +13,7 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16-
def hi_l1c(dependencies: list, data_version: str):
16+
def hi_l1c(dependencies: list, data_version: str) -> xr.Dataset:
1717
"""
1818
High level IMAP-Hi l1c processing function.
1919
@@ -52,7 +52,7 @@ def hi_l1c(dependencies: list, data_version: str):
5252
return l1c_dataset
5353

5454

55-
def generate_pset_dataset(de_dataset: Union[Path, str]) -> xr.Dataset:
55+
def generate_pset_dataset(de_dataset: xr.Dataset) -> xr.Dataset:
5656
"""
5757
Will process IMAP-Hi l1b product into a l1c pset xarray dataset.
5858
@@ -77,7 +77,7 @@ def generate_pset_dataset(de_dataset: Union[Path, str]) -> xr.Dataset:
7777
return pset_dataset
7878

7979

80-
def allocate_pset_dataset(n_esa_steps: int, sensor_str: str):
80+
def allocate_pset_dataset(n_esa_steps: int, sensor_str: str) -> xr.Dataset:
8181
"""
8282
Allocate an empty xarray.Dataset.
8383
@@ -191,7 +191,9 @@ def allocate_pset_dataset(n_esa_steps: int, sensor_str: str):
191191
return dataset
192192

193193

194-
def full_dataarray(name, attrs, coords: dict, shape=None):
194+
def full_dataarray(
195+
name: str, attrs: dict, coords: dict, shape: Optional[Sized] = None
196+
) -> xr.DataArray:
195197
"""
196198
Generate an empty xarray.DataArray with appropriate attributes.
197199

imap_processing/hi/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HIAPID(IntEnum):
1515
H90_SCI_DE = 834
1616

1717
@property
18-
def sensor(self):
18+
def sensor(self) -> str:
1919
"""
2020
Define the sensor name attribute for this class.
2121

imap_processing/hit/hit_cdf_attrs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,5 +863,9 @@
863863
}
864864

865865
# Dictionaries of complete L1A and L1B housekeeping attributes
866-
l1a_hk_attrs.update(l1a_l1b_hk_attrs)
867-
l1b_hk_attrs.update(l1a_l1b_hk_attrs)
866+
l1a_hk_attrs.update(l1a_l1b_hk_attrs) # type: ignore[arg-type]
867+
l1b_hk_attrs.update(l1a_l1b_hk_attrs) # type: ignore[arg-type]
868+
869+
# TODO Fix mypy error above.
870+
# Argument 1 to "update" of "MutableMapping" has incompatible type
871+
# "dict[str, AttrBase]"; expected "SupportsKeysAndGetItem[str, ScienceAttrs]"

imap_processing/hit/l0/data_classes/housekeeping.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dataclasses import dataclass
44

55
import numpy as np
6+
import space_packet_parser
67

78
from imap_processing.ccsds.ccsds_data import CcsdsData
89
from imap_processing.hit.l0.utils.hit_base import HITBase
@@ -210,13 +211,18 @@ class Housekeeping(HITBase):
210211
L34B_BIAS: int
211212
EBOX_P2D0VD: int
212213

213-
def __init__(self, packet, software_version: str, packet_file_name: str):
214+
def __init__(
215+
self,
216+
packet: space_packet_parser.parser.Packet,
217+
software_version: str,
218+
packet_file_name: str,
219+
):
214220
"""Housekeeping Data class initialization method."""
215221
super().__init__(software_version, packet_file_name, CcsdsData(packet.header))
216222
self.parse_data(packet)
217223
self._parse_leak()
218224

219-
def _parse_leak(self):
225+
def _parse_leak(self) -> None:
220226
"""Parse each current leakage field and put into an array."""
221227
# Each Leak field is 10 bits long
222228
leak_bit_length = 10

imap_processing/hit/l0/utils/hit_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from dataclasses import dataclass, fields
44

5+
import space_packet_parser
6+
57
from imap_processing.ccsds.ccsds_data import CcsdsData
68

79

@@ -29,13 +31,13 @@ class HITBase:
2931
packet_file_name: str
3032
ccsds_header: CcsdsData
3133

32-
def parse_data(self, packet):
34+
def parse_data(self, packet: space_packet_parser.parser.Packet) -> None:
3335
"""
3436
Parse Lo L0 packet data.
3537
3638
Parameters
3739
----------
38-
packet : dict
40+
packet : space_packet_parser.parser.Packet
3941
A single Lo L0 packet from space packet parser.
4042
"""
4143
attributes = [field.name for field in fields(self)]

0 commit comments

Comments
 (0)