Xqt has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/966503 )
Change subject: [typing] solve some mypy issues ......................................................................
[typing] solve some mypy issues
Change-Id: I3048641df1a6e5d4ced1ad3688a12a3e2f72c6e7 --- M pywikibot/textlib.py M pywikibot/family.py M pywikibot/throttle.py M pywikibot/cosmetic_changes.py M pywikibot/backports.py 5 files changed, 30 insertions(+), 16 deletions(-)
Approvals: Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/backports.py b/pywikibot/backports.py index f2a77ec..02b59fe 100644 --- a/pywikibot/backports.py +++ b/pywikibot/backports.py @@ -69,6 +69,7 @@ if PYTHON_VERSION < (3, 9): from typing import ( Container, + Counter, Dict, FrozenSet, Generator, @@ -92,6 +93,7 @@ Mapping, Sequence, ) + from collections import Counter from re import Match, Pattern Dict = dict # type: ignore[misc] FrozenSet = frozenset # type: ignore[misc] diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py index 29d8f64..05d7209 100644 --- a/pywikibot/cosmetic_changes.py +++ b/pywikibot/cosmetic_changes.py @@ -707,7 +707,7 @@ 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: diff --git a/pywikibot/family.py b/pywikibot/family.py index 52c6d3a..ed3bba9 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -21,6 +21,7 @@ import pywikibot from pywikibot import config from pywikibot.backports import ( + DefaultDict, Dict, FrozenSet, List, @@ -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 @@ -135,8 +138,8 @@ # 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. @@ -182,7 +185,7 @@ # 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). @@ -267,7 +270,7 @@ # 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 : # @@ -302,7 +305,7 @@ .. versionadded:: 7.0 """
- _families = {} + _families: Dict[str, 'Family'] = {}
@staticmethod def load(fam: Optional[str] = None): diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 99ca938..f68e6a6 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -582,7 +582,7 @@ """
textdata = '' - keeptags = [] + keeptags: List[str] = []
def __enter__(self) -> None: pass @@ -1140,15 +1140,14 @@ 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() @@ -1157,7 +1156,7 @@ # 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 = [] diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py index 825bfa3..348e751 100644 --- a/pywikibot/throttle.py +++ b/pywikibot/throttle.py @@ -15,6 +15,7 @@
import pywikibot from pywikibot import config +from pywikibot.backports import Counter as CounterType from pywikibot.tools import deprecated
@@ -72,7 +73,7 @@ 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()
pywikibot-commits@lists.wikimedia.org