Skip to content

Commit 6f3b6cf

Browse files
keyvankhademiKeyvan
and
Keyvan
authored
fix: add type check and fix the type issues (#26)
* 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]>
1 parent 6f1802a commit 6f3b6cf

File tree

7 files changed

+184
-73
lines changed

7 files changed

+184
-73
lines changed

.github/workflows/pre-commit.yaml

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,14 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v2
13-
- uses: actions/setup-python@v2
14-
- uses: pre-commit/[email protected]
13+
- uses: actions/setup-python@v4
14+
with:
15+
python-version: '3.10'
16+
- name: Run image
17+
uses: abatilo/actions-poetry@v2
18+
with:
19+
poetry-version: '1.3.2'
20+
- name: Install dependencies
21+
run: poetry install
22+
- name: Run pre-commit
23+
run: poetry run pre-commit run --all-files

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ repos:
1414
rev: 6.0.0
1515
hooks:
1616
- id: flake8
17+
- repo: https://github.com/fsouza/mirrors-pyright
18+
rev: v1.1.354
19+
hooks:
20+
- id: pyright

example_publisher/publisher.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ def __init__(self, config: Config) -> None:
3232
if not getattr(self.config, self.config.provider_engine):
3333
raise ValueError(f"Missing {self.config.provider_engine} config")
3434

35-
if self.config.provider_engine == "coin_gecko":
35+
if (
36+
self.config.provider_engine == "coin_gecko"
37+
and config.coin_gecko is not None
38+
):
3639
self.provider = CoinGecko(config.coin_gecko)
37-
elif self.config.provider_engine == "pyth_replicator":
40+
elif (
41+
self.config.provider_engine == "pyth_replicator"
42+
and config.pyth_replicator is not None
43+
):
3844
self.provider: Provider = PythReplicator(config.pyth_replicator)
3945
else:
40-
raise ValueError(f"Unknown provider {self.config.provider_engine}")
46+
raise ValueError(
47+
f"Unknown provider {self.config.provider_engine}, possibly the env variables are not set."
48+
)
4149

4250
self.pythd: Pythd = Pythd(
4351
address=config.pythd.endpoint,

example_publisher/pythd.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from dataclasses import dataclass, field
33
import sys
44
import traceback
5-
from dataclasses_json import config, dataclass_json
6-
from typing import Awaitable, Callable, List
5+
from dataclasses_json import config, DataClassJsonMixin
6+
from typing import Callable, Coroutine, List
77
from structlog import get_logger
88
from jsonrpc_websocket import Server
99

@@ -15,37 +15,36 @@
1515
TRADING = "trading"
1616

1717

18-
@dataclass_json
1918
@dataclass
20-
class Price:
19+
class Price(DataClassJsonMixin):
2120
account: str
2221
exponent: int = field(metadata=config(field_name="price_exponent"))
2322

2423

25-
@dataclass_json
2624
@dataclass
27-
class Metadata:
25+
class Metadata(DataClassJsonMixin):
2826
symbol: str
2927

3028

31-
@dataclass_json
3229
@dataclass
33-
class Product:
30+
class Product(DataClassJsonMixin):
3431
account: str
3532
metadata: Metadata = field(metadata=config(field_name="attr_dict"))
3633
prices: List[Price] = field(metadata=config(field_name="price"))
3734

3835

3936
class Pythd:
4037
def __init__(
41-
self, address: str, on_notify_price_sched: Callable[[SubscriptionId], Awaitable]
38+
self,
39+
address: str,
40+
on_notify_price_sched: Callable[[SubscriptionId], Coroutine[None, None, None]],
4241
) -> None:
4342
self.address = address
44-
self.server: Server = None
43+
self.server: Server
4544
self.on_notify_price_sched = on_notify_price_sched
4645
self._tasks = set()
4746

48-
async def connect(self) -> Server:
47+
async def connect(self):
4948
self.server = Server(self.address)
5049
self.server.notify_price_sched = self._notify_price_sched
5150
task = await self.server.ws_connect()

example_publisher/tests/test_pyth_replicator_manual_aggregate.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import random
2+
from typing import List
23
from example_publisher.providers.pyth_replicator import manual_aggregate
34

45

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

910
agg_price, agg_confidence_interval = manual_aggregate(prices)

0 commit comments

Comments
 (0)