Skip to content

Commit

Permalink
finalize typing; add py.typed file to signal typing is present
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Jan 6, 2024
1 parent dfad82c commit 13af9c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions mediawiki/exceptions.py
@@ -1,7 +1,7 @@
"""
MediaWiki Exceptions
"""
from typing import List, Optional
from typing import Dict, List, Optional

from mediawiki.utilities import str_or_unicode

Expand Down Expand Up @@ -118,7 +118,7 @@ class DisambiguationError(MediaWikiBaseException):
`options` only includes titles that link to valid \
MediaWiki pages """

def __init__(self, title: str, may_refer_to: List[str], url: str, details: Optional[List[str]] = None):
def __init__(self, title: str, may_refer_to: List[str], url: str, details: Optional[List[Dict]] = None):
self._title = title
self._unordered_options = may_refer_to
self._options = sorted(may_refer_to)
Expand Down Expand Up @@ -149,7 +149,7 @@ def unordered_options(self) -> List[str]:
return self._unordered_options

@property
def details(self) -> Optional[List[str]]:
def details(self) -> Optional[List[Dict]]:
"""list: The details of the proposed non-disambigous pages"""
return self._details

Expand Down
22 changes: 13 additions & 9 deletions mediawiki/mediawikipage.py
Expand Up @@ -284,7 +284,7 @@ def logos(self) -> List[str]:
if not self._soup:
self._soup = BeautifulSoup(self.html, "html.parser")
info = self._soup.find("table", {"class": "infobox"})
if info is not None:
if info is not None and isinstance(info, Tag):
children = info.find_all("a", class_="image")
for child in children:
self._logos.append("https:" + child.img["src"])
Expand Down Expand Up @@ -353,7 +353,7 @@ def coordinates(self) -> Optional[Tuple[Decimal, Decimal]]:
if self._coordinates is False:
self._coordinates = None
self.__pull_combined_properties()
return self._coordinates
return self._coordinates # type: ignore

@property
def links(self) -> List[str]:
Expand Down Expand Up @@ -632,7 +632,7 @@ def _raise_page_error(self):
raise PageError(title=self.title)
raise PageError(pageid=self.pageid)

def _raise_disambiguation_error(self, page: str, pageid: int):
def _raise_disambiguation_error(self, page: Dict, pageid: int):
"""parse and throw a disambiguation error"""
query_params = {
"prop": "revisions",
Expand Down Expand Up @@ -666,7 +666,7 @@ def _raise_disambiguation_error(self, page: str, pageid: int):
disambiguation,
)

def _handle_redirect(self, redirect: bool, preload: bool, query: str, page: Dict[str, Any]):
def _handle_redirect(self, redirect: bool, preload: bool, query: Dict, page: Dict[str, Any]):
"""handle redirect"""
if redirect:
redirects = query["redirects"][0]
Expand All @@ -685,7 +685,7 @@ def _handle_redirect(self, redirect: bool, preload: bool, query: str, page: Dict
raise MediaWikiException(ODD_ERROR_MESSAGE)

# change the title and reload the whole object
self.__init__(
self.__init__( # type: ignore
self.mediawiki,
title=redirects["to"],
redirect=redirect,
Expand All @@ -699,7 +699,7 @@ def _continued_query(self, query_params: Dict[str, Any], key: str = "pages") ->
https://www.mediawiki.org/wiki/API:Query#Continuing_queries"""
query_params.update(self.__title_query_param())

last_cont = {}
last_cont: Dict = {}
prop = query_params.get("prop")

while True:
Expand Down Expand Up @@ -742,20 +742,23 @@ def _parse_section_links(self, id_tag: Optional[str]) -> List[Tuple[str, str]]:
root = self._soup.find("span", {"id": id_tag})
if root is None:
return all_links
candidates = self._soup.find(id=id_tag).parent.next_siblings
candidates = self._soup.find(id=id_tag).parent.next_siblings # type: ignore

for node in candidates:
if not isinstance(node, Tag):
continue
if node.get("role", "") == "navigation":
continue
if "infobox" in node.get("class", []):
classes = node.get("class", [])
if not isinstance(classes, list):
classes = [classes if classes else ""]
if "infobox" in classes:
continue

# If the classname contains "toc", the element is a table of contents.
# The comprehension is necessary because there are several possible
# types of tocs: "toclevel", "toc", ...
toc_classnames = [cname for cname in node.get("class", []) if "toc" in cname]
toc_classnames = [cname for cname in classes if "toc" in cname]
if toc_classnames:
continue

Expand All @@ -775,6 +778,7 @@ def __parse_link_info(self, link: Tag) -> Tuple[str, str]:
href = link.get("href", "")
if isinstance(href, list):
href = href[0]
href = "" if href is None else href
txt = link.string or href
is_rel = is_relative_url(href)
if is_rel is True:
Expand Down
Empty file added mediawiki/py.typed
Empty file.

0 comments on commit 13af9c8

Please sign in to comment.