Skip to content

Commit e96e7ce

Browse files
committed
chore: add protocol
1 parent 08af059 commit e96e7ce

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/zarr/storage/_utils.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import importlib
77
import re
88
from pathlib import Path
9-
from typing import TYPE_CHECKING, Any, ParamSpec, TypeVar
9+
from typing import TYPE_CHECKING, Any, ParamSpec, Protocol, TypeVar
1010

1111
from zarr.abc.store import OffsetByteRequest, RangeByteRequest, SuffixByteRequest
1212

@@ -29,6 +29,12 @@ class UPath: # type: ignore[no-redef]
2929
T_co = TypeVar("T_co", covariant=True)
3030

3131

32+
class HasConcurrencyLimit(Protocol):
33+
"""Protocol for objects that support semaphore-based concurrency limiting."""
34+
35+
def _limit(self) -> asyncio.Semaphore | contextlib.nullcontext[None]: ...
36+
37+
3238
class ConcurrencyLimiter:
3339
"""Mixin that adds a semaphore-based concurrency limit to a store.
3440
@@ -82,8 +88,8 @@ async def get(self, key, prototype, byte_range=None):
8288

8389
@functools.wraps(func)
8490
async def wrapper(*args: P.args, **kwargs: P.kwargs) -> T_co:
85-
self = args[0]
86-
async with self._limit(): # type: ignore[attr-defined]
91+
self: HasConcurrencyLimit = args[0] # type: ignore[assignment]
92+
async with self._limit():
8793
return await func(*args, **kwargs)
8894

8995
return wrapper

0 commit comments

Comments
 (0)