Skip to content

Commit

Permalink
type annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth Reitz <me@kennethreitz.org>
  • Loading branch information
kennethreitz committed Feb 27, 2018
1 parent c11cd6c commit 3dbcf4e
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions requests_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from lxml.html import HtmlElement
from lxml.html.soupparser import fromstring
from parse import search as parse_search
from parse import findall
from parse import findall, Result
from w3lib.encoding import html_to_unicode


Expand Down Expand Up @@ -110,11 +110,11 @@ def xpath(self, selector: str, first: bool = False, _encoding: str = None):
else:
return c

def search(self, template: str):
def search(self, template: str) -> Result:
"""Searches the :class:`Element <Element>` for the given parse template."""
return parse_search(template, self.html)

def search_all(self, template: str):
def search_all(self, template: str) -> Result:
"""Searches the :class:`Element <Element>` (multiple times) for the given parse
template.
"""
Expand Down Expand Up @@ -178,11 +178,11 @@ def base_url(self) -> str:
class Element(BaseParser):
"""An element of HTML."""

def __init__(self, *, element, url, default_encoding):
def __init__(self, *, element, url, default_encoding) -> None:
super(Element, self).__init__(element=element, url=url, default_encoding=default_encoding)
self.element = element

def __repr__(self):
def __repr__(self) -> str:
attrs = []
for attr in self.attrs:
attrs.append('{}={}'.format(attr, repr(self.attrs[attr])))
Expand All @@ -204,7 +204,7 @@ def attrs(self) -> dict:
class HTML(BaseParser):
"""An HTML document, ready for parsing."""

def __init__(self, *, url, html, default_encoding=DEFAULT_ENCODING):
def __init__(self, *, url, html, default_encoding=DEFAULT_ENCODING) -> None:
super(HTML, self).__init__(
element=PyQuery(html)('html'),
html=html,
Expand All @@ -215,7 +215,7 @@ def __init__(self, *, url, html, default_encoding=DEFAULT_ENCODING):
def __repr__(self) -> str:
return "<HTML url={}>".format(repr(self.url))

def render(self, retries: int = 8):
def render(self, retries: int = 8) -> None:
"""Loads the response in Chromium, and replaces HTML content
with an updated version, JavaScript executed.
"""
Expand Down Expand Up @@ -245,16 +245,14 @@ async def _async_render(url: str):
html = HTML(url=self.url, html=content, default_encoding=DEFAULT_ENCODING)
self.__dict__.update(html.__dict__)

return self


class HTMLResponse(requests.Response):
"""An HTML-enabled :class:`Response <Response>` object.
Same as Requests class:`Response <Response>` object, but with an
intelligent ``.html`` property added.
"""

def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
super(HTMLResponse, self).__init__(*args, **kwargs)
self._html = None

Expand Down

0 comments on commit 3dbcf4e

Please sign in to comment.