diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index de927cff8..515439b2b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,3 +32,8 @@ repos: additional_dependencies: [black==22.12.0] files: ^(.*\.py|.*\.rst)$ args: [--line-length=120] + +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.19.1 + hooks: + - id: mypy diff --git a/libensemble/utils/misc.py b/libensemble/utils/misc.py index c4327dd02..6e3779910 100644 --- a/libensemble/utils/misc.py +++ b/libensemble/utils/misc.py @@ -4,7 +4,6 @@ from itertools import chain, groupby from operator import itemgetter -from typing import List import numpy as np import numpy.typing as npt @@ -85,6 +84,7 @@ def _get_combinable_multidim_names(first: dict, new_dtype_names: list) -> list: def _decide_dtype(name: str, entry, size: int) -> tuple: """decide dtype of field, and size if needed""" + output_type: str | type # str for numpy string type, type for python type if isinstance(entry, str): # use numpy style for string type output_type = "U" + str(len(entry) + 1) else: @@ -115,7 +115,7 @@ def _pack_field(input_dict: dict, field_names: list) -> tuple: return tuple(input_dict[name] for name in field_names) if len(field_names) > 1 else input_dict[field_names[0]] -def list_dicts_to_np(list_dicts: list, dtype: list = None, mapping: dict = {}) -> npt.NDArray: +def list_dicts_to_np(list_dicts: list, dtype: list | None = None, mapping: dict = {}) -> npt.NDArray: """Convert list of dicts to numpy structured array""" if list_dicts is None: return None @@ -231,7 +231,7 @@ def map_numpy_array(array: npt.NDArray, mapping: dict = {}) -> npt.NDArray: return array # Create new dtype with mapped fields - new_fields = [] + new_fields: list[tuple] = [] # Track fields processed by mapping to avoid duplication mapped_source_fields = set() @@ -275,7 +275,7 @@ def map_numpy_array(array: npt.NDArray, mapping: dict = {}) -> npt.NDArray: return mapped_array -def np_to_list_dicts(array: npt.NDArray, mapping: dict = {}) -> List[dict]: +def np_to_list_dicts(array: npt.NDArray, mapping: dict = {}) -> list[dict]: """Convert numpy structured array to list of dicts""" out = []