Skip to content

Commit 4068dd3

Browse files
author
Jack Doughty
committed
Ruff/pyright adapters logging devices.init
1 parent 31a2a12 commit 4068dd3

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

lewis/core/adapters.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"""
2525

2626
import inspect
27+
import logging
2728
import threading
2829
from collections import namedtuple
2930
from types import TracebackType
@@ -237,6 +238,7 @@ def __init__(self, *args: Adapter) -> None:
237238
self._threads = {}
238239
self._running = {}
239240
self._lock = threading.Lock()
241+
self.log: logging.Logger
240242

241243
for adapter in args:
242244
self.add_adapter(adapter)
@@ -301,7 +303,7 @@ def connect(self, *args: str) -> None:
301303

302304
def _start_server(self, adapter: Adapter) -> None:
303305
if adapter.protocol not in self._threads:
304-
self.log.info("Connecting device interface for protocol '%s'", adapter.protocol) # type: ignore not sure?
306+
self.log.info("Connecting device interface for protocol '%s'", adapter.protocol)
305307

306308
adapter_thread = threading.Thread(target=self._adapter_loop, args=(adapter, 0.01))
307309
adapter_thread.daemon = True
@@ -322,7 +324,7 @@ def _adapter_loop(self, adapter: Adapter, dt: float) -> None:
322324

323325
self._running[adapter.protocol].set()
324326

325-
self.log.debug("Starting adapter loop for protocol %s.", adapter.protocol) # type: ignore not sure?
327+
self.log.debug("Starting adapter loop for protocol %s.", adapter.protocol)
326328
while self._running[adapter.protocol].is_set():
327329
adapter.handle(dt)
328330

@@ -340,7 +342,7 @@ def disconnect(self, *args: str) -> None:
340342

341343
def _stop_server(self, adapter: Adapter) -> None:
342344
if adapter.protocol in self._threads:
343-
self.log.info("Disconnecting device interface for protocol '%s'", adapter.protocol) # type: ignore not sure?
345+
self.log.info("Disconnecting device interface for protocol '%s'", adapter.protocol)
344346

345347
self._running[adapter.protocol].clear()
346348
self._threads[adapter.protocol].join()

lewis/core/logging.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,26 @@
3333
"""
3434

3535
import logging
36+
from typing import Callable, overload, ParamSpec, TypeVar, Type, Protocol
37+
38+
39+
class HasLog(Protocol):
40+
log: logging.Logger
41+
42+
43+
P = ParamSpec("P")
44+
T = TypeVar("T")
3645

3746
root_logger_name = "lewis"
3847
default_log_format = "%(asctime)s %(levelname)s %(name)s: %(message)s"
3948

4049

50+
@overload
51+
def has_log(target: Type[T]) -> Type[T]: ...
52+
@overload
53+
def has_log(target: Callable[P, T]) -> Callable[P, T]: ...
54+
55+
4156
def has_log(target):
4257
"""
4358
This is a decorator to add logging functionality to a class or function.
@@ -95,15 +110,15 @@ def foo(bar):
95110
"""
96111
logger_name = target.__name__
97112

98-
def get_logger_name(context=None):
113+
def get_logger_name(context: object = None) -> str:
99114
log_names = [root_logger_name, logger_name]
100115

101116
if context is not None:
102117
log_names.insert(1, context if isinstance(context, str) else context.__class__.__name__)
103118

104119
return ".".join(log_names)
105120

106-
def _set_logging_context(obj, context):
121+
def _set_logging_context(obj: HasLog, context: object) -> None:
107122
"""
108123
Changes the logger name of this class using the supplied context
109124
according to the rules described in the documentation of :func:`has_log`. To

lewis/devices/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using a state machine.
2424
"""
2525

26+
import logging
2627
from typing import Callable
2728

2829
from lewis.core.devices import DeviceBase
@@ -103,7 +104,8 @@ def __init__(
103104
) -> None:
104105
super(StateMachineDevice, self).__init__()
105106

106-
self.log.info("Creating device, setting up state machine") # type: ignore not sure?
107+
self.log: logging.Logger
108+
self.log.info("Creating device, setting up state machine")
107109

108110
self._initialize_data()
109111
self._override_data(override_initial_data)
@@ -193,7 +195,7 @@ def _override_data(self, overrides: dict[str, float] | None) -> None:
193195
"""
194196
if overrides is not None:
195197
for name, val in overrides.items():
196-
self.log.debug("Trying to override initial data (%s=%s)", name, val) # type: ignore not sure?
198+
self.log.debug("Trying to override initial data (%s=%s)", name, val)
197199
if name not in dir(self):
198200
raise AttributeError(
199201
"Can not override non-existing attribute" "'{}' of class '{}'.".format(

0 commit comments

Comments
 (0)