Skip to content

Commit fadfc88

Browse files
author
rodrigo.nogueira
committed
fix: Add return type annotations for async-compatible methods
Methods activate_initial_state(), send(), _processing_loop(), and Event.__call__() can return a coroutine when AsyncEngine is active, but their type signatures did not reflect this, causing type checkers to report 'Type bool is not awaitable' errors. Add -> Any return type to these methods and to run_async_from_sync() so type checkers allow await on the return values.
1 parent 62e99aa commit fadfc88

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

statemachine/event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from inspect import isawaitable
22
from typing import TYPE_CHECKING
3+
from typing import Any
34
from typing import List
45
from uuid import uuid4
56

@@ -108,7 +109,7 @@ def __get__(self, instance, owner):
108109
return self
109110
return BoundEvent(id=self.id, name=self.name, _sm=instance)
110111

111-
def __call__(self, *args, **kwargs):
112+
def __call__(self, *args, **kwargs) -> Any:
112113
"""Send this event to the current state machine.
113114
114115
Triggering an event on a state machine means invoking or sending a signal, initiating the

statemachine/statemachine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ def _get_engine(self, rtc: bool):
104104

105105
return SyncEngine(self, rtc=rtc)
106106

107-
def activate_initial_state(self):
107+
def activate_initial_state(self) -> Any:
108108
result = self._engine.activate_initial_state()
109109
if not isawaitable(result):
110110
return result
111111
return run_async_from_sync(result)
112112

113-
def _processing_loop(self):
113+
def _processing_loop(self) -> Any:
114114
return self._engine.processing_loop()
115115

116116
def __init_subclass__(cls, strict_states: bool = False):
@@ -307,7 +307,7 @@ def _put_nonblocking(self, trigger_data: TriggerData):
307307
"""Put the trigger on the queue without blocking the caller."""
308308
self._engine.put(trigger_data)
309309

310-
def send(self, event: str, *args, **kwargs):
310+
def send(self, event: str, *args, **kwargs) -> Any:
311311
"""Send an :ref:`Event` to the state machine.
312312
313313
.. seealso::

statemachine/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import threading
3+
from typing import Any
34

45
_cached_loop = threading.local()
56
"""Loop that will be used when the SM is running in a synchronous context. One loop per thread."""
@@ -25,7 +26,7 @@ def ensure_iterable(obj):
2526
return [obj]
2627

2728

28-
def run_async_from_sync(coroutine):
29+
def run_async_from_sync(coroutine: Any) -> Any:
2930
"""
3031
Compatibility layer to run an async coroutine from a synchronous context.
3132
"""

0 commit comments

Comments
 (0)