Skip to content

Commit

Permalink
Remove Optional usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nalepae committed Oct 25, 2023
1 parent 1dbdfdc commit 6582d36
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 58 deletions.
4 changes: 2 additions & 2 deletions eth_validator_watcher/beacon.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
"""Get rewards.
Expand Down Expand Up @@ -333,7 +333,7 @@ def get_validators_liveness(

return {item.index: item.is_live for item in validators_liveness.data}

def get_potential_block(self, slot) -> Optional[Block]:
def get_potential_block(self, slot) -> Block | None:
"""Get a block if it exists, otherwise return None.
Parameters:
Expand Down
36 changes: 19 additions & 17 deletions eth_validator_watcher/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,27 @@
def handler(
beacon_url: str = Option(..., help="URL of beacon node", show_default=False),
execution_url: str = Option(None, help="URL of execution node", show_default=False),
pubkeys_file_path: Optional[Path] = Option(
pubkeys_file_path: Path
| None = Option(
None,
help="File containing the list of public keys to watch",
exists=True,
file_okay=True,
dir_okay=False,
show_default=False,
),
web3signer_url: Optional[str] = Option(
web3signer_url: str
| None = Option(
None, help="URL to web3signer managing keys to watch", show_default=False
),
fee_recipient: Optional[str] = Option(
fee_recipient: str
| None = Option(
None,
help="Fee recipient address - --execution-url must be set",
show_default=False,
),
slack_channel: Optional[str] = Option(
slack_channel: str
| None = Option(
None,
help="Slack channel to send alerts - SLACK_TOKEN env var must be set",
show_default=False,
Expand All @@ -116,9 +120,7 @@ def handler(
relay_url: List[str] = Option(
[], help="URL of allow listed relay", show_default=False
),
liveness_file: Optional[Path] = Option(
None, help="Liveness file", show_default=False
),
liveness_file: Path | None = Option(None, help="Liveness file", show_default=False),
) -> None:
"""
🚨 Ethereum Validator Watcher 🚨
Expand Down Expand Up @@ -188,14 +190,14 @@ def handler(

def _handler(
beacon_url: str,
execution_url: Optional[str],
pubkeys_file_path: Optional[Path],
web3signer_url: Optional[str],
fee_recipient: Optional[str],
slack_channel: Optional[str],
execution_url: str | None,
pubkeys_file_path: Path | None,
web3signer_url: str | None,
fee_recipient: str | None,
slack_channel: str | None,
beacon_type: BeaconType,
relays_url: List[str],
liveness_file: Optional[Path],
liveness_file: Path | None,
) -> None:
"""Just a wrapper to be able to test the handler function"""
slack_token = environ.get("SLACK_TOKEN")
Expand Down Expand Up @@ -240,11 +242,11 @@ def _handler(
exited_validators = ExitedValidators(slack)
slashed_validators = SlashedValidators(slack)

last_missed_attestations_process_epoch: Optional[int] = None
last_rewards_process_epoch: Optional[int] = None
last_missed_attestations_process_epoch: int | None = None
last_rewards_process_epoch: int | None = None

previous_epoch: Optional[int] = None
last_processed_finalized_slot: Optional[int] = None
previous_epoch: int | None = None
last_processed_finalized_slot: int | None = None

genesis = beacon.get_genesis()

Expand Down
5 changes: 2 additions & 3 deletions eth_validator_watcher/exited_validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains the ExitedValidators class, which is responsible for managing the exited
validators."""

from typing import Optional

from prometheus_client import Gauge

Expand All @@ -17,13 +16,13 @@
class ExitedValidators:
"""Exited validators abstraction."""

def __init__(self, slack: Optional[Slack]) -> None:
def __init__(self, slack: Slack | None) -> None:
"""Exited validators
Parameters:
slack: Optional slack client
"""
self.__our_exited_unslashed_indexes: Optional[set[int]] = None
self.__our_exited_unslashed_indexes: set[int] | None = None
self.__slack = slack

def process(
Expand Down
7 changes: 3 additions & 4 deletions eth_validator_watcher/fee_recipient.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Contains the logic to check if the fee recipient is the one expected."""

from typing import Optional

from prometheus_client import Counter

Expand All @@ -17,9 +16,9 @@
def process_fee_recipient(
block: Block,
index_to_validator: dict[int, Validators.DataItem.Validator],
execution: Optional[Execution],
expected_fee_recipient: Optional[str],
slack: Optional[Slack],
execution: Execution | None,
expected_fee_recipient: str | None,
slack: Slack | None,
) -> None:
"""Check if the fee recipient is the one expected.
Expand Down
5 changes: 2 additions & 3 deletions eth_validator_watcher/missed_attestations.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains the logic to check if the validators missed attestations."""

import functools
from typing import Optional, Set

from prometheus_client import Gauge

Expand Down Expand Up @@ -87,8 +86,8 @@ def process_double_missed_attestations(
previous_dead_indexes: set[int],
epoch_to_index_to_validator_index: LimitedDict,
epoch: int,
slack: Optional[Slack],
) -> Set[int]:
slack: Slack | None,
) -> set[int]:
"""Process double missed attestations.
Parameters:
Expand Down
7 changes: 3 additions & 4 deletions eth_validator_watcher/missed_blocks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains functions to handle missed block proposals detection on head"""

import functools
from typing import Optional

from prometheus_client import Counter

Expand Down Expand Up @@ -36,10 +35,10 @@

def process_missed_blocks_head(
beacon: Beacon,
potential_block: Optional[Block],
potential_block: Block | None,
slot: int,
our_pubkeys: set[str],
slack: Optional[Slack],
slack: Slack | None,
) -> bool:
"""Process missed block proposals detection at head
Expand Down Expand Up @@ -111,7 +110,7 @@ def process_missed_blocks_finalized(
last_processed_finalized_slot: int,
slot: int,
our_pubkeys: set[str],
slack: Optional[Slack],
slack: Slack | None,
) -> int:
"""Process missed block proposals detection at finalized
Expand Down
3 changes: 1 addition & 2 deletions eth_validator_watcher/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Contains the models for the validator watcher."""

from enum import StrEnum
from typing import Optional

from pydantic import BaseModel

Expand Down Expand Up @@ -153,7 +152,7 @@ class EthGetBlockByHashRequest(BaseModel):
class ExecutionBlock(BaseModel):
class Result(BaseModel):
class Transaction(BaseModel):
to: Optional[str]
to: str | None

transactions: list[Transaction]

Expand Down
9 changes: 3 additions & 6 deletions eth_validator_watcher/slashed_validators.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
"""Contains the SlashedValidators class, which is responsible for managing the slashed
validators."""

from typing import Optional

from prometheus_client import Gauge

from .models import Validators
Expand All @@ -22,14 +19,14 @@
class SlashedValidators:
"""Slashed validators abstraction."""

def __init__(self, slack: Optional[Slack]) -> None:
def __init__(self, slack: Slack | None) -> None:
"""Slashed validators
Parameters:
slack: Optional slack client
"""
self.__total_exited_slashed_indexes: Optional[set[int]] = None
self.__our_exited_slashed_indexes: Optional[set[int]] = None
self.__total_exited_slashed_indexes: set[int] | None = None
self.__our_exited_slashed_indexes: set[int] | None = None
self.__slack = slack

def process(
Expand Down
4 changes: 2 additions & 2 deletions eth_validator_watcher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ def load_pubkeys_from_file(path: Path) -> set[str]:


def get_our_pubkeys(
pubkeys_file_path: Optional[Path],
web3signer: Optional[Web3Signer],
pubkeys_file_path: Path | None,
web3signer: Web3Signer | None,
) -> set[str]:
"""Get our pubkeys
Expand Down
13 changes: 6 additions & 7 deletions tests/entrypoint/test__handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from pathlib import Path
from typing import Iterator, Optional, Tuple

from freezegun import freeze_time
from pytest import raises
from typer import BadParameter

from eth_validator_watcher import entrypoint
from eth_validator_watcher.entrypoint import _handler
from eth_validator_watcher.models import BeaconType, Genesis, Validators
from eth_validator_watcher.utils import LimitedDict, Slack
from eth_validator_watcher.web3signer import Web3Signer
from freezegun import freeze_time
from pytest import raises
from typer import BadParameter

StatusEnum = Validators.DataItem.StatusEnum
Validator = Validators.DataItem.Validator
Expand Down Expand Up @@ -199,7 +198,7 @@ def get_status_to_index_to_validator(
},
}

def get_potential_block(self, slot: int) -> Optional[str]:
def get_potential_block(self, slot: int) -> str | None:
assert slot in {63, 64}
return "A BLOCK"

Expand Down Expand Up @@ -291,7 +290,7 @@ def process_missed_blocks_finalized(

def process_suboptimal_attestations(
beacon: Beacon,
potential_block: Optional[str],
potential_block: str | None,
slot: int,
index_to_validator: dict[int, Validator],
) -> set[int]:
Expand All @@ -308,7 +307,7 @@ def process_suboptimal_attestations(

def process_missed_blocks(
beacon: Beacon,
potential_block: Optional[str],
potential_block: str | None,
slot: int,
pubkeys: set[str],
slack: Slack,
Expand Down
7 changes: 3 additions & 4 deletions tests/rewards/test_process_rewards_net.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from math import isclose
from typing import Optional

from eth_validator_watcher.models import BeaconType, Rewards, Validators
from eth_validator_watcher.rewards import (
Expand Down Expand Up @@ -53,7 +52,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down Expand Up @@ -184,7 +183,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down Expand Up @@ -375,7 +374,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down
7 changes: 3 additions & 4 deletions tests/rewards/test_process_rewards_our.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from math import isclose
from typing import Optional

from eth_validator_watcher.models import BeaconType, Rewards, Validators
from eth_validator_watcher.rewards import (
Expand Down Expand Up @@ -27,7 +26,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down Expand Up @@ -158,7 +157,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down Expand Up @@ -349,7 +348,7 @@ def get_rewards(
self,
beacon_type: BeaconType,
epoch: int,
validators_index: Optional[set[int]] = None,
validators_index: set[int] | None = None,
) -> Rewards:
assert isinstance(beacon_type, BeaconType)
assert epoch == 40
Expand Down

0 comments on commit 6582d36

Please sign in to comment.