jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/623030 )
Change subject: [IMPR] Add typing hints and solve some statical type errors
......................................................................
[IMPR] Add typing hints and solve some statical type errors
Change-Id: I79525d21778cdbce8032df49ebf2d716b1eee5d5
---
M pywikibot/bot.py
M pywikibot/config2.py
M pywikibot/exceptions.py
M pywikibot/family.py
M pywikibot/logentries.py
M pywikibot/page/__init__.py
M pywikibot/tools/formatter.py
7 files changed, 41 insertions(+), 36 deletions(-)
Approvals:
Matěj Suchánek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 4f87b3a..1dd2c69 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -99,6 +99,7 @@
from importlib import import_module
from pathlib import Path
from textwrap import fill
+from typing import Any, Dict
from warnings import warn
import pywikibot
@@ -1024,7 +1025,7 @@
# The values are the default values
# Overwrite this in subclasses!
- availableOptions = {}
+ availableOptions = {} # type: Dict[str, Any]
def __init__(self, **kwargs):
"""
diff --git a/pywikibot/config2.py b/pywikibot/config2.py
index a6ef6ef..2c67120 100644
--- a/pywikibot/config2.py
+++ b/pywikibot/config2.py
@@ -48,6 +48,7 @@
from locale import getdefaultlocale
from os import getenv, environ
from textwrap import fill
+from typing import Dict, List, Tuple
from warnings import warn
from pywikibot import __version__ as pwb_version
@@ -133,7 +134,7 @@
# usernames['wikibooks']['*'] = 'mySingleUsername'
# You may use '*' for family name in a similar manner.
#
-usernames = collections.defaultdict(dict)
+usernames = collections.defaultdict(dict) # type: Dict[str, Dict[str, str]]
disambiguation_comment = collections.defaultdict(dict)
# User agent format.
@@ -441,7 +442,7 @@
# this can be used to pass variables to the UI init function
# useful for e.g.
# userinterface_init_kwargs = {'default_stream': 'stdout'}
-userinterface_init_kwargs = {}
+userinterface_init_kwargs = {} # type: Dict[str, str]
# i18n setting for user interface language
# default is obtained from L{locale.getdefaultlocale}
@@ -552,7 +553,7 @@
#
# sample:
# user_script_paths = ['scripts.myscripts']
-user_script_paths = []
+user_script_paths = [] # type: List[str]
# ############# INTERWIKI SETTINGS ##############
@@ -807,7 +808,7 @@
# (if cosmetic_changes_mylang_only is set)
# Please set your dictionary by adding such lines to your user-config.py:
# cosmetic_changes_enable['wikipedia'] = ('de', 'en',
'fr')
-cosmetic_changes_enable = {}
+cosmetic_changes_enable = {} # type: Dict[str, Tuple[str, ...]]
# The dictionary cosmetic_changes_disable should contain a tuple of languages
# for each site where you wish to disable cosmetic changes. You may use it with
@@ -815,7 +816,7 @@
# language. This also overrides the settings in the cosmetic_changes_enable
# dictionary. Please set your dict by adding such lines to your user-config.py:
# cosmetic_changes_disable['wikipedia'] = ('de', 'en',
'fr')
-cosmetic_changes_disable = {}
+cosmetic_changes_disable = {} # type: Dict[str, Tuple[str, ...]]
# cosmetic_changes_deny_script is a list of scripts for which cosmetic changes
# are disabled. You may add additional scripts by appending script names in
@@ -837,7 +838,7 @@
#
# to replace all occurrences of 'Hoofdpagina' with 'Veurblaad' when
writing to
# liwiki. Note that this does not take the origin wiki into account.
-replicate_replace = {}
+replicate_replace = {} # type: Dict[str, Dict[str, str]]
# ############# FURTHER SETTINGS ##############
@@ -847,7 +848,7 @@
# on the wiki server. Allows simulation runs of bots to be carried out without
# changing any page on the server side. Use this setting to add more actions
# in user-config.py for wikis with extra write actions.
-actions_to_block = []
+actions_to_block = [] # type: List[str]
# Set simulate to True or use -simulate option to block all actions given
# above.
@@ -1117,7 +1118,7 @@
if userinterface_lang is None:
userinterface_lang = os.getenv('PYWIKIBOT_USERINTERFACE_LANG') \
or getdefaultlocale()[0]
- if userinterface_lang in [None, 'C']:
+ if userinterface_lang is None or userinterface_lang == 'C':
userinterface_lang = 'en'
else:
userinterface_lang = userinterface_lang.split('_')[0]
diff --git a/pywikibot/exceptions.py b/pywikibot/exceptions.py
index a16de1b..9bbe2b7 100644
--- a/pywikibot/exceptions.py
+++ b/pywikibot/exceptions.py
@@ -87,6 +87,8 @@
#
# Distributed under the terms of the MIT license.
#
+from typing import Optional
+
from pywikibot.tools import _NotImplementedWarning
@@ -133,12 +135,11 @@
Page, and when a generic message can be written once for all.
"""
- # Preformatted UNICODE message where the page title will be inserted
+ # Preformatted message where the page title will be inserted.
# Override this in subclasses.
- # 'Oh noes! Page %s is too funky, we should not delete it ;('
- message = None
+ message = ''
- def __init__(self, page, message=None):
+ def __init__(self, page, message: Optional[str] = None):
"""
Initializer.
diff --git a/pywikibot/family.py b/pywikibot/family.py
index 3641c68..8df90ce 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -16,6 +16,7 @@
from importlib import import_module
from itertools import chain
from os.path import basename, dirname, splitext
+from typing import Dict, List, Optional, Tuple
import pywikibot
from pywikibot.comms.http import fetch
@@ -78,7 +79,7 @@
name = None
- langs = {}
+ langs = {} # type: Dict[str, str]
# For interwiki sorting order see
#
https://meta.wikimedia.org/wiki/Interwiki_sorting_order
@@ -471,12 +472,12 @@
# A dict of tuples for different sites with names of templates
# that indicate an edit should be avoided
- edit_restricted_templates = {}
+ edit_restricted_templates = {} # type: Dict[str, Tuple[str, ...]]
# A dict of tuples for different sites with names of archive
# templates that indicate an edit of non-archive bots
# should be avoided
- archived_page_templates = {}
+ archived_page_templates = {} # type: Dict[str, Tuple[str, ...]]
# A list of projects that share cross-project sessions.
cross_projects = []
@@ -489,38 +490,38 @@
cross_projects_cookie_username = 'centralauth_User'
# A list with the name in the cross-language flag permissions
- cross_allowed = []
+ cross_allowed = [] # type: List[str]
# A dict with the name of the category containing disambiguation
# pages for the various languages. Only one category per language,
# and without the namespace, so add things like:
# 'en': "Disambiguation"
- disambcatname = {}
+ disambcatname = {} # type: Dict[str, str]
# DEPRECATED, stores the code of the site which have a case sensitive
# main namespace. Use the Namespace given from the Site instead
- nocapitalize = []
+ nocapitalize = [] # type: List[str]
# attop is a list of languages that prefer to have the interwiki
# links at the top of the page.
- interwiki_attop = []
+ interwiki_attop = [] # type: List[str]
# on_one_line is a list of languages that want the interwiki links
# one-after-another on a single line
- interwiki_on_one_line = []
+ interwiki_on_one_line = [] # type: List[str]
# String used as separator between interwiki links and the text
interwiki_text_separator = '\n\n'
# Similar for category
- category_attop = []
+ category_attop = [] # type: List[str]
# on_one_line is a list of languages that want the category links
# one-after-another on a single line
- category_on_one_line = []
+ category_on_one_line = [] # type: List[str]
# String used as separator between category links and the text
category_text_separator = '\n\n'
# When both at the bottom should categories come after interwikilinks?
# TODO: T86284 Needed on Wikia sites, as it uses the CategorySelect
# extension which puts categories last on all sites. TO BE DEPRECATED!
- categories_last = []
+ categories_last = [] # type: List[str]
# Which languages have a special order for putting interlanguage
# links, and what order is it? If a language is not in
@@ -546,15 +547,15 @@
# Which language codes no longer exist and by which language code
# should they be replaced. If for example the language with code xx:
# now should get code yy:, add {'xx':'yy'} to obsolete.
- interwiki_replacements = {}
+ interwiki_replacements = {} # type: Dict[str, str]
# Codes that should be removed, usually because the site has been
# taken down.
- interwiki_removals = []
+ interwiki_removals = [] # type: List[str]
# Language codes of the largest wikis. They should be roughly sorted
# by size.
- languages_by_size = []
+ languages_by_size = [] # type: List[str]
# Some languages belong to a group where the possibility is high that
# equivalent articles have identical titles among the group.
@@ -646,7 +647,7 @@
# Some wiki farms have UrlShortener extension enabled only on the main
# site. This value can specify this last one with (lang, family) tuple.
- shared_urlshortner_wiki = None
+ shared_urlshortner_wiki = None # type: Optional[Tuple[str, str]]
_families = {}
@@ -1364,9 +1365,9 @@
}
# Not open for edits; stewards can still edit.
- closed_wikis = []
+ closed_wikis = [] # type: List[str]
# Completely removed
- removed_wikis = []
+ removed_wikis = [] # type: List[str]
# WikimediaFamily uses wikibase for the category name containing
# disambiguation pages for the various languages. We need the
diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py
index b4dd0e7..61a31a3 100644
--- a/pywikibot/logentries.py
+++ b/pywikibot/logentries.py
@@ -6,6 +6,7 @@
# Distributed under the terms of the MIT license.
#
from collections import UserDict
+from typing import Optional
import pywikibot
from pywikibot.exceptions import Error, HiddenKeyError
@@ -29,7 +30,7 @@
# Log type expected. None for every type, or one of the (letype) str :
# block/patrol/etc...
# Overridden in subclasses.
- _expected_type = None
+ _expected_type = None # type: Optional[str]
def __init__(self, apidata, site):
"""Initialize object from a logevent dict returned by MW
API."""
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index 9c5054a..b5e8495 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -27,7 +27,7 @@
from collections.abc import MutableMapping
from html.entities import name2codepoint
from itertools import chain
-from typing import List, Optional, Tuple, Union
+from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.parse import quote_from_bytes, unquote_to_bytes
from warnings import warn
@@ -3855,7 +3855,6 @@
@cvar DATA_ATTRIBUTES: dictionary which maps data attributes (eg. 'labels',
'claims') to appropriate collection classes (eg. LanguageDict,
ClaimsCollection)
- @type DATA_ATTRIBUTES: dict
@cvar entity_type: entity type identifier
@type entity_type: str
@@ -3865,7 +3864,7 @@
@type title_pattern: str
"""
- DATA_ATTRIBUTES = {}
+ DATA_ATTRIBUTES = {} # type: Dict[str, Any]
def __init__(self, repo, id_=None):
"""
diff --git a/pywikibot/tools/formatter.py b/pywikibot/tools/formatter.py
index 7ee318b..6ab45cb 100644
--- a/pywikibot/tools/formatter.py
+++ b/pywikibot/tools/formatter.py
@@ -8,7 +8,7 @@
import math
from string import Formatter
-from typing import Sequence
+from typing import Any, Mapping, Sequence
from pywikibot.logging import output
from pywikibot.userinterfaces.terminal_interface_base import colors
@@ -104,7 +104,8 @@
if previous_literal:
yield previous_literal, None, None, None
- def vformat(self, format_string: str, args: Sequence, kwargs: dict) -> str:
+ def vformat(self, format_string: str, args: Sequence,
+ kwargs: Mapping[str, Any]) -> str:
"""Return the format result but verify no colors are keywords.
@param format_string: The format template string
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/623030
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I79525d21778cdbce8032df49ebf2d716b1eee5d5
Gerrit-Change-Number: 623030
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged