From 9cf3c325846375ae7f95cbb88c873ff4702fbddd Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Thu, 11 Jul 2024 11:19:58 +0200 Subject: [PATCH] Fix type annotations of logging.run_main and .run_cli --- CHANGELOG.md | 7 +++++++ src/sensai/util/logging.py | 7 ++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e77fec0..e14627d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## Unreleased + +### Improvements/Changes + +* `util.logging`: + * Fix type annotations of `run_main` and `run_cli` + ## v1.2.0 (2024-06-10) ### Improvements/Changes diff --git a/src/sensai/util/logging.py b/src/sensai/util/logging.py index d09330ad..cc35195c 100644 --- a/src/sensai/util/logging.py +++ b/src/sensai/util/logging.py @@ -5,13 +5,14 @@ from datetime import datetime from io import StringIO from logging import * -from typing import List, Callable, Any, Optional +from typing import List, Callable, Any, Optional, TypeVar import pandas as pd log = getLogger(__name__) LOG_DEFAULT_FORMAT = '%(levelname)-5s %(asctime)-15s %(name)s:%(funcName)s:%(lineno)d - %(message)s' +T = TypeVar("T") # Holds the log format that is configured by the user (using function `configure`), such # that it can be reused in other places @@ -82,7 +83,7 @@ def configure(format=LOG_DEFAULT_FORMAT, level=lg.DEBUG): # noinspection PyShadowingBuiltins -def run_main(main_fn: Callable[[], Any], format=LOG_DEFAULT_FORMAT, level=lg.DEBUG): +def run_main(main_fn: Callable[..., T], format=LOG_DEFAULT_FORMAT, level=lg.DEBUG) -> T: """ Configures logging with the given parameters, ensuring that any exceptions that occur during the execution of the given function are logged. @@ -104,7 +105,7 @@ def run_main(main_fn: Callable[[], Any], format=LOG_DEFAULT_FORMAT, level=lg.DEB # noinspection PyShadowingBuiltins -def run_cli(main_fn: Callable[[], Any], format=LOG_DEFAULT_FORMAT, level=lg.DEBUG): +def run_cli(main_fn: Callable[..., T], format: str = LOG_DEFAULT_FORMAT, level: int = lg.DEBUG) -> Optional[T]: """ Configures logging with the given parameters and runs the given main function as a CLI using `jsonargparse` (which is configured to also parse attribute docstrings, such