Skip to content

Commit

Permalink
fixed typing
Browse files Browse the repository at this point in the history
  • Loading branch information
kmike committed May 1, 2024
1 parent b8d0352 commit ee3e734
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
27 changes: 8 additions & 19 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from warnings import warn

import html_text # type: ignore[import]
import html_text # type: ignore[import-untyped]
import jmespath
from lxml import etree, html
from lxml.html.clean import Cleaner # pylint: disable=no-name-in-module
Expand Down Expand Up @@ -275,14 +275,6 @@ def getall(

extract = getall

# TODO: bring types back
# @typing.overload
# def get(self, default: None = None) -> Optional[str]:
# pass
#
# @typing.overload
# def get(self, default: str) -> str:
# pass
def get(
self,
default: Optional[str] = None,
Expand All @@ -291,7 +283,7 @@ def get(
cleaner: Union[str, None, Cleaner] = "auto",
guess_punct_space: bool = True,
guess_layout: bool = True,
) -> Optional[str]:
) -> Any:
"""
Return the result of ``.get()`` for the first element in this list.
If the list is empty, return the ``default`` value.
Expand Down Expand Up @@ -822,14 +814,11 @@ def get(
)

try:
return typing.cast(
str,
etree.tostring(
tree,
method=_ctgroup[self.type]["_tostring_method"],
encoding="unicode",
with_tail=False,
),
etree.tostring(
tree,
method=_ctgroup[self.type]["_tostring_method"],
encoding="unicode",
with_tail=False,
)
except (AttributeError, TypeError):
if tree is True:
Expand Down Expand Up @@ -975,7 +964,7 @@ def cleaned(
else:
cleaner_obj = cleaner

root = cleaner_obj.clean_html(self.root) # type: ignore[type-var]
root = cleaner_obj.clean_html(self.root)
return self.__class__(
root=root,
_expr=self._expr,
Expand Down
16 changes: 8 additions & 8 deletions tests/test_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,47 +1356,47 @@ class SelectorTextTestCase(unittest.TestCase):
</body>
"""

def test_text_get(self):
def test_text_get(self) -> None:
sel = self.sscls(text="<p>title:<h1>some text</h1></p>")
txt = sel.get(text=True)
self.assertEqual(txt, "title:\n\nsome text")

def test_text_getall(self):
def test_text_getall(self) -> None:
sel = self.sscls(text="<ul><li>option1</li><li>option2</li></ul>").getall(
text=True
)
self.assertEqual(1, len(sel))
self.assertEqual("option1\noption2", sel[0])

def test_text_cleaned_get(self):
def test_text_cleaned_get(self) -> None:
sel = (
self.sscls(text="<p>paragraph</p><style>.items</style>")
.cleaned("html")
.get(text=True)
)
self.assertEqual("paragraph", sel)

def test_text_get_guess_punct_space_false(self):
def test_text_get_guess_punct_space_false(self) -> None:
sel = self.sscls(text='<p>hello<b>"Folks"</b></p>')
txt = sel.get(text=True, guess_punct_space=False)
self.assertEqual(txt, 'hello "Folks"')

def test_text_get_guess_layout_false(self):
def test_text_get_guess_layout_false(self) -> None:
sel = self.sscls(text="<ul><li>option1</li><li>option2</li></ul>")
txt = sel.get(text=True, guess_layout=False)
self.assertEqual(txt, "option1 option2")

def test_text_get_guess_layout_true(self):
def test_text_get_guess_layout_true(self) -> None:
sel = self.sscls(text="<ul><li>option1</li><li>option2</li></ul>")
txt = sel.get(text=True, guess_layout=True)
self.assertEqual(txt, "option1\noption2")

def test_text_css_multiple(self):
def test_text_css_multiple(self) -> None:
html = self.sscls(text=self.html_body)
items = html.css(".product .price").getall(text=True)
self.assertEqual(items, ["Price: 100", "Price: 200"])

def test_text_xpath_get(self):
def test_text_xpath_get(self) -> None:
html = self.sscls(text=self.html_body)
self.assertEqual(
html.xpath('//div[@class="product"]/span').getall(text=True),
Expand Down

0 comments on commit ee3e734

Please sign in to comment.