fix: Add return type annotations for async-compatible methods#545
fix: Add return type annotations for async-compatible methods#545rodrigobnogueira wants to merge 1 commit intofgmacedo:developfrom
Conversation
|
|
Hey @rodrigo-nogueira, thanks for taking the time to put this together — I appreciate the effort and the focus on improving type safety! After reviewing the changes, I've decided not to merge this as-is. Here's my detailed feedback: Doesn't address #515The root cause of #515 (Pyright not detecting unresolved references on if TYPE_CHECKING:
def __getattr__(self, attribute: str) -> Any: ...This tells type checkers that any attribute exists and returns
|
|
Thanks @fgmacedo , you're right. I was about to revisit this one. Sorry about the mess. I was working in a project and we needed to make type checkers happy. Minimal reproduction: class MyMachine(StateMachine):
initial = State(initial=True)
final = State(final=True)
go = initial.to(final)
# This single async callback forces AsyncEngine
async def on_enter_final(self) -> None:
passasync def main() -> None:
sm = MyMachine()
await sm.activate_initial_state() # ← type error
await sm.send("go") # ← type error
assert sm.current_state == sm.finalType checkers (pyright, mypy) report: The root cause is that |
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.
fadfc88 to
b659163
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #545 +/- ##
===========================================
- Coverage 100.00% 99.77% -0.23%
===========================================
Files 25 25
Lines 1631 1758 +127
Branches 257 230 -27
===========================================
+ Hits 1631 1754 +123
- Misses 0 2 +2
- Partials 0 2 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Sorry for the messy history on this one. I've rebased the branch, dropped the Generic[TModel] commits and kept only the async type annotation fix for the minimal reproduction in the previous message. I still don't know if -> Any is the best solution, but it works and all tests pass. Feel free to close this if you'd prefer a different approach, and let me know if I can help with anything else. Thanks 🙏👍 |
|
Thanks @rodrigobnogueira , closing in favor of #566 |



Methods
activate_initial_state(),send(),_processing_loop(), andEvent.__call__()can return a coroutine whenAsyncEngineis active, but their type signatures did not reflect this, causing type checkers to reportType 'bool' is not awaitableerrors.What changed
Added
-> Anyreturn type annotation to:run_async_from_sync()inutils.pyactivate_initial_state()instatemachine.py_processing_loop()instatemachine.pysend()instatemachine.pyEvent.__call__()inevent.pyMinimal reproduction