Hello <@ULVA73B9P> , is it ok to call a function d...
# ask-marvin
j
Hello @Marvin , is it ok to call a function decorated
sync_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
.
n
mypy and sync_compatible are unfortunately not good friends
j
that's what I figured lol , thank you Nate 🙏 i will type ignore it 😄
n
👍
catjam 1
m
I've found some relevant issues on the PrefectHQ GitHub that might shed some light on your situation with the
sync_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.