gs-quant icon indicating copy to clipboard operation
gs-quant copied to clipboard

IRSwap does not round trip. IRSwap.from_dict(swap.as_dict()) throws type error

Open AlexanderJHall opened this issue 4 years ago • 0 comments

When attempting to round trip an unresolved IRSwap instrument a TypeError is thrown.

The issue is not with IRSwap instrument but the way that the "Base" classes method __from_dict() handles datetime.dates. In 0.8.149 setting a instrument property to a datetime and then attempting to roundtrip via Instrument.from_dict(inst.as_dict()) will fail as Base.__from_dict() cannot handle datetime.date inputs.

To Reproduce import datetime from gs_quant.instrument import IRSwap swap = IRSwap('Pay', termination_date=datetime.date(2030, 1, 2), fixed_rate='atm', notional_currency='eur') swap.as_dict() new_swap = IRSwap.from_dict(swap.as_dict())

Expected behavior new_swap should be a new IRSwap instrument.

This used to work as expected in prior versions, eg 0.8.90.

Screenshots If applicable, add screenshots to help explain your problem.

Systems setup:

  • OS: Windows 10
  • Python version 3.7
  • GS-Quant version 0.8.149

Additional context Error is: File "", line 7, in IRSwap.from_dict(swap.as_dict()) File "C:\Users\alexa\Anaconda3\lib\site-packages\gs_quant\instrument\core.py", line 55, in from_dict return cls._from_dict(values) File "C:\Users\alexa\Anaconda3\lib\site-packages\gs_quant\base.py", line 330, in _from_dict instance.__from_dict(values) File "C:\Users\alexa\Anaconda3\lib\site-packages\gs_quant\base.py", line 282, in __from_dict setattr(self, prop, dateutil.parser.isoparse(prop_value).date()) File "C:\Users\alexa\Anaconda3\lib\site-packages\dateutil\parser\isoparser.py", line 37, in func return f(self, str_in, *args, **kwargs) File "C:\Users\alexa\Anaconda3\lib\site-packages\dateutil\parser\isoparser.py", line 134, in isoparse components, pos = self._parse_isodate(dt_str) File "C:\Users\alexa\Anaconda3\lib\site-packages\dateutil\parser\isoparser.py", line 208, in _parse_isodate return self._parse_isodate_common(dt_str) File "C:\Users\alexa\Anaconda3\lib\site-packages\dateutil\parser\isoparser.py", line 213, in _parse_isodate_common len_str = len(dt_str) TypeError: object of type 'datetime.date' has no len()

Issue is around line 280 of gs_quant.base.py:

elif issubclass(prop_type, dt.date): try: setattr(self, prop, dateutil.parser.isoparse(prop_value).date()) except ValueError: if str in additional_types: setattr(self, prop, prop_value)

suggest the except clause should be extended to include TypeError

This didn't occur in earlier versions of gs_quant as in this case prop_type (line 259) evaluated to None so this section of code ran: if prop_type is None: # This shouldn't happen setattr(self, prop, prop_value)

AlexanderJHall avatar Jul 07 '20 16:07 AlexanderJHall