From 82cb68ef720f48884f3209fa90e09739bcbd6ff2 Mon Sep 17 00:00:00 2001 From: Mike Shultz Date: Mon, 20 Nov 2023 14:03:48 -0700 Subject: [PATCH] fix: type | None is new in 3.10 --- silverback/persistence.py | 6 +++--- silverback/runner.py | 10 ++++++++-- silverback/types.py | 6 ++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/silverback/persistence.py b/silverback/persistence.py index 2e640c47..48b1cb60 100644 --- a/silverback/persistence.py +++ b/silverback/persistence.py @@ -8,7 +8,7 @@ from taskiq import TaskiqResult from typing_extensions import Self # Introduced 3.11 -from .types import SilverbackIdent, SilverbackSettings +from .types import IntOrNone, SilverbackIdent, SilverbackSettings class SilverbackState(BaseModel): @@ -39,8 +39,8 @@ def from_taskiq( cls, ident: SilverbackIdent, handler_id: str, - block_number: int | None, - log_index: int | None, + block_number: IntOrNone, + log_index: IntOrNone, result: TaskiqResult, ) -> Self: return cls( diff --git a/silverback/runner.py b/silverback/runner.py index a7c51e2f..d48e00fa 100644 --- a/silverback/runner.py +++ b/silverback/runner.py @@ -14,7 +14,13 @@ from .persistence import BasePersistentStorage, HandlerResult from .settings import Settings from .subscriptions import SubscriptionType, Web3SubscriptionsManager -from .types import SilverbackIdent, SilverbackStartupState, handler_id_block, handler_id_event +from .types import ( + IntOrNone, + SilverbackIdent, + SilverbackStartupState, + handler_id_block, + handler_id_event, +) from .utils import async_wrap_iter, hexbytes_dict settings = Settings() @@ -32,7 +38,7 @@ def __init__(self, app: SilverbackApp, *args, max_exceptions: int = 3, **kwargs) self.ident = SilverbackIdent.from_settings(settings) async def _handle_result( - self, handler_id: str, block_number: int | None, log_index: int | None, result: TaskiqResult + self, handler_id: str, block_number: IntOrNone, log_index: IntOrNone, result: TaskiqResult ): if result.is_err: self.exceptions += 1 diff --git a/silverback/types.py b/silverback/types.py index 17be0040..5db10ccb 100644 --- a/silverback/types.py +++ b/silverback/types.py @@ -1,8 +1,10 @@ -from typing import Optional, Protocol +from typing import Optional, Protocol, Union from pydantic import BaseModel from typing_extensions import Self # Introduced 3.11 +StrOrNone = Union[str, None] # For Python < 3.10 + class SilverbackSettings(Protocol): INSTANCE: str @@ -34,7 +36,7 @@ def handler_id_block(block_number: Optional[int]) -> str: return f"block/{block_number}" -def handler_id_event(contract_address: str | None, event_signature: str) -> str: +def handler_id_event(contract_address: StrOrNone, event_signature: str) -> str: """Return a unique handler ID string for an event""" # TODO: Under what circumstance can address be None? return f"event/{contract_address or 'unknown'}/{event_signature}"