Skip to content

Commit

Permalink
Merge branch '5.0' into inject-traceback
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm authored May 5, 2024
2 parents 2ca27d6 + 1839835 commit 60bf69d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/asphalt/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from ._concurrent import start_background_task_factory as start_background_task_factory
from ._concurrent import start_service_task as start_service_task
from ._context import Context as Context
from ._context import GeneratedResource as GeneratedResource
from ._context import ResourceEvent as ResourceEvent
from ._context import add_resource as add_resource
from ._context import add_resource_factory as add_resource_factory
Expand Down
36 changes: 6 additions & 30 deletions src/asphalt/core/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,6 @@ def add_resource_factory(
)

origin = get_origin(return_type_hint)
if origin is GeneratedResource:
args = get_args(return_type_hint)
if len(args) != 1:
raise ValueError(
f"GeneratedResource must specify exactly one parameter, got "
f"{len(args)}"
)

return_type_hint = args[0]

if origin is Union or (
sys.version_info >= (3, 10) and origin is stdlib_types.UnionType
):
Expand Down Expand Up @@ -509,20 +499,13 @@ def get_resource_nowait(
if key in ctx._resource_factories:
# Call the factory callback to generate the resource
factory = ctx._resource_factories[key]
retval = factory.value_or_factory()
resource = factory.value_or_factory()

# Raise AsyncResourceError if the factory returns a coroutine object
if iscoroutine(retval):
retval.close()
if iscoroutine(resource):
resource.close()
raise AsyncResourceError()

if isinstance(retval, GeneratedResource):
resource = retval.resource
if retval.teardown_callback is not None:
self.add_teardown_callback(retval.teardown_callback)
else:
resource = retval

# Store the generated resource in the context
container = ResourceContainer(
resource, factory.types, factory.name, factory.description, False
Expand Down Expand Up @@ -606,16 +589,9 @@ async def get_resource(
for ctx in self.context_chain:
if key in ctx._resource_factories:
factory = ctx._resource_factories[key]
retval = factory.value_or_factory()
if isawaitable(retval):
retval = await retval

if isinstance(retval, GeneratedResource):
resource = retval.resource
if retval.teardown_callback is not None:
self.add_teardown_callback(retval.teardown_callback)
else:
resource = retval
resource = factory.value_or_factory()
if isawaitable(resource):
resource = await resource

container = ResourceContainer(
resource, factory.types, factory.name, factory.description, False
Expand Down

0 comments on commit 60bf69d

Please sign in to comment.