Skip to content

Commit 6d5fe41

Browse files
proboscisclaude
andcommitted
Add AsyncLock protocol for better type safety
Replace AbstractAsyncContextManager with a custom AsyncLock protocol to make the type annotations more specific to the requirements. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 68ad7c5 commit 6d5fe41

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

returns/primitives/reawaitable.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
from collections.abc import Awaitable, Callable, Generator
2-
from contextlib import AbstractAsyncContextManager
32
from functools import wraps
4-
from typing import NewType, ParamSpec, TypeVar, cast, final
3+
from typing import NewType, ParamSpec, Protocol, TypeVar, cast, final
4+
5+
6+
class AsyncLock(Protocol):
7+
"""A protocol for an asynchronous lock."""
8+
9+
async def __aenter__(self) -> None:
10+
...
11+
12+
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
13+
...
14+
515

616
# Try to use anyio.Lock, fall back to asyncio.Lock
717
# Note: anyio is required for proper trio support
@@ -10,9 +20,9 @@
1020
except ImportError: # pragma: no cover
1121
import asyncio # noqa: WPS433
1222

13-
Lock: AbstractAsyncContextManager = asyncio.Lock
23+
Lock: AsyncLock = asyncio.Lock
1424
else:
15-
Lock: AbstractAsyncContextManager = anyio.Lock
25+
Lock: AsyncLock = anyio.Lock
1626

1727
_ValueType = TypeVar('_ValueType')
1828
_AwaitableT = TypeVar('_AwaitableT', bound=Awaitable)

0 commit comments

Comments
 (0)