Jack P
04/12/2024, 6:45 PMsync_compatible
from prefect.utilities.asyncutils
within a sync function? Just asking as MyPy is alerting me that is wrong, but I think MyPy might be not recognizing sync_compatible
.Nate
04/12/2024, 6:46 PMJack P
04/12/2024, 6:46 PMNate
04/12/2024, 6:46 PMMarvin
04/12/2024, 6:46 PMsync_compatible
decorator. Let's go through them:
1. Make the sync_compatible
decorator work with type-checkers (Issue #9688):
- The issue addresses the problem of type checkers not liking the sync_compatible
decorator. It discusses that the dynamic behavior of sync_compatible
functions/methods might never be fully understood by type checkers. The issue proposes a solution involving the use of a Protocol
that allows marking code for synchronous use that avoids type checker complaints.
2. sync_compatible
async/sync context detection error (Issue #6677):
- This bug report indicates a problem with the sync_compatible
decorator where it incorrectly detects an async context when using pytest-asyncio for tests. This results in a runtime error.
3. Calling sync_compatible
functions in Jupyter notebooks (Issue #7722):
- This issue explains that sync_compatible
decorated functions that run fine in .py scripts and ipython environments require an await
in Jupyter notebooks which is inconsistent and can cause some inconvenience.
From these discussions, it's clear that using sync_compatible
can be a bit tricky with type checkers like MyPy, and there seem to be certain cases where it doesn't behave as expected. Unfortunately, I could not find definitive documentation on using sync_compatible
within synchronous functions that would solve your MyPy issue directly.
However, I suggest giving a careful read to these GitHub issues as they contain detailed discussions and proposed workarounds that might help you resolve the warnings raised by MyPy until a solution is implemented by the Prefect team.