From 63720ffa0fc1c0960d3f01612f9dfa83f92720c3 Mon Sep 17 00:00:00 2001 From: Michael Joyce Date: Thu, 19 Jan 2023 14:44:58 -0800 Subject: [PATCH 1/2] Issue #491 - Add logger type annotations for GUI mypy checks Add minor type annotations in log.py to keep type checking happy in AIT-GUI. Resolve #491 --- ait/core/log.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ait/core/log.py b/ait/core/log.py index 56672ee5..d94faeb2 100644 --- a/ait/core/log.py +++ b/ait/core/log.py @@ -23,6 +23,7 @@ import socket import datetime import time +from typing import Callable, Optional import logging import logging.handlers @@ -316,10 +317,10 @@ def notice(*args, **kwargs): # type `Any` addresses mypy issues where log calls are marked as # "None" being not callable. logger = None -crit = None -debug = None -error = None -info = None -warn = None +crit: Optional[Callable[[str], str]] = None +debug: Optional[Callable[[str], str]] = None +error: Optional[Callable[[str], str]] = None +info: Optional[Callable[[str], str]] = None +warn: Optional[Callable[[str], str]] = None init() From 74f9778482a2c743006e687cca6fb75776711f2e Mon Sep 17 00:00:00 2001 From: Michael Joyce Date: Tue, 24 Jan 2023 14:05:16 -0800 Subject: [PATCH 2/2] Issue #491 - Move logger globals for Python bug Shift the `log.py` logger globals to the beginning of the file to avoid a Python bug that causes spurious "annotated name can't be global" errors when mypy lints the files. See here for the issue report: https://bugs.python.org/issue34939 Clarification on the ticket marks this as a bug and a fix was added. This avoids the error in Python 3.7. --- ait/core/log.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/ait/core/log.py b/ait/core/log.py index d94faeb2..7bea5a87 100644 --- a/ait/core/log.py +++ b/ait/core/log.py @@ -39,6 +39,13 @@ logging.addLevelName(COMMAND, "COMMAND") logging.addLevelName(PROGRAM, "PROGRAM") +logger = None +crit: Optional[Callable[[str], str]] = None +debug: Optional[Callable[[str], str]] = None +error: Optional[Callable[[str], str]] = None +info: Optional[Callable[[str], str]] = None +warn: Optional[Callable[[str], str]] = None + class LogFormatter(logging.Formatter): """LogFormatter @@ -313,14 +320,4 @@ def notice(*args, **kwargs): logger.log(NOTICE, *args, **kwargs) -# These are "guaranteed" at runtime to be not-None. Marking this as -# type `Any` addresses mypy issues where log calls are marked as -# "None" being not callable. -logger = None -crit: Optional[Callable[[str], str]] = None -debug: Optional[Callable[[str], str]] = None -error: Optional[Callable[[str], str]] = None -info: Optional[Callable[[str], str]] = None -warn: Optional[Callable[[str], str]] = None - init()