* Mypy: Initial support

 * Add to pre-commit (currently installs own deps, could maybe changed
   to poetry venv in the future to reuse environment and don't need
   duplicated types deps).
 * Add type hints.

* Mypy: Add missing annotations
This commit is contained in:
Dominique Lasserre
2024-11-26 22:28:05 +01:00
committed by GitHub
parent 2a163569bc
commit 1163ddb4ac
31 changed files with 637 additions and 531 deletions
+16 -5
View File
@@ -1,12 +1,13 @@
import datetime
import json
import zoneinfo
from typing import Any
import numpy as np
# currently unused
def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
def ist_dst_wechsel(tag: datetime.datetime, timezone: str = "Europe/Berlin") -> bool:
"""Checks if Daylight Saving Time (DST) starts or ends on a given day."""
tz = zoneinfo.ZoneInfo(timezone)
# Get the current day and the next day
@@ -20,15 +21,25 @@ def ist_dst_wechsel(tag: datetime.datetime, timezone="Europe/Berlin") -> bool:
class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
@classmethod
def convert_numpy(cls, obj: Any) -> tuple[Any, bool]:
if isinstance(obj, np.ndarray):
return obj.tolist() # Convert NumPy arrays to lists
# Convert NumPy arrays to lists
return [
None if isinstance(x, (int, float)) and np.isnan(x) else x for x in obj.tolist()
], True
if isinstance(obj, np.generic):
return obj.item() # Convert NumPy scalars to native Python types
return obj.item(), True # Convert NumPy scalars to native Python types
return obj, False
def default(self, obj: Any) -> Any:
obj, converted = NumpyEncoder.convert_numpy(obj)
if converted:
return obj
return super(NumpyEncoder, self).default(obj)
@staticmethod
def dumps(data):
def dumps(data: Any) -> str:
"""Static method to serialize a Python object into a JSON string using NumpyEncoder.
Args: