Skip to content

Commit 9621c01

Browse files
committed
Replace param xpath with query in SelectorList.xpath()
1 parent 80509e4 commit 9621c01

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

docs/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ Converting CSS to XPath
11301130

11311131
When you're using an API that only accepts XPath expressions, it's sometimes
11321132
useful to convert CSS to XPath. This allows you to take advantage of the
1133-
conciseness of CSS to query elements by classes and the easeness of
1133+
conciseness of CSS to query elements by classes and the easiness of
11341134
manipulating XPath expressions at the same time.
11351135

11361136
On those occasions, use the function :func:`~parsel.css2xpath`:

parsel/selector.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import sys
6+
from warnings import warn
67

78
import six
89
from lxml import etree, html
@@ -73,7 +74,7 @@ def __getitem__(self, pos):
7374
def __getstate__(self):
7475
raise TypeError("can't pickle SelectorList objects")
7576

76-
def xpath(self, xpath, namespaces=None, **kwargs):
77+
def xpath(self, query=None, namespaces=None, **kwargs):
7778
"""
7879
Call the ``.xpath()`` method for each element in this list and return
7980
their results flattened as another :class:`SelectorList`.
@@ -90,7 +91,18 @@ def xpath(self, xpath, namespaces=None, **kwargs):
9091
9192
selector.xpath('//a[href=$url]', url="http://www.example.com")
9293
"""
93-
return self.__class__(flatten([x.xpath(xpath, namespaces=namespaces, **kwargs) for x in self]))
94+
if query is None:
95+
query = kwargs.pop("xpath", None)
96+
if query is not None:
97+
warn(
98+
"The parameter 'xpath' is deprecated, use 'query' instead.",
99+
DeprecationWarning,
100+
stacklevel=2
101+
)
102+
else:
103+
raise ValueError("The 'query' parameter is missing or None")
104+
105+
return self.__class__(flatten([x.xpath(query, namespaces=namespaces, **kwargs) for x in self]))
94106

95107
def css(self, query):
96108
"""

0 commit comments

Comments
 (0)