Skip to content

Commit

Permalink
changelog + version + refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
biniona committed Oct 1, 2024
1 parent 22c73fb commit f00b433
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
18 changes: 18 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion minject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions minject/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion minject/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions minject/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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.
"""
...

@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
Expand Down
11 changes: 7 additions & 4 deletions minject/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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":
"""
Expand Down
1 change: 1 addition & 0 deletions minject/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
...

Expand Down

0 comments on commit f00b433

Please sign in to comment.