From a2388f2c344ac6f3545455a2beebb97ba68f1629 Mon Sep 17 00:00:00 2001 From: Victor Ruiz Date: Mon, 3 Jun 2024 08:42:54 +0200 Subject: [PATCH] Return None for empty nested selectors --- itemloaders/__init__.py | 8 ++++++-- tests/test_nested_loader.py | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/itemloaders/__init__.py b/itemloaders/__init__.py index f13bd77..998224e 100644 --- a/itemloaders/__init__.py +++ b/itemloaders/__init__.py @@ -154,7 +154,7 @@ def item(self) -> Any: else: return self._local_item - def nested_xpath(self, xpath: str, **context: Any) -> Self: + def nested_xpath(self, xpath: str, **context: Any) -> Optional[Self]: """ Create a nested loader with an xpath selector. The supplied selector is applied relative to selector associated @@ -165,11 +165,13 @@ def nested_xpath(self, xpath: str, **context: Any) -> Self: self._check_selector_method() assert self.selector selector = self.selector.xpath(xpath) + if not selector: + return None context.update(selector=selector) subloader = self.__class__(item=self.item, parent=self, **context) return subloader - def nested_css(self, css: str, **context: Any) -> Self: + def nested_css(self, css: str, **context: Any) -> Optional[Self]: """ Create a nested loader with a css selector. The supplied selector is applied relative to selector associated @@ -180,6 +182,8 @@ def nested_css(self, css: str, **context: Any) -> Self: self._check_selector_method() assert self.selector selector = self.selector.css(css) + if not selector: + return None context.update(selector=selector) subloader = self.__class__(item=self.item, parent=self, **context) return subloader diff --git a/tests/test_nested_loader.py b/tests/test_nested_loader.py index 82d24f7..501bae9 100644 --- a/tests/test_nested_loader.py +++ b/tests/test_nested_loader.py @@ -117,3 +117,8 @@ def test_nested_load_item(self): self.assertEqual(item["name"], ["marta"]) self.assertEqual(item["url"], ["http://www.scrapy.org"]) self.assertEqual(item["image"], ["/images/logo.png"]) + + def test_nested_empty_selector(self): + loader = ItemLoader(selector=self.selector) + assert loader.nested_xpath("//bar") is None + assert loader.nested_css(".foo") is None