Skip to content

Commit

Permalink
Replace param xpath with query in SelectorList.xpath()
Browse files Browse the repository at this point in the history
  • Loading branch information
laggardkernel committed Jul 15, 2021
1 parent 80509e4 commit 4abb905
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ Converting CSS to XPath

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

On those occasions, use the function :func:`~parsel.css2xpath`:
Expand Down
16 changes: 14 additions & 2 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import sys
from warnings import warn

import six
from lxml import etree, html
Expand Down Expand Up @@ -73,7 +74,7 @@ def __getitem__(self, pos):
def __getstate__(self):
raise TypeError("can't pickle SelectorList objects")

def xpath(self, xpath, namespaces=None, **kwargs):
def xpath(self, query=None, namespaces=None, **kwargs):
"""
Call the ``.xpath()`` method for each element in this list and return
their results flattened as another :class:`SelectorList`.
Expand All @@ -90,7 +91,18 @@ def xpath(self, xpath, namespaces=None, **kwargs):
selector.xpath('//a[href=$url]', url="http://www.example.com")
"""
return self.__class__(flatten([x.xpath(xpath, namespaces=namespaces, **kwargs) for x in self]))
if query is None:
query = kwargs.pop("xpath", None)
if query is not None:
warn(
"The parameter 'xpath' is deprecated, use 'query' instead.",
DeprecationWarning,
stacklevel=2
)
else:
raise TypeError("xpath() missing 1 required positional argument: 'query'")

return self.__class__(flatten([x.xpath(query, namespaces=namespaces, **kwargs) for x in self]))

def css(self, query):
"""
Expand Down

0 comments on commit 4abb905

Please sign in to comment.