Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlexanderJHall opened this issue Jul 7, 2020 · 0 comments
Assignees

Comments

@AlexanderJHall
Copy link

AlexanderJHall commented Jul 7, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants