Skip to content

Commit

Permalink
fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
biniona committed Oct 2, 2024
1 parent 77af2de commit 234b9c9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions minject/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def wrap(cls: Type[T]) -> Type[T]:

def async_context(cls: Type[T_async_context]) -> Type[T_async_context]:
"""
Decorator to declare that a class is as an async context manager
Declare that a class is as an async context manager
that can be initialized by the registry through aget(). This
is to distinguish the class from an async context manager that
should not be initialized by the registry (an example of
this being asyncio.Lock).
"""
meta = _gen_meta(cls)
meta.update_async_context(True)
meta.is_async_context = True
return cls


Expand Down Expand Up @@ -126,13 +126,13 @@ def _is_key_async(key: "RegistryKey[T]") -> bool:
if isinstance(key, str):
return False
elif isinstance(key, RegistryMetadata):
return key.is_async_context()
return key.is_async_context
else:
assert_type(key, type)
assert_type(key, Type[T])
inject_metadata = _get_meta(key)
if inject_metadata is None:
return False
return inject_metadata.is_async_context()
return inject_metadata.is_async_context


class _RegistryReference(Deferred[T_co]):
Expand Down
14 changes: 8 additions & 6 deletions minject/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,20 @@ def update_bindings(self, **bindings: DeferredAny) -> None:
# TODO: 'lock' the bindings once added to the registry to make above note unnecessary
self._bindings.update(bindings)

def update_async_context(self, is_async_context: bool) -> None:
"""
Set the async context flag for this metadata.
"""
self._is_async_context = is_async_context

@property
def is_async_context(self) -> bool:
"""
Returns the value of the async context flag for this metadata.
"""
return self._is_async_context

@is_async_context.setter
def is_async_context(self, is_async_context: bool) -> None:
"""
Set the async context flag for this metadata.
"""
self._is_async_context = is_async_context

def _new_object(self) -> T_co:
return self._cls.__new__(self._cls)

Expand Down
2 changes: 1 addition & 1 deletion minject/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ async def _aregister_by_metadata(self, meta: RegistryMetadata[T]) -> RegistryWra

# after creating an object, enter the objects context
# if it is marked with the @async_context decorator.
if meta.is_async_context():
if meta.is_async_context:
await self._push_async_context(obj)

success = True
Expand Down

0 comments on commit 234b9c9

Please sign in to comment.