From b01f2f230304150516f30895f8c93e459ed88b1c Mon Sep 17 00:00:00 2001 From: Krasimir Trenchev Date: Mon, 13 May 2024 21:37:33 +0300 Subject: [PATCH] Add support for set type --- itemloaders/utils.py | 4 ++++ tests/test_nested_items.py | 7 +++++++ 2 files changed, 11 insertions(+) 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