Skip to content

Commit b659163

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 9a089ed commit b659163

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
@@ -102,13 +102,13 @@ def _get_engine(self, rtc: bool):
102102

103103
return SyncEngine(self, rtc=rtc)
104104

105-
def activate_initial_state(self):
105+
def activate_initial_state(self) -> Any:
106106
result = self._engine.activate_initial_state()
107107
if not isawaitable(result):
108108
return result
109109
return run_async_from_sync(result)
110110

111-
def _processing_loop(self):
111+
def _processing_loop(self) -> Any:
112112
return self._engine.processing_loop()
113113

114114
def __init_subclass__(cls, strict_states: bool = False):
@@ -298,7 +298,7 @@ def _put_nonblocking(self, trigger_data: TriggerData):
298298
"""Put the trigger on the queue without blocking the caller."""
299299
self._engine.put(trigger_data)
300300

301-
def send(self, event: str, *args, **kwargs):
301+
def send(self, event: str, *args, **kwargs) -> Any:
302302
"""Send an :ref:`Event` to the state machine.
303303
304304
.. 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)