Skip to content

Commit

Permalink
chore: fix sourcery errors in singleton (#860)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 16, 2024
1 parent 20ae13e commit c0f66dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
22 changes: 12 additions & 10 deletions y/classes/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ def __call__(cls, address: AnyAddressOrContract, *args, **kwargs) -> T: # type:
instance = super().__call__(address, *args, **kwargs)
cls.__instances[is_sync][address] = instance
cls.__delete_address_lock(address, is_sync)
assert (
instance.asynchronous is not is_sync
), f"You must initialize your objects with 'asynchronous' specified as a kwarg, not a positional arg. {instance} {kwargs} {is_sync} {instance.asynchronous} {args} {kwargs}"
if instance.asynchronous is is_sync:
raise RuntimeError(
"You must initialize your objects with 'asynchronous' specified as a kwarg, not a positional arg. "
+ f"{instance} {kwargs} {is_sync} {instance.asynchronous} {args} {kwargs}"
)
return instance

def __get_address_lock(
cls, address: AnyAddressOrContract, is_sync: bool
self, address: AnyAddressOrContract, is_sync: bool
) -> threading.Lock:
"""
Acquire a lock for the given address to ensure thread safety.
Expand All @@ -130,12 +132,12 @@ def __get_address_lock(
>>> meta = ChecksumASyncSingletonMeta('MySingleton', (), {})
>>> lock = meta._ChecksumASyncSingletonMeta__get_address_lock('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', True)
"""
with cls.__locks_lock:
return cls.__locks[is_sync][address]
with self.__locks_lock:
return self.__locks[is_sync][address]

def __delete_address_lock(
cls, address: AnyAddressOrContract, is_sync: bool
) -> None:
self, address: AnyAddressOrContract, is_sync: bool
) -> None: # sourcery skip: use-contextlib-suppress
"""
Delete the lock for an address once the instance is created.
Expand All @@ -150,8 +152,8 @@ def __delete_address_lock(
>>> meta = ChecksumASyncSingletonMeta('MySingleton', (), {})
>>> meta._ChecksumASyncSingletonMeta__delete_address_lock('0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb', True)
"""
with cls.__locks_lock:
with self.__locks_lock:
try:
del cls.__locks[is_sync][address]
del self.__locks[is_sync][address]
except KeyError:
pass
2 changes: 1 addition & 1 deletion y/utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ async def _get_logs_async_no_cache(address, topics, start, end) -> List[Log]:
# This is some intermittent error I need to debug in dank_mids, I think it occurs when we get rate limited
if str(e) != "a bytes-like object is required, not 'NoneType'":
raise
await asyncio.sleep(0.5)
await sleep(0.5)
# remove this logger before merging to master
logger.info("eth_getLogs call failed, retrying...")
return await _get_logs_async_no_cache(address, topics, start, end)
Expand Down

0 comments on commit c0f66dc

Please sign in to comment.