diff --git a/apywire/runtime.py b/apywire/runtime.py index 2b1f48f..ee92911 100644 --- a/apywire/runtime.py +++ b/apywire/runtime.py @@ -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 @@ -108,7 +109,7 @@ 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. @@ -116,9 +117,7 @@ def aio(self) -> "AioAccessor": 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, diff --git a/apywire/wiring.py b/apywire/wiring.py index d038090..e70b3dd 100644 --- a/apywire/wiring.py +++ b/apywire/wiring.py @@ -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. diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py index ef28b93..1be3e3e 100644 --- a/tests/test_edge_cases.py +++ b/tests/test_edge_cases.py @@ -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