asphalt icon indicating copy to clipboard operation
asphalt copied to clipboard

Asphalt v5 doesn't support resource of type Callable

Open davidbrochart opened this issue 1 year ago • 0 comments

With the upcoming version 5 of Asphalt, one cannot set the type of a resource to e.g. a typing.Callable anymore, while it worked fine with version 4.

  • With Asphalt v4:
from typing import Callable
from asphalt.core import CLIApplicationComponent, Component, Context, run_application

def resource() -> str:
    return "resource"

class Subcomponent0(Component):
    async def start(self, ctx: Context):
        ctx.add_resource(resource, types=Callable[[], str])

class Component0(CLIApplicationComponent):
    async def start(self, ctx: Context):
        self.add_component("subcomponent0", Subcomponent0)
        await super().start(ctx)

    async def run(self, ctx: Context):
        res = await ctx.request_resource(Callable[[], str])
        print(f"{res=}")

run_application(Component0())

The following is printed:

res=<function resource at 0x7f33fa7d1440>
  • With Asphalt v5:
from typing import Callable
from asphalt.core import Component, add_resource, get_resource, run_application

def resource() -> str:
    return "resource"

class Subcomponent0(Component):
    async def start(self):
        add_resource(resource, types=Callable[[], str])

class Component0(Component):
    def __init__(self):
        self.add_component("subcomponent0", Subcomponent0)

    async def start(self):
        res = await get_resource(Callable[[], str])
        print(f"{res=}")

run_application(Component0)

The following error is raised:

asphalt.core.ComponentStartError: error starting component 'subcomponent0' (__main__.Subcomponent0): TypeError: '_CallableGenericAlias' object is not iterable

davidbrochart avatar Dec 24 '24 13:12 davidbrochart