From 63bd7589b74f7e87c49f9ff6490af0116506adc9 Mon Sep 17 00:00:00 2001 From: Jean Bredeche Date: Thu, 14 Apr 2016 11:07:22 -0400 Subject: [PATCH] BUG: support passing an empty list to `data` methods. Our type checking code was a bit too aggressive. --- tests/test_algorithm.py | 15 +++++++++++++++ zipline/_protocol.pyx | 12 +++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/test_algorithm.py b/tests/test_algorithm.py index 3d8a64b4..452d52fb 100644 --- a/tests/test_algorithm.py +++ b/tests/test_algorithm.py @@ -1905,6 +1905,21 @@ def handle_data(context, data): self.assertEqual(expected, cm.exception.args[0]) + def test_empty_asset_list_to_history(self): + algo = TradingAlgorithm( + script=dedent(""" + def initialize(context): + pass + + def handle_data(context, data): + data.history([], "price", 5, '1d') + """), + sim_params=self.sim_params, + env=self.env + ) + + algo.run(self.data_portal) + class TestGetDatetime(TestCase): diff --git a/zipline/_protocol.pyx b/zipline/_protocol.pyx index bb91e80b..486f9931 100644 --- a/zipline/_protocol.pyx +++ b/zipline/_protocol.pyx @@ -62,18 +62,17 @@ cdef class check_parameters(object): " '%s'" % (func.__name__, field)) # verify type of each arg - i = 0 - while i < (len(args) - 1): - arg = args[i + 1] + for i, arg in enumerate(args[1:]): expected_type = self.types[i] if isinstance(arg, expected_type): - i += 1 continue elif (i == 0 or i == 1) and _is_iterable(arg): + if len(arg) == 0: + continue + if isinstance(arg[0], expected_type): - i += 1 continue expected_type_name = expected_type.__name__ \ @@ -91,6 +90,9 @@ cdef class check_parameters(object): if isinstance(arg, self.keys_to_types[keyword]): continue elif keyword in ('assets', 'fields') and _is_iterable(arg): + if len(arg) == 0: + continue + if isinstance(arg[0], self.keys_to_types[i]): continue