diff --git a/itemloaders/__init__.py b/itemloaders/__init__.py index f13bd77..d90d5f4 100644 --- a/itemloaders/__init__.py +++ b/itemloaders/__init__.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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( diff --git a/tests/test_nested_loader.py b/tests/test_nested_loader.py index 82d24f7..19e4bd3 100644 --- a/tests/test_nested_loader.py +++ b/tests/test_nested_loader.py @@ -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")