def _asdict(obj: Any) -> Any: """ Helper for ItemAdapter.asdict """ if isinstance(obj, dict): return {key: _asdict(value) for key, value in obj.items()} elif isinstance(obj, (list, set, tuple)): return obj.__class__(_asdict(x) for x in obj) elif isinstance(obj, ItemAdapter): return obj.asdict() elif is_item(obj): return ItemAdapter(obj).asdict() else: return obj
def test_true_pydantic(self): self.assertTrue(is_item(PydanticModel(name="asdf", value=1234))) self.assertTrue(ItemAdapter.is_item_class(PydanticModel))
def test_true_attrs(self): self.assertTrue(is_item(AttrsItem(name="asdf", value=1234))) self.assertTrue(ItemAdapter.is_item_class(AttrsItem))
def test_true_dataclass(self): self.assertTrue(is_item(DataClassItem(name="asdf", value=1234))) self.assertTrue(ItemAdapter.is_item_class(DataClassItem))
def test_true_scrapy(self): self.assertTrue(is_item(ScrapyItem())) self.assertTrue(is_item(ScrapySubclassedItem(name="asdf", value=1234))) self.assertTrue(ItemAdapter.is_item_class(ScrapyItem)) self.assertTrue(ItemAdapter.is_item_class(ScrapySubclassedItem))
def test_true_dict(self): self.assertTrue(is_item({"a": "dict"})) self.assertTrue(ItemAdapter.is_item_class(dict))
def test_false(self): self.assertFalse(is_item(int)) self.assertFalse(is_item(sum)) self.assertFalse(is_item(1234)) self.assertFalse(is_item(object())) self.assertFalse(is_item("a string")) self.assertFalse(is_item(b"some bytes")) self.assertFalse(is_item(["a", "list"])) self.assertFalse(is_item(("a", "tuple"))) self.assertFalse(is_item({"a", "set"})) self.assertFalse(is_item(dict)) self.assertFalse(is_item(ScrapyItem)) self.assertFalse(is_item(DataClassItem)) self.assertFalse(is_item(ScrapySubclassedItem)) self.assertFalse(is_item(AttrsItem)) self.assertFalse(is_item(PydanticModel)) self.assertFalse(ItemAdapter.is_item_class(list)) self.assertFalse(ItemAdapter.is_item_class(int)) self.assertFalse(ItemAdapter.is_item_class(tuple))
def test_true_attrs(self): self.assertTrue(is_item(AttrsItem(name="asdf", value=1234)))
def test_true_dict(self): self.assertTrue(is_item({"a": "dict"}))