Response.follow_all: return empty generators for empty sequences

This commit is contained in:
Eugenio Lacuesta 2020-03-12 09:36:15 -03:00
parent 388f23c30c
commit 8d30dc0888
No known key found for this signature in database
GPG Key ID: DA3EF2D0913E9810
2 changed files with 9 additions and 3 deletions

View File

@ -188,9 +188,11 @@ class TextResponse(Response):
selectors from which links cannot be obtained (for instance, anchor tags without an
``href`` attribute)
"""
arg_count = len(list(filter(None, (urls, css, xpath))))
if arg_count != 1:
raise ValueError('Please supply exactly one of the following arguments: urls, css, xpath')
arguments = [x for x in (urls, css, xpath) if x is not None]
if len(arguments) != 1:
raise ValueError(
"Please supply exactly one of the following arguments: urls, css, xpath"
)
if not urls:
if css:
urls = self.css(css)

View File

@ -215,6 +215,10 @@ class BaseResponseTest(unittest.TestCase):
links = map(Link, absolute)
self._assert_followed_all_urls(links, absolute)
def test_follow_all_empty(self):
r = self.response_class("http://example.com")
self.assertEqual([], list(r.follow_all([])))
def test_follow_all_invalid(self):
r = self.response_class("http://example.com")
if self.response_class == Response: