diff --git a/dank_mids/semaphores.py b/dank_mids/semaphores.py index 545d2cf0..58951403 100644 --- a/dank_mids/semaphores.py +++ b/dank_mids/semaphores.py @@ -1,6 +1,6 @@ import functools from decimal import Decimal -from typing import TYPE_CHECKING, Literal, Optional, Union +from typing import TYPE_CHECKING, Literal, Optional, Type, Union import a_sync from a_sync.primitives import DummySemaphore, ThreadsafeSemaphore @@ -8,6 +8,7 @@ _AbstractPrioritySemaphore, _PrioritySemaphoreContextManager, ) +from eth_typing import HexStr from web3.types import RPCEndpoint if TYPE_CHECKING: @@ -36,7 +37,12 @@ def __init__( super().__init__(parent, priority, name) -class BlockSemaphore(_AbstractPrioritySemaphore[str, _BlockSemaphoreContextManager]): # type: ignore [type-var] +_TOP_PRIORITY = -1 + + +# NOTE: keep this so we can include in type stubs +# class BlockSemaphore(_AbstractPrioritySemaphore[str, _BlockSemaphoreContextManager]): # type: ignore [type-var] +class BlockSemaphore(_AbstractPrioritySemaphore): """A semaphore for managing concurrency based on block numbers. This class extends :class:`_AbstractPrioritySemaphore` to provide block-specific concurrency control. @@ -49,34 +55,28 @@ class BlockSemaphore(_AbstractPrioritySemaphore[str, _BlockSemaphoreContextManag :class:`_BlockSemaphoreContextManager`: The context manager used by this semaphore. """ - _context_manager_class = _BlockSemaphoreContextManager # type: ignore [assignment] + _context_manager_class: Type[_BlockSemaphoreContextManager] """The context manager class used by this semaphore.""" - _top_priority: int = -1 # type: ignore [assignment] + _top_priority: Literal[-1] """The highest priority value, set to -1.""" - def __getitem__(self, block: Union[int, str, Literal["latest", None]]) -> "_BlockSemaphoreContextManager": # type: ignore [override] - return super().__getitem__( # type: ignore [return-value] - block - if isinstance(block, int) # type: ignore [index] - else ( - int(block.hex(), 16) - if isinstance(block, bytes) # type: ignore [union-attr] - else ( - int(block, 16) - if isinstance(block, str) and "0x" in block - else ( - block - if block - not in [ - None, - "latest", - ] # NOTE: We do this to generate an err if an unsuitable value was provided - else self._top_priority - ) - ) - ) - ) # type: ignore [index] + def __init__(self, value=1, *, name=None): + super().__init__(_BlockSemaphoreContextManager, -1, value, name=name) + + def __getitem__(self, block: Union[int, HexStr, Literal["latest", None]]) -> "_BlockSemaphoreContextManager": # type: ignore [override] + if isinstance(block, int): + priority = block + elif isinstance(block, bytes): + priority = int(block.hex(), 16) + elif isinstance(block, str) and "0x" in block: + priority = int(block, 16) + elif block not in [None, "latest"]: + # NOTE: We do this to generate an err if an unsuitable value was provided + priority = block + else: + priority = _TOP_PRIORITY + return super().__getitem__(priority) class _MethodSemaphores: diff --git a/poetry.lock b/poetry.lock index 7a55a25e..bf27f61e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2092,12 +2092,12 @@ testing = ["hatch", "pre-commit", "pytest", "tox"] [[package]] name = "ez-a-sync" -version = "0.23.5" +version = "0.24.7" description = "A library that makes it easy to define objects that can be used for both sync and async use cases." optional = false python-versions = "<3.13,>=3.8" files = [ - {file = "ez_a_sync-0.23.5.tar.gz", hash = "sha256:ce5e55df4037456649236926e5388e0c3895afa8b7afcd432cacd1a7b468c64e"}, + {file = "ez_a_sync-0.24.7.tar.gz", hash = "sha256:b4acc2c238358b372d42d994e4e5a280a88089f3a000ebf15e1c59ea5afef0e3"}, ] [package.dependencies] @@ -4328,13 +4328,13 @@ telegram = ["requests"] [[package]] name = "typed-envs" -version = "0.0.4" +version = "0.0.5" description = "typed_envs is used to create specialized EnvironmentVariable objects that behave exactly the same as any other instance of the `typ` used to create them." optional = false python-versions = "*" files = [ - {file = "typed-envs-0.0.4.tar.gz", hash = "sha256:5a71ec50cc1b274c39455885d276a97ae0000eecf0a1d1023bdc5f698fccb6fe"}, - {file = "typed_envs-0.0.4-py3-none-any.whl", hash = "sha256:f995ecbccff283ed6579f33187ea253d9d37fdd652866ec312b8ccc508d4c07b"}, + {file = "typed_envs-0.0.5-py3-none-any.whl", hash = "sha256:856ad258fd3584f3418c84b2cd3dc14c0c13e504fce1b7b48ff07fb05dc509d4"}, + {file = "typed_envs-0.0.5.tar.gz", hash = "sha256:5432b9800bfef965b37204ca946d864a29fa072b9560faf1cc7de728c3a00105"}, ] [[package]] @@ -5096,4 +5096,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.0" python-versions = ">=3.8,<3.13" -content-hash = "bc317e7e17b80daabe702245d69851ff30dc08a9193d857a28e4c8b6d11453b4" +content-hash = "4e2375fd9c68bb7e48d30504630c33bf4498fa55e1bada5ea11a360528bd4253" diff --git a/pyproject.toml b/pyproject.toml index c413db09..5de3afd0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,9 +14,9 @@ python = ">=3.8,<3.13" aiofiles = "*" eth-retry = ">=0.1.15,<0.2" evmspec = ">=0.0.1,<0.1" -ez-a-sync = ">=0.20.7,<0.25" +ez-a-sync = ">=0.24.7,<1" multicall = ">=0.6.2,<1" -typed-envs = ">=0.0.2,<0.1" +typed-envs = ">=0.0.5,<0.1" web3 = ">=5.27,!=5.29.*,!=5.30.*,!=5.31.1,!=5.31.2,<8" [tool.poetry.group.dev.dependencies]