Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions apywire/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import asyncio
import importlib
import re
from functools import cached_property
from operator import itemgetter
from typing import Awaitable, Callable, Protocol, cast, final

Expand Down Expand Up @@ -108,17 +109,15 @@ def __getattr__(self, name: str) -> Accessor:
f"'{type(self).__name__}' object has no attribute '{name}'"
)

@property
@cached_property
def aio(self) -> "AioAccessor":
"""Return a wrapper object providing async accessors.

Use `await wired.aio.name()` to obtain the instantiated value
asynchronously. We use `aio` to avoid the reserved keyword
`async` (so `wired.async` would be invalid syntax).
"""
if not hasattr(self, "_aio_accessor"):
self._aio_accessor = AioAccessor(self)
return self._aio_accessor
return AioAccessor(self)

def _instantiate_attr(
self,
Expand Down
2 changes: 1 addition & 1 deletion apywire/wiring.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _extract_placeholder_name(self, s: str) -> str:
Returns:
The placeholder name without braces
"""
return s[len(PLACEHOLDER_START) : -len(PLACEHOLDER_END)]
return s.removeprefix(PLACEHOLDER_START).removesuffix(PLACEHOLDER_END)

def _resolve(self, obj: _SpecValue) -> _ResolvedValue:
"""Resolve placeholders into `_WiredRef` markers for runtime.
Expand Down
8 changes: 3 additions & 5 deletions tests/test_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,13 @@ def failing_maker() -> None:


def test_wiring_aio_lazy_init() -> None:
"""Test lazy initialization of aio property."""
"""Test initialization of aio accessor with @cached_property."""
from apywire import Wiring

wired = Wiring({}, thread_safe=False)
# Access internal attribute to verify it's not there yet
assert not hasattr(wired, "_aio_accessor")
# Access property
# Access property (cached_property handles caching automatically)
aio = wired.aio
assert hasattr(wired, "_aio_accessor")
# Verify same object is returned (caching works)
assert wired.aio is aio
Comment thread
alganet marked this conversation as resolved.


Expand Down