Skip to content

Commit e368f60

Browse files
committed
Use cached properties, string methods
1 parent c11c979 commit e368f60

3 files changed

Lines changed: 7 additions & 10 deletions

File tree

apywire/runtime.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import asyncio
1111
import importlib
1212
import re
13+
from functools import cached_property
1314
from operator import itemgetter
1415
from typing import Awaitable, Callable, Protocol, cast, final
1516

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

111-
@property
112+
@cached_property
112113
def aio(self) -> "AioAccessor":
113114
"""Return a wrapper object providing async accessors.
114115
115116
Use `await wired.aio.name()` to obtain the instantiated value
116117
asynchronously. We use `aio` to avoid the reserved keyword
117118
`async` (so `wired.async` would be invalid syntax).
118119
"""
119-
if not hasattr(self, "_aio_accessor"):
120-
self._aio_accessor = AioAccessor(self)
121-
return self._aio_accessor
120+
return AioAccessor(self)
122121

123122
def _instantiate_attr(
124123
self,

apywire/wiring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def _extract_placeholder_name(self, s: str) -> str:
289289
Returns:
290290
The placeholder name without braces
291291
"""
292-
return s[len(PLACEHOLDER_START) : -len(PLACEHOLDER_END)]
292+
return s.removeprefix(PLACEHOLDER_START).removesuffix(PLACEHOLDER_END)
293293

294294
def _resolve(self, obj: _SpecValue) -> _ResolvedValue:
295295
"""Resolve placeholders into `_WiredRef` markers for runtime.

tests/test_edge_cases.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,13 @@ def failing_maker() -> None:
195195

196196

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

201201
wired = Wiring({}, thread_safe=False)
202-
# Access internal attribute to verify it's not there yet
203-
assert not hasattr(wired, "_aio_accessor")
204-
# Access property
202+
# Access property (cached_property handles caching automatically)
205203
aio = wired.aio
206-
assert hasattr(wired, "_aio_accessor")
204+
# Verify same object is returned (caching works)
207205
assert wired.aio is aio
208206

209207

0 commit comments

Comments
 (0)