https://prefect.io logo
Title
a

Alejandro

10/24/2022, 4:49 PM
Is it possible to load a
Block
inside the class in which another
Block
is defined using the Python API?
class Foo(Block):

    target_name: str
    _block_type_slug = "foo-block"

    def block_initialization(self) -> None:
        bar_block = JSON.load("bar-block").value
        another_block = JSON.load(target_name).value
I have tried with the above code but I get the following warning, which results in a coroutine being returned instead of a block:
RuntimeWarning: coroutine 'Block.load' was never awaited
a

Andrew Huang

10/24/2022, 5:05 PM
maybe:
async def block_initialization(self) -> None:
        bar_block = await JSON.load("bar-block").value
        another_block = await JSON.load(target_name).value
but maybe it’ll just have another coroutine output since https://github.com/PrefectHQ/prefect/blob/ab5310827e88124538d8e9e5e9b63c427edd5d28/src/prefect/blocks/core.py#L176 is not async perhaps you can try to use sync()? https://github.com/PrefectHQ/prefect/blob/main/src/prefect/utilities/asyncutils.py#L221
a

Alejandro

10/31/2022, 8:54 AM
Hi @Andrew Huang. Sorry for the late response. I finally managed to approach the solution without the need for nested block definition. Thanks anyway!
1