Skip to content

Commit

Permalink
Return None for empty nested selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
VMRuiz committed Jun 3, 2024
1 parent c235e4e commit a2388f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions itemloaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/test_nested_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a2388f2

Please sign in to comment.