Skip to content

Commit 5f25dab

Browse files
author
clickingbuttons
authored
fix non-list return types (#148)
1 parent 77e1084 commit 5f25dab

File tree

9 files changed

+198
-235
lines changed

9 files changed

+198
-235
lines changed

polygon/rest/aggs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def get_grouped_daily_aggs(
5656
adjusted: Optional[bool] = None,
5757
params: Optional[Dict[str, Any]] = None,
5858
raw: bool = False,
59-
) -> Union[List[GroupedDailyAgg], HTTPResponse]:
59+
) -> Union[GroupedDailyAgg, HTTPResponse]:
6060
"""
6161
Get the daily open, high, low, and close (OHLC) for the entire market.
6262

polygon/rest/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,11 @@ def _get(
7272
if result_key:
7373
obj = obj[result_key]
7474

75-
# If obj is not yet a list, need to turn it into a list
76-
# This is for the Daily Open/Close and Last Trade endpoints
77-
if type(obj) != list:
78-
obj = [obj]
79-
8075
if deserializer:
81-
obj = [deserializer(o) for o in obj]
76+
if type(obj) == list:
77+
obj = [deserializer(o) for o in obj]
78+
else:
79+
obj = deserializer(obj)
8280

8381
return obj
8482

polygon/rest/models/trades.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,3 @@ class Last:
6969
@staticmethod
7070
def from_dict(d):
7171
return Last(**d)
72-
73-
74-
@dataclass
75-
class LastTradeCrypto:
76-
last: Last
77-
ticker: str
78-
status: str
79-
request_id: str
80-
81-
@staticmethod
82-
def from_dict(d):
83-
return LastTradeCrypto(
84-
d.get("last", None),
85-
d.get("symbol", None),
86-
d.get("status", None),
87-
d.get("request_id", None),
88-
)

polygon/rest/trades.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .base import BaseClient
22
from typing import Optional, Any, Dict, Union, Iterator
3-
from .models import Trade, LastTrade, LastTradeCrypto, Sort, Order
3+
from .models import Trade, LastTrade, Last, Sort, Order
44
from urllib3 import HTTPResponse
55
from datetime import datetime, date
66

@@ -75,7 +75,7 @@ def get_last_trade_crypto(
7575
to: str,
7676
params: Optional[Dict[str, Any]] = None,
7777
raw: bool = False,
78-
) -> Union[LastTrade, HTTPResponse]:
78+
) -> Union[Last, HTTPResponse]:
7979
"""
8080
Get the most recent trade for a ticker.
8181
@@ -89,6 +89,7 @@ def get_last_trade_crypto(
8989
return self._get(
9090
path=url,
9191
params=self._get_params(self.get_last_trade_crypto, locals()),
92-
deserializer=LastTradeCrypto.from_dict,
92+
result_key="last",
93+
deserializer=Last.from_dict,
9394
raw=raw,
9495
)

tests/test_aggs.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,19 @@ def test_get_grouped_daily_aggs(self):
5353

5454
def test_get_daily_open_close_agg(self):
5555
aggs = self.c.get_daily_open_close_agg("AAPL", "2005-04-01", True)
56-
expected = [
57-
DailyOpenCloseAgg(
58-
after_hours=123,
59-
close=123,
60-
from_="2021-04-01",
61-
high=124.18,
62-
low=122.49,
63-
open=123.66,
64-
pre_market=123.45,
65-
status="OK",
66-
symbol="AAPL",
67-
volume=75089134,
68-
)
69-
]
56+
expected = DailyOpenCloseAgg(
57+
after_hours=123,
58+
close=123,
59+
from_="2021-04-01",
60+
high=124.18,
61+
low=122.49,
62+
open=123.66,
63+
pre_market=123.45,
64+
status="OK",
65+
symbol="AAPL",
66+
volume=75089134,
67+
)
68+
7069
self.assertEqual(aggs, expected)
7170

7271
def test_get_previous_close_agg(self):

tests/test_markets.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,16 @@ def test_get_market_holidays(self):
123123

124124
def test_get_market_status(self):
125125
status = self.c.get_market_status()
126-
expected = [
127-
MarketStatus(
128-
after_hours=True,
129-
currencies={"fx": "open", "crypto": "open"},
130-
early_hours=False,
131-
exchanges={
132-
"nyse": "extended-hours",
133-
"nasdaq": "extended-hours",
134-
"otc": "extended-hours",
135-
},
136-
market="extended-hours",
137-
server_time="2022-04-28T16:48:08-04:00",
138-
)
139-
]
126+
expected = MarketStatus(
127+
after_hours=True,
128+
currencies={"fx": "open", "crypto": "open"},
129+
early_hours=False,
130+
exchanges={
131+
"nyse": "extended-hours",
132+
"nasdaq": "extended-hours",
133+
"otc": "extended-hours",
134+
},
135+
market="extended-hours",
136+
server_time="2022-04-28T16:48:08-04:00",
137+
)
140138
self.assertEqual(status, expected)

tests/test_snapshots.py

Lines changed: 105 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -155,123 +155,117 @@ def test_get_snapshot_direction(self):
155155

156156
def test_get_snapshot_ticker(self):
157157
snapshots = self.c.get_snapshot_ticker("AAPL")
158-
expected = [
159-
Snapshot(
160-
day={
161-
"c": 160.315,
162-
"h": 166.2,
163-
"l": 159.8,
164-
"o": 161.84,
165-
"v": 68840127,
166-
"vw": 162.7124,
167-
},
168-
last_quote={
169-
"P": 159.99,
170-
"S": 5,
171-
"p": 159.98,
172-
"s": 3,
173-
"t": 1651251948407646487,
174-
},
175-
last_trade={
176-
"c": None,
177-
"i": "121351",
178-
"p": 159.99,
179-
"s": 200,
180-
"t": 1651251948294080343,
181-
"x": 12,
182-
},
183-
min={
184-
"av": 68834255,
185-
"c": 160.3,
186-
"h": 160.71,
187-
"l": 160.3,
188-
"o": 160.71,
189-
"v": 197226,
190-
"vw": 160.5259,
191-
},
192-
prev_day={
193-
"c": 163.64,
194-
"h": 164.515,
195-
"l": 158.93,
196-
"o": 159.25,
197-
"v": 130149192,
198-
"vw": 161.8622,
199-
},
200-
ticker="AAPL",
201-
todays_change=-3.65,
202-
todays_change_percent=None,
203-
updated=1651251948294080343,
204-
)
205-
]
158+
expected = Snapshot(
159+
day={
160+
"c": 160.315,
161+
"h": 166.2,
162+
"l": 159.8,
163+
"o": 161.84,
164+
"v": 68840127,
165+
"vw": 162.7124,
166+
},
167+
last_quote={
168+
"P": 159.99,
169+
"S": 5,
170+
"p": 159.98,
171+
"s": 3,
172+
"t": 1651251948407646487,
173+
},
174+
last_trade={
175+
"c": None,
176+
"i": "121351",
177+
"p": 159.99,
178+
"s": 200,
179+
"t": 1651251948294080343,
180+
"x": 12,
181+
},
182+
min={
183+
"av": 68834255,
184+
"c": 160.3,
185+
"h": 160.71,
186+
"l": 160.3,
187+
"o": 160.71,
188+
"v": 197226,
189+
"vw": 160.5259,
190+
},
191+
prev_day={
192+
"c": 163.64,
193+
"h": 164.515,
194+
"l": 158.93,
195+
"o": 159.25,
196+
"v": 130149192,
197+
"vw": 161.8622,
198+
},
199+
ticker="AAPL",
200+
todays_change=-3.65,
201+
todays_change_percent=None,
202+
updated=1651251948294080343,
203+
)
206204
self.assertEqual(snapshots, expected)
207205

208206
def test_get_snapshot_option(self):
209207
snapshots = self.c.get_snapshot_option("AAPL", "O:AAPL230616C00150000")
210-
expected = [
211-
OptionContractSnapshot(
212-
break_even_price=179.075,
213-
day={
214-
"change": -2.3999999999999986,
215-
"change_percent": -7.643312101910824,
216-
"close": 29,
217-
"high": 32.25,
218-
"last_updated": 1651204800000000000,
219-
"low": 29,
220-
"open": 29.99,
221-
"previous_close": 31.4,
222-
"volume": 8,
223-
"vwap": 30.7738,
224-
},
225-
details={
226-
"contract_type": "call",
227-
"exercise_style": "american",
228-
"expiration_date": "2023-06-16",
229-
"shares_per_contract": 100,
230-
"strike_price": 150,
231-
"ticker": "O:AAPL230616C00150000",
232-
},
233-
greeks={
234-
"delta": 0.6436614934293701,
235-
"gamma": 0.0061735291012820675,
236-
"theta": -0.028227189324641973,
237-
"vega": 0.6381159723175714,
238-
},
239-
implied_volatility=0.3570277203465058,
240-
last_quote={
241-
"ask": 29.25,
242-
"ask_size": 209,
243-
"bid": 28.9,
244-
"bid_size": 294,
245-
"last_updated": 1651254260800059648,
246-
"midpoint": 29.075,
247-
"timeframe": "REAL-TIME",
248-
},
249-
open_interest=8133,
250-
underlying_asset={
251-
"change_to_break_even": 19.11439999999999,
252-
"last_updated": 1651254263172073152,
253-
"price": 159.9606,
254-
"ticker": "AAPL",
255-
"timeframe": "REAL-TIME",
256-
},
257-
)
258-
]
208+
expected = OptionContractSnapshot(
209+
break_even_price=179.075,
210+
day={
211+
"change": -2.3999999999999986,
212+
"change_percent": -7.643312101910824,
213+
"close": 29,
214+
"high": 32.25,
215+
"last_updated": 1651204800000000000,
216+
"low": 29,
217+
"open": 29.99,
218+
"previous_close": 31.4,
219+
"volume": 8,
220+
"vwap": 30.7738,
221+
},
222+
details={
223+
"contract_type": "call",
224+
"exercise_style": "american",
225+
"expiration_date": "2023-06-16",
226+
"shares_per_contract": 100,
227+
"strike_price": 150,
228+
"ticker": "O:AAPL230616C00150000",
229+
},
230+
greeks={
231+
"delta": 0.6436614934293701,
232+
"gamma": 0.0061735291012820675,
233+
"theta": -0.028227189324641973,
234+
"vega": 0.6381159723175714,
235+
},
236+
implied_volatility=0.3570277203465058,
237+
last_quote={
238+
"ask": 29.25,
239+
"ask_size": 209,
240+
"bid": 28.9,
241+
"bid_size": 294,
242+
"last_updated": 1651254260800059648,
243+
"midpoint": 29.075,
244+
"timeframe": "REAL-TIME",
245+
},
246+
open_interest=8133,
247+
underlying_asset={
248+
"change_to_break_even": 19.11439999999999,
249+
"last_updated": 1651254263172073152,
250+
"price": 159.9606,
251+
"ticker": "AAPL",
252+
"timeframe": "REAL-TIME",
253+
},
254+
)
259255
self.assertEqual(snapshots, expected)
260256

261257
def test_get_snapshot_crypto_book(self):
262258
snapshots = self.c.get_snapshot_crypto_book("X:BTCUSD")
263-
expected = [
264-
SnapshotTickerFullBook(
265-
ticker="X:BTCUSD",
266-
bids=[
267-
{"p": 16303.17, "x": {"1": 2}},
268-
{"p": 16302.94, "x": {"1": 0.02859424, "6": 0.023455}},
269-
],
270-
asks=[{"p": 11454, "x": {"2": 1}}, {"p": 11455, "x": {"2": 1}}],
271-
bid_count=694.951789670001,
272-
ask_count=593.1412981600005,
273-
spread=-4849.17,
274-
updated=1605295074162,
275-
)
276-
]
259+
expected = SnapshotTickerFullBook(
260+
ticker="X:BTCUSD",
261+
bids=[
262+
{"p": 16303.17, "x": {"1": 2}},
263+
{"p": 16302.94, "x": {"1": 0.02859424, "6": 0.023455}},
264+
],
265+
asks=[{"p": 11454, "x": {"2": 1}}, {"p": 11455, "x": {"2": 1}}],
266+
bid_count=694.951789670001,
267+
ask_count=593.1412981600005,
268+
spread=-4849.17,
269+
updated=1605295074162,
270+
)
277271
self.assertEqual(snapshots, expected)

0 commit comments

Comments
 (0)