Skip to content

Commit

Permalink
fix: type | None is new in 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeshultz committed Nov 20, 2023
1 parent 8be2d10 commit 82cb68e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 3 additions & 3 deletions silverback/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions silverback/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions silverback/types.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}"

0 comments on commit 82cb68e

Please sign in to comment.