Skip to content

Commit

Permalink
fix: add type check and fix the type issues (#26)
Browse files Browse the repository at this point in the history
* feat: add pyright type check to pre commit

* chore: update the dependencies required to fix the type issues

* fix: all the type issues

* fix: install dependencies before running pre commit in ci

* fix: python version in ci

* fix: pre-commit issue on ci

* fix: refine typing and fix grammar in error message

The 'Any' type was removed from the `on_notify_price_sched` callback function in the `Pythd` class, specifying it to not return any value. In `publisher.py`, a grammatical error was corrected in the exception message for an unknown provider.

---------

Co-authored-by: Keyvan <[email protected]>
  • Loading branch information
keyvankhademi and Keyvan authored Mar 21, 2024
1 parent 6f1802a commit 6f3b6cf
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 73 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: pre-commit/[email protected]
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Run image
uses: abatilo/actions-poetry@v2
with:
poetry-version: '1.3.2'
- name: Install dependencies
run: poetry install
- name: Run pre-commit
run: poetry run pre-commit run --all-files
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ repos:
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/fsouza/mirrors-pyright
rev: v1.1.354
hooks:
- id: pyright
14 changes: 11 additions & 3 deletions example_publisher/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ def __init__(self, config: Config) -> None:
if not getattr(self.config, self.config.provider_engine):
raise ValueError(f"Missing {self.config.provider_engine} config")

if self.config.provider_engine == "coin_gecko":
if (
self.config.provider_engine == "coin_gecko"
and config.coin_gecko is not None
):
self.provider = CoinGecko(config.coin_gecko)
elif self.config.provider_engine == "pyth_replicator":
elif (
self.config.provider_engine == "pyth_replicator"
and config.pyth_replicator is not None
):
self.provider: Provider = PythReplicator(config.pyth_replicator)
else:
raise ValueError(f"Unknown provider {self.config.provider_engine}")
raise ValueError(
f"Unknown provider {self.config.provider_engine}, possibly the env variables are not set."
)

self.pythd: Pythd = Pythd(
address=config.pythd.endpoint,
Expand Down
21 changes: 10 additions & 11 deletions example_publisher/pythd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from dataclasses import dataclass, field
import sys
import traceback
from dataclasses_json import config, dataclass_json
from typing import Awaitable, Callable, List
from dataclasses_json import config, DataClassJsonMixin
from typing import Callable, Coroutine, List
from structlog import get_logger
from jsonrpc_websocket import Server

Expand All @@ -15,37 +15,36 @@
TRADING = "trading"


@dataclass_json
@dataclass
class Price:
class Price(DataClassJsonMixin):
account: str
exponent: int = field(metadata=config(field_name="price_exponent"))


@dataclass_json
@dataclass
class Metadata:
class Metadata(DataClassJsonMixin):
symbol: str


@dataclass_json
@dataclass
class Product:
class Product(DataClassJsonMixin):
account: str
metadata: Metadata = field(metadata=config(field_name="attr_dict"))
prices: List[Price] = field(metadata=config(field_name="price"))


class Pythd:
def __init__(
self, address: str, on_notify_price_sched: Callable[[SubscriptionId], Awaitable]
self,
address: str,
on_notify_price_sched: Callable[[SubscriptionId], Coroutine[None, None, None]],
) -> None:
self.address = address
self.server: Server = None
self.server: Server
self.on_notify_price_sched = on_notify_price_sched
self._tasks = set()

async def connect(self) -> Server:
async def connect(self):
self.server = Server(self.address)
self.server.notify_price_sched = self._notify_price_sched
task = await self.server.ws_connect()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import random
from typing import List
from example_publisher.providers.pyth_replicator import manual_aggregate


def test_manual_aggregate_works():
prices = [1, 2, 3, 4, 5, 6, 8, 10, 12, 14]
prices: List[float] = [1, 2, 3, 4, 5, 6, 8, 10, 12, 14]
random.shuffle(prices)

agg_price, agg_confidence_interval = manual_aggregate(prices)
Expand Down
Loading

0 comments on commit 6f3b6cf

Please sign in to comment.