Kyle McChesney
03/17/2025, 9:37 PMVariable.get
supposed to be async? If I just call Variable.get('name')
and then try and use the value (which is a dictionary) I get: TypeError: 'coroutine' object is not subscriptable
. If I add the await
on the .get
call it works, but I get a typing error in pyright as it does not claim to be awaitable in the type hinting.Marvin
03/17/2025, 9:38 PMVariable.get
method in Prefect is indeed designed to be compatible with both synchronous and asynchronous contexts due to the use of the @sync_compatible
decorator. This means that if you call it in a synchronous context, it should behave like a regular function, but if you call it in an asynchronous context, you need to await
it.
Here's the catch: the type hinting for Variable.get
has been reported to be incorrect. It suggests that the method is not awaitable, which leads to confusion when using type checkers like Pyright. This is a known issue and has been discussed in GitHub issues.
For now, if you're using it in an asynchronous context, you should continue to use await Variable.get(...)
, and you might need to ignore the type checker warnings until the type hints are corrected in future releases.
For further details, please refer to the related GitHub issue and the documentation on this topic.Nate
03/17/2025, 9:38 PMaget
more context: https://github.com/PrefectHQ/prefect/issues/15008Kyle McChesney
03/17/2025, 9:40 PMaget
. Is the long term plan to still make .get
async if the inferred context is async?Nate
03/17/2025, 9:41 PMthis ^ is how it has been, which has caused a lot of confusion over time for now, if you want to opt out of the dual contextual behavior, you can passasync if the inferred context is async?.get
_sync=true
to the get methodKyle McChesney
03/17/2025, 9:42 PM.aget
is calledNate
03/17/2025, 9:43 PMSerina G
03/17/2025, 9:58 PMNate
03/17/2025, 10:01 PMaget
in this case) which means you need to be on a version where that's implemented
things should be type safe once we've finished then explicit methods and then pass through the deprecation window (after which we'll totally remove sync_compatible)Serina G
03/17/2025, 10:04 PM