Skip to content

Commit 766c255

Browse files
committed
upgrade parsel and add shim for deprecated selectorlist methods
1 parent e50610b commit 766c255

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ queuelib
77
six>=1.5.2
88
PyDispatcher>=2.0.5
99
service_identity
10-
parsel>=0.9.3
10+
parsel>=0.9.5

scrapy/selector/unified.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import warnings
6-
from parsel import Selector as ParselSelector, SelectorList
6+
from parsel import Selector as ParselSelector, SelectorList as ParselSelectorList
77
from scrapy.utils.trackref import object_ref
88
from scrapy.utils.python import to_bytes
99
from scrapy.http import HtmlResponse, XmlResponse
@@ -26,9 +26,24 @@ def _response_from_text(text, st):
2626
body=to_bytes(text, 'utf-8'))
2727

2828

29+
class SelectorList(ParselSelectorList, object_ref):
30+
@deprecated(use_instead='.extract()')
31+
def extract_unquoted(self):
32+
return [x.extract_unquoted() for x in self]
33+
34+
@deprecated(use_instead='.xpath()')
35+
def x(self, xpath):
36+
return self.select(xpath)
37+
38+
@deprecated(use_instead='.xpath()')
39+
def select(self, xpath):
40+
return self.xpath(xpath)
41+
42+
2943
class Selector(ParselSelector, object_ref):
3044

3145
__slots__ = ['response']
46+
selectorlist_cls = SelectorList
3247

3348
def __init__(self, response=None, text=None, type=None, root=None, _root=None, **kwargs):
3449
st = _st(response, type or self._default_type)

tests/test_selector.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ def test_weakref_slots(self):
101101
assert not hasattr(x, '__dict__'), "%s does not use __slots__" % \
102102
x.__class__.__name__
103103

104+
def test_deprecated_selector_methods(self):
105+
sel = Selector(TextResponse(url="http://example.com", body=b'<p>some text</p>'))
106+
107+
with warnings.catch_warnings(record=True) as w:
108+
sel.select('//p')
109+
self.assertSubstring('Use .xpath() instead', str(w[-1].message))
110+
111+
with warnings.catch_warnings(record=True) as w:
112+
sel.extract_unquoted()
113+
self.assertSubstring('Use .extract() instead', str(w[-1].message))
114+
115+
def test_deprecated_selectorlist_methods(self):
116+
sel = Selector(TextResponse(url="http://example.com", body=b'<p>some text</p>'))
117+
118+
with warnings.catch_warnings(record=True) as w:
119+
sel.xpath('//p').select('.')
120+
self.assertSubstring('Use .xpath() instead', str(w[-1].message))
121+
122+
with warnings.catch_warnings(record=True) as w:
123+
sel.xpath('//p').extract_unquoted()
124+
self.assertSubstring('Use .extract() instead', str(w[-1].message))
125+
104126

105127
class DeprecatedXpathSelectorTest(unittest.TestCase):
106128

0 commit comments

Comments
 (0)