diff --git a/CHANGES.md b/CHANGES.md new file mode 100644 index 0000000..9f79da8 --- /dev/null +++ b/CHANGES.md @@ -0,0 +1,18 @@ +# Versioning + +`minject` uses semantic versioning. To learn more about semantic versioning, see the [semantic versioning specification](https://semver.org/#semantic-versioning-200). + +# Changelog + +## v1.1.0 + +Add support for async Python. This version introduces the following methods and decorators: + +- `Registry.__aenter__` +- `Registry.__aexit__` +- `Registry.aget` +- `@async_context` + +## v1.0.0 + +- Initial Release diff --git a/minject/__init__.py b/minject/__init__.py index fbd7319..f61615e 100644 --- a/minject/__init__.py +++ b/minject/__init__.py @@ -44,7 +44,7 @@ def __init__(self, api, path): ... something = registry['something_i_need'] """ -__version__ = "1.0.0" +__version__ = "1.1.0" from . import inject from .inject_attrs import inject_define as define, inject_field as field diff --git a/minject/inject.py b/minject/inject.py index 59ef529..cb3948f 100644 --- a/minject/inject.py +++ b/minject/inject.py @@ -150,8 +150,8 @@ def resolve(self, registry_impl: Resolver) -> T_co: async def aresolve(self, registry_impl: Resolver) -> T_co: if _is_key_async(self._key): - result = await registry_impl.aresolve(self._key) - await registry_impl.push_async_context(result) + result = await registry_impl._aresolve(self._key) + await registry_impl._push_async_context(result) return result return registry_impl.resolve(self._key) diff --git a/minject/metadata.py b/minject/metadata.py index e07c481..f8de427 100644 --- a/minject/metadata.py +++ b/minject/metadata.py @@ -179,7 +179,7 @@ async def _ainit_object(self, obj: T_co, registry_impl: "Registry") -> None: # """ init_kwargs = {} for name_, value in self._bindings.items(): - init_kwargs[name_] = await registry_impl._aresolve(value) + init_kwargs[name_] = await registry_impl._aresolve_resolvable(value) self._cls.__init__(obj, **init_kwargs) def _close_object(self, obj: T_co) -> None: # type: ignore[misc] diff --git a/minject/model.py b/minject/model.py index f05ec6d..33bdd31 100644 --- a/minject/model.py +++ b/minject/model.py @@ -35,7 +35,7 @@ def resolve(self, key: "RegistryKey[T]") -> T: ... @abc.abstractmethod - async def aresolve(self, key: "RegistryKey[T]") -> T: + async def _aresolve(self, key: "RegistryKey[T]") -> T: """ Resolve a key into an instance of that key. The key must be marked with the @async_context decorator. @@ -43,7 +43,7 @@ async def aresolve(self, key: "RegistryKey[T]") -> T: ... @abc.abstractmethod - async def push_async_context(self, key: Any) -> Any: + async def _push_async_context(self, key: Any) -> Any: """ Push an async context onto the context stack maintained by the Resolver. This is necessary to enter/close the context of an object diff --git a/minject/registry.py b/minject/registry.py index a642d47..925d9af 100644 --- a/minject/registry.py +++ b/minject/registry.py @@ -102,13 +102,13 @@ def config(self) -> RegistryConfigWrapper: def resolve(self, key: "RegistryKey[T]") -> T: return self[key] - async def aresolve(self, key: "RegistryKey[T]") -> T: + async def _aresolve(self, key: "RegistryKey[T]") -> T: result = await self._aget(key) if result is None: raise KeyError(key, "could not be resolved") return result - async def push_async_context(self, key: Any) -> Any: + async def _push_async_context(self, key: Any) -> Any: result = await self._async_context_stack.enter_async_context(key) if result is not key: raise ValueError( @@ -125,7 +125,10 @@ async def push_async_context(self, key: Any) -> Any: def _resolve(self, value: Resolvable[T]) -> T: return resolve_value(self, value) - async def _aresolve(self, value: Resolvable[T]) -> T: + async def _aresolve_resolvable(self, value: Resolvable[T]) -> T: + """ + Async version of _resolve. + """ return await aresolve_value(self, value) @_synchronized @@ -387,7 +390,7 @@ async def aget(self, key: "RegistryKey[T]") -> Optional[T]: # requires that the top level context of key be entered, and contexts are entered # in a RegistryReference's aresolve method reference = _RegistryReference(key) - return await self._aresolve(reference) + return await self._aresolve_resolvable(reference) async def __aenter__(self) -> "Registry": """ diff --git a/minject/types.py b/minject/types.py index 5f10cf9..4153f2c 100644 --- a/minject/types.py +++ b/minject/types.py @@ -29,6 +29,7 @@ class _AsyncContext(Protocol): """ Protocol for any object that can be used as an async context manager. """ + async def __aenter__(self: Self) -> Self: ...