diff --git a/itemloaders/utils.py b/itemloaders/utils.py index 91a6556..10d2f13 100644 --- a/itemloaders/utils.py +++ b/itemloaders/utils.py @@ -13,6 +13,8 @@ def arg_to_iter(arg: Any) -> Iterable[Any]: If *arg* is a list, a tuple or a generator, it will be returned as is. + If *arg* is a set, it will be casted to a list. + If *arg* is ``None``, an empty list will be returned. If *arg* is anything else, a list will be returned with *arg* as its only @@ -22,6 +24,8 @@ def arg_to_iter(arg: Any) -> Iterable[Any]: return [] if isinstance(arg, (list, tuple, Generator)): return arg + if isinstance(arg, set): + return list(arg) return [arg] diff --git a/tests/test_nested_items.py b/tests/test_nested_items.py index fee9913..ef9ef2a 100644 --- a/tests/test_nested_items.py +++ b/tests/test_nested_items.py @@ -39,6 +39,13 @@ class TestItem: def test_dict(self): self._test_item({"foo": "bar"}) + def test_set(self): + item = {"foo", "bar"} + il = ItemLoader() + il.add_value("item_list", item) + + self.assertEqual(il.load_item(), {"item_list": list(item)}) + def test_scrapy_item(self): try: from scrapy import Field, Item