Skip to content

Commit

Permalink
Fix assert onempty 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 bb5a57b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
10 changes: 5 additions & 5 deletions itemloaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def nested_xpath(self, xpath: str, **context: Any) -> Self:
:meth:`add_value`, :meth:`replace_value`, etc. will behave as expected.
"""
self._check_selector_method()
assert self.selector
assert self.selector is not None
selector = self.selector.xpath(xpath)
context.update(selector=selector)
subloader = self.__class__(item=self.item, parent=self, **context)
Expand All @@ -178,7 +178,7 @@ def nested_css(self, css: str, **context: Any) -> Self:
:meth:`add_value`, :meth:`replace_value`, etc. will behave as expected.
"""
self._check_selector_method()
assert self.selector
assert self.selector is not None
selector = self.selector.css(css)
context.update(selector=selector)
subloader = self.__class__(item=self.item, parent=self, **context)
Expand Down Expand Up @@ -473,7 +473,7 @@ def _get_xpathvalues(
self, xpaths: Union[str, Iterable[str]], **kw: Any
) -> List[Any]:
self._check_selector_method()
assert self.selector
assert self.selector is not None
xpaths = arg_to_iter(xpaths)
return flatten(self.selector.xpath(xpath, **kw).getall() for xpath in xpaths)

Expand Down Expand Up @@ -558,7 +558,7 @@ def get_css(

def _get_cssvalues(self, csss: Union[str, Iterable[str]]) -> List[Any]:
self._check_selector_method()
assert self.selector
assert self.selector is not None
csss = arg_to_iter(csss)
return flatten(self.selector.css(css).getall() for css in csss)

Expand Down Expand Up @@ -641,7 +641,7 @@ def get_jmes(

def _get_jmesvalues(self, jmess: Union[str, Iterable[str]]) -> List[Any]:
self._check_selector_method()
assert self.selector
assert self.selector is not None
jmess = arg_to_iter(jmess)
if not hasattr(self.selector, "jmespath"):
raise AttributeError(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_nested_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ 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)
nested_xpath = loader.nested_xpath("//bar")
assert isinstance(nested_xpath, ItemLoader)
nested_xpath.add_xpath("foo", "./foo")

nested_css = loader.nested_css("bar")
assert isinstance(nested_css, ItemLoader)
nested_css.add_css("foo", "foo")

0 comments on commit bb5a57b

Please sign in to comment.