From 07891b606c74295c7375b4488cf8e11e7513867a Mon Sep 17 00:00:00 2001 From: Robert-Zacchigna Date: Thu, 18 Apr 2024 16:19:57 -0500 Subject: [PATCH] Make explicit when an optional variable is 'None' when not being used --- pyetrade/accounts.py | 6 ++---- pyetrade/market.py | 1 - pyetrade/order.py | 26 ++++++++++++++------------ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pyetrade/accounts.py b/pyetrade/accounts.py index b13000f..28cb413 100644 --- a/pyetrade/accounts.py +++ b/pyetrade/accounts.py @@ -277,10 +277,8 @@ def list_transactions( ) payload = { - "startDate": start_date.date().strftime("%m%d%Y") - if start_date - else start_date, - "endDate": end_date.date().strftime("%m%d%Y") if end_date else end_date, + "startDate": start_date.date().strftime("%m%d%Y") if start_date else None, + "endDate": end_date.date().strftime("%m%d%Y") if end_date else None, "sortOrder": sort_order, "marker": marker, "count": count, diff --git a/pyetrade/market.py b/pyetrade/market.py index 74fc01d..f908443 100644 --- a/pyetrade/market.py +++ b/pyetrade/market.py @@ -290,7 +290,6 @@ def get_option_expire_date(self, symbol: str, resp_format: str = "xml") -> dict: :EtradeRef: https://apisb.etrade.com/docs/api/market/api-market-v1.html """ - assert isinstance(resp_format, str) assert resp_format in ["xml", "json"] api_url = "%s%s" % ( diff --git a/pyetrade/order.py b/pyetrade/order.py index 9759d8c..5eb3c2f 100644 --- a/pyetrade/order.py +++ b/pyetrade/order.py @@ -35,20 +35,22 @@ def to_decimal_str(price: float, round_down: bool) -> str: def get_request_result(req: OAuth1Session.request, resp_format: str = "xml") -> dict: LOGGER.debug(req.text) - if resp_format == "json": - if req.text.strip() == "": - # otherwise, when ETrade server return empty string, we got this error: - # simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) - req_output = {} - else: - req_output = req.json() - else: + # Initialize as empty dict, otherwise, when ETrade server returns an empty string, you get this error: + # "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)" + req_output = {} + + assert resp_format in ["xml", "json"] + + if resp_format == "json" and req.text.strip() != "": + req_output = req.json() + elif resp_format == "xml": req_output = xmltodict.parse(req.text) if "Error" in req_output.keys(): raise Exception( f'Etrade API Error - Code: {req_output["Error"]["code"]}, Msg: {req_output["Error"]["message"]}' ) + return req_output @@ -204,7 +206,7 @@ def list_orders( if count >= 101: LOGGER.debug( - f"Count {count} is greater than the max allowable value (100), using 100" + f"Count {count} is greater than the max allowable value (100), using 100." ) count = 100 @@ -212,9 +214,9 @@ def list_orders( "marker": marker, "count": count, "status": status, - "fromDate": from_date.date().strftime("%m%d%Y") if from_date else from_date, - "toDate": to_date.date().strftime("%m%d%Y") if to_date else to_date, - "symbol": ",".join([sym for sym in symbols[:25]]) if symbols else symbols, + "fromDate": from_date.date().strftime("%m%d%Y") if from_date else None, + "toDate": to_date.date().strftime("%m%d%Y") if to_date else None, + "symbol": ",".join([sym for sym in symbols[:25]]) if symbols else None, "securityType": security_type, "transactionType": transaction_type, "marketSession": market_session,