Skip to content

Commit

Permalink
mypy stuff
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 28, 2018
1 parent aae5f13 commit 77c6906
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions requests_html.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from urllib.parse import urlparse, urlunparse
from concurrent.futures._base import TimeoutError
from typing import Set
from typing import Set, Union, List

import pyppeteer
import requests
Expand All @@ -21,6 +21,10 @@

useragent = UserAgent()

# Typing.
_Find = Union[List['Element'], 'Element']
_XPath = Union[List[str], List['Element'], str, 'Element']
_HTML = Union[str, bytes]


class BaseParser:
Expand All @@ -33,12 +37,17 @@ class BaseParser:
"""

def __init__(self, *, element, default_encoding: str = None, html: str = None, url: str) -> None:
def __init__(self, *, element, default_encoding: str = None, html: _HTML = None, url: str) -> None:
self.element = element
self.url = url
self.skip_anchors = True
self.default_encoding = default_encoding
self._encoding = None

# Encode incoming unicode HTML into bytes.
if isinstance(html, str):
html = html.encode(DEFAULT_ENCODING)

self._html = html

@property
Expand Down Expand Up @@ -100,7 +109,7 @@ def full_text(self) -> str:
"""The full text content (including links) of the :class:`Element <Element>` or :class:`HTML <HTML>`.."""
return self.lxml.text_content()

def find(self, selector: str, first: bool = False, _encoding: str = None):
def find(self, selector: str, first: bool = False, _encoding: str = None) -> _Find:
"""Given a CSS Selector, returns a list of :class:`Element <Element>` objects.
:param selector: CSS Selector to use.
Expand Down Expand Up @@ -131,7 +140,7 @@ def find(self, selector: str, first: bool = False, _encoding: str = None):
else:
return elements

def xpath(self, selector: str, first: bool = False, _encoding: str = None):
def xpath(self, selector: str, first: bool = False, _encoding: str = None) -> _XPath:
"""Given an XPath selector, returns a list of
:class:`Element <Element>` objects.
Expand All @@ -154,7 +163,7 @@ def xpath(self, selector: str, first: bool = False, _encoding: str = None):
if not isinstance(selection, etree._ElementUnicodeResult):
element = Element(element=selection, url=self.url, default_encoding=_encoding or self.encoding)
else:
element = selection
element = str(selection)
c.append(element)

if first:
Expand Down Expand Up @@ -280,7 +289,7 @@ class HTML(BaseParser):
:param default_encoding: Which encoding to default to.
"""

def __init__(self, *, url=DEFAULT_URL, html, default_encoding=DEFAULT_ENCODING) -> None:
def __init__(self, *, url: str = DEFAULT_URL, html: _HTML, default_encoding: str =DEFAULT_ENCODING) -> None:

# Convert incoming unicode HTML into bytes.
if isinstance(html, str):
Expand Down Expand Up @@ -382,9 +391,9 @@ class HTMLResponse(requests.Response):
intelligent ``.html`` property added.
"""

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

@property
def html(self) -> HTML:
Expand Down

0 comments on commit 77c6906

Please sign in to comment.