Fix type annots in resource cacher internals

context_caching
Tyler Goodlet 2021-11-28 12:48:26 -05:00
parent 5f41dbf34f
commit ac22b4a875
1 changed files with 4 additions and 5 deletions

View File

@ -23,6 +23,7 @@ from typing import (
Any, Any,
AsyncContextManager, AsyncContextManager,
AsyncGenerator, AsyncGenerator,
AsyncIterator,
Hashable, Hashable,
Optional, Optional,
Sequence, Sequence,
@ -122,7 +123,7 @@ class cache:
values: dict[Any, Any] = {} values: dict[Any, Any] = {}
resources: dict[ resources: dict[
int, int,
Optional[tuple[trio.Nursery, trio.Event]] tuple[trio.Nursery, trio.Event]
] = {} ] = {}
no_more_users: Optional[trio.Event] = None no_more_users: Optional[trio.Event] = None
@ -153,7 +154,7 @@ async def maybe_open_context(
key: Hashable, key: Hashable,
mngr: AsyncContextManager[T], mngr: AsyncContextManager[T],
) -> (bool, T): ) -> AsyncIterator[tuple[bool, T]]:
''' '''
Maybe open a context manager if there is not already a cached Maybe open a context manager if there is not already a cached
version for the provided ``key``. Return the cached instance on version for the provided ``key``. Return the cached instance on
@ -186,9 +187,7 @@ async def maybe_open_context(
service_n = current_actor()._service_n service_n = current_actor()._service_n
# TODO: does this need to be a tractor "root nursery"? # TODO: does this need to be a tractor "root nursery"?
ln = cache.resources.get(ctx_key) assert not cache.resources.get(ctx_key), f'Resource exists? {ctx_key}'
assert not ln
ln, _ = cache.resources[ctx_key] = (service_n, trio.Event()) ln, _ = cache.resources[ctx_key] = (service_n, trio.Event())
value = await ln.start(cache.run_ctx, mngr, key) value = await ln.start(cache.run_ctx, mngr, key)