Skip to content

Commit

Permalink
Merge "[typing] solve some mypy issues"
Browse files Browse the repository at this point in the history
  • Loading branch information
xqt authored and Gerrit Code Review committed Oct 17, 2023
2 parents 2791376 + 0e2287f commit a117e91
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pywikibot/backports.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __exit__(self, *excinfo: Any) -> None:
if PYTHON_VERSION < (3, 9):
from typing import (
Container,
Counter,
Dict,
FrozenSet,
Generator,
Expand All @@ -92,6 +93,7 @@ def __exit__(self, *excinfo: Any) -> None:
Mapping,
Sequence,
)
from collections import Counter
from re import Match, Pattern
Dict = dict # type: ignore[misc]
FrozenSet = frozenset # type: ignore[misc]
Expand Down
2 changes: 1 addition & 1 deletion pywikibot/cosmetic_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def removeEmptySections(self, text: str) -> str:
header, sections, footer = textlib.extract_sections(text, self.site)

# iterate stripped sections and create a new page body
new_body = []
new_body: List[textlib.Section] = []
for i, strip_section in enumerate(strip_sections):
current_dep = sections[i].level
try:
Expand Down
13 changes: 8 additions & 5 deletions pywikibot/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import pywikibot
from pywikibot import config
from pywikibot.backports import (
DefaultDict,
Dict,
FrozenSet,
List,
Expand All @@ -36,6 +37,8 @@

logger = logging.getLogger('pywiki.wiki.family')

CrossnamespaceType = DefaultDict[str, Dict[str, List[int]]]

# Legal characters for Family.name and Family.langs keys
NAME_CHARACTERS = string.ascii_letters + string.digits
# nds_nl code alias requires "_"n
Expand Down Expand Up @@ -135,8 +138,8 @@ def instance(cls):
# should be avoided
archived_page_templates: Dict[str, Tuple[str, ...]] = {}

# A list of projects that share cross-project sessions.
cross_projects = []
# A set of projects that share cross-project sessions.
cross_projects: Set[str] = set()

# A list with the name for cross-project cookies.
# default for wikimedia centralAuth extensions.
Expand Down Expand Up @@ -182,7 +185,7 @@ def instance(cls):
# is checked first, and languages are put in the order given there.
# All other languages are put after those, in code-alphabetical
# order.
interwiki_putfirst = {}
interwiki_putfirst: Dict[str, str] = {}

# Some families, e. g. commons and meta, are not multilingual and
# forward interlanguage links to another family (wikipedia).
Expand Down Expand Up @@ -267,7 +270,7 @@ def instance(cls):
# values are dicts where:
# keys are the languages that can be linked to from the lang+ns, or
# '_default'; values are a list of namespace numbers
crossnamespace = collections.defaultdict(dict)
crossnamespace: CrossnamespaceType = collections.defaultdict(dict)
##
# Examples :
#
Expand Down Expand Up @@ -302,7 +305,7 @@ def instance(cls):
.. versionadded:: 7.0
"""

_families = {}
_families: Dict[str, 'Family'] = {}

@staticmethod
def load(fam: Optional[str] = None):
Expand Down
17 changes: 8 additions & 9 deletions pywikibot/textlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ class _GetDataHTML(HTMLParser):
"""

textdata = ''
keeptags = []
keeptags: List[str] = []

def __enter__(self) -> None:
pass
Expand Down Expand Up @@ -1140,15 +1140,14 @@ def getLanguageLinks(
text: str,
insite=None,
template_subpage: bool = False
) -> Dict:
"""
Return a dict of inter-language links found in text.
) -> Dict['pywikibot.site.BaseSite', 'pywikibot.Page']:
"""Return a dict of inter-language links found in text.
The returned dict uses the site as keys and Page objects as values. It does
not contain its own site.
The returned dict uses the site as keys and Page objects as values.
It does not contain its own site.
Do not call this routine directly, use Page.interwiki() method
instead.
Do not call this routine directly, use
:meth:`page.BasePage.interwiki` method instead.
"""
if insite is None:
insite = pywikibot.Site()
Expand All @@ -1157,7 +1156,7 @@ def getLanguageLinks(
# infos there
if fam.interwiki_forward:
fam = Family.load(fam.interwiki_forward)
result = {}
result: Dict[pywikibot.site.BaseSite, pywikibot.Page] = {}
# Ignore interwiki links within nowiki tags, includeonly tags, pre tags,
# and HTML comments
include = []
Expand Down
3 changes: 2 additions & 1 deletion pywikibot/throttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import pywikibot
from pywikibot import config
from pywikibot.backports import Counter as CounterType
from pywikibot.tools import deprecated


Expand Down Expand Up @@ -72,7 +73,7 @@ def __init__(self, site: Union['pywikibot.site.BaseSite', str], *,
self.retry_after = 0 # set by http.request
self.delay = 0
self.checktime = 0.0
self.modules = Counter()
self.modules: CounterType[str] = Counter()

self.checkMultiplicity()
self.setDelays()
Expand Down

0 comments on commit a117e91

Please sign in to comment.