Skip to content

Commit

Permalink
Prevent throwing KeyErrors for bond orders (#138)
Browse files Browse the repository at this point in the history
* Ignore canceled deposits

* Implement suggestion to ignore all events with status "canceled" and map card_failed_transaction to PPEventType.REMOVAL

* Replace subscription '''return_vals[]''' with '''return_vals.get()''' in event.py/_parse_shares_and_fees

---------

Co-authored-by: Christoph Langer <[email protected]>
  • Loading branch information
pinzutu and RealCLanger authored Nov 5, 2024
1 parent 50ad548 commit 223ba67
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions pytr/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
class ConditionalEventType(Enum):
"""Events that conditionally map to None or one/multiple PPEventType events"""

FAILED_CARD_TRANSACTION = auto()
SAVEBACK = auto()
TRADE_INVOICE = auto()

Expand Down Expand Up @@ -50,7 +49,7 @@ class EventType(Enum):
"CREDIT": PPEventType.DIVIDEND,
"ssp_corporate_action_invoice_cash": PPEventType.DIVIDEND,
# Failed card transactions
"card_failed_transaction": ConditionalEventType.FAILED_CARD_TRANSACTION,
"card_failed_transaction": PPEventType.REMOVAL,
# Interests
"INTEREST_PAYOUT": PPEventType.INTEREST,
"INTEREST_PAYOUT_CREATED": PPEventType.INTEREST,
Expand Down Expand Up @@ -116,12 +115,8 @@ def _parse_type(event_dict: Dict[Any, Any]) -> Optional[EventType]:
event_type: Optional[EventType] = tr_event_type_mapping.get(
event_dict.get("eventType", ""), None
)
if event_type == ConditionalEventType.FAILED_CARD_TRANSACTION:
event_type = (
PPEventType.REMOVAL
if event_dict.get("status", "").lower() == "executed"
else None
)
if event_dict.get("status", "").lower() == "canceled":
event_type = None
return event_type

@classmethod
Expand Down Expand Up @@ -210,7 +205,7 @@ def _parse_shares_and_fees(
titles, shares_dicts + fees_dicts, locales
):
return_vals[key] = cls._parse_float_from_detail(elem_dict, locale)
return return_vals["shares"], return_vals.get("fees", None)
return return_vals.get("shares"), return_vals.get("fees")

@classmethod
def _parse_taxes(cls, event_dict: Dict[Any, Any]) -> Optional[float]:
Expand Down

0 comments on commit 223ba67

Please sign in to comment.