jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769476 )
Change subject: [typing] Add some type hints
......................................................................
[typing] Add some type hints
Change-Id: Idd5004b3cd589696b77b6a3b4635ab0e434315e4
---
M pywikibot/data/api.py
M pywikibot/family.py
M pywikibot/scripts/generate_user_files.py
M pywikibot/userinterfaces/gui.py
M pywikibot/userinterfaces/terminal_interface_base.py
M pywikibot/userinterfaces/win32_unicode.py
M pywikibot/version.py
M scripts/interwiki.py
M scripts/protect.py
M scripts/replace.py
M scripts/revertbot.py
M scripts/solve_disambiguation.py
12 files changed, 29 insertions(+), 63 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 7c81237..90e8198 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1258,7 +1258,7 @@
self.__defaulted = True
- def _encoded_items(self):
+ def _encoded_items(self) -> Dict[str, Union[str, bytes]]:
"""
Build a dict of params with minimal encoding needed for the site.
@@ -1272,7 +1272,6 @@
are not supported.
:return: Parameters either in the site encoding, or ASCII strings
- :rtype: dict with values of either str or bytes
"""
params = {}
for key, values in self._params.items():
@@ -1978,12 +1977,8 @@
request_key = repr(sorted(self._encoded_items().items()))
return '{!r}{}{}'.format(self.site, user_key, request_key)
- def _create_file_name(self):
- """
- Return a unique ascii identifier for the cache entry.
-
- :rtype: str (hexadecimal; i.e. characters 0-9 and a-f only)
- """
+ def _create_file_name(self) -> str:
+ """Return a unique ascii identifier for the cache entry."""
return hashlib.sha256(
self._uniquedescriptionstr().encode('utf-8')
).hexdigest()
diff --git a/pywikibot/family.py b/pywikibot/family.py
index b49f88d..171e740 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -19,7 +19,7 @@
import pywikibot
from pywikibot import config
-from pywikibot.backports import Dict, List, Tuple
+from pywikibot.backports import Dict, List, Set, Tuple
from pywikibot.exceptions import FamilyMaintenanceWarning, UnknownFamilyError
from pywikibot.tools import classproperty, deprecated
@@ -803,12 +803,8 @@
"""Return the path to title using index.php with redirects disabled."""
return '{}?title={}&redirect=no'.format(self.path(code), title)
- def interface(self, code):
- """
- Return interface to use for code.
-
- :rtype: str or subclass of BaseSite
- """
+ def interface(self, code) -> str:
+ """Return interface to use for code."""
if code in self.interwiki_removals:
if code in self.codes:
pywikibot.warn('Interwiki removal {} is in {} codes'
@@ -953,14 +949,13 @@
return putText
@property
- def obsolete(self):
+ def obsolete(self) -> Dict[str, Optional[str]]:
"""
Old codes that are not part of the family.
Interwiki replacements override removals for the same code.
:return: mapping of old codes to new codes (or None)
- :rtype: dict
"""
data = {code: None for code in self.interwiki_removals}
data.update(self.interwiki_replacements)
@@ -977,13 +972,11 @@
if new is not None)
@classproperty
- def domains(cls):
+ def domains(cls) -> Set[str]:
"""
Get list of unique domain names included in this family.
These domains may also exist in another family.
-
- :rtype: set of str
"""
return set(cls.langs.values())
@@ -1234,14 +1227,13 @@
return '{}.wikimedia.org'.format(cls.name)
-def AutoFamily(name: str, url: str):
+def AutoFamily(name: str, url: str) -> SingleSiteFamily:
"""
Family that automatically loads the site configuration.
:param name: Name for the family
:param url: API endpoint URL of the wiki
:return: Generated family class
- :rtype: SingleSiteFamily
"""
url = urlparse.urlparse(url)
domain = url.netloc
diff --git a/pywikibot/scripts/generate_user_files.py b/pywikibot/scripts/generate_user_files.py
index 7924328..0281985 100755
--- a/pywikibot/scripts/generate_user_files.py
+++ b/pywikibot/scripts/generate_user_files.py
@@ -5,7 +5,7 @@
moved to pywikibot.scripts folder
"""
#
-# (C) Pywikibot team, 2010-2021
+# (C) Pywikibot team, 2010-2022
#
# Distributed under the terms of the MIT license.
#
@@ -18,6 +18,7 @@
from textwrap import fill
from typing import Optional
+from pywikibot.backports import Tuple
from pywikibot.scripts import _import_with_no_user_config
@@ -105,7 +106,7 @@
default_lang: Optional[str] = 'en',
default_username: Optional[str] = None,
force: bool = False
-):
+) -> Tuple[str, str, str]:
"""
Ask the user for the family, site code and username.
@@ -114,7 +115,6 @@
if the family supports it.
:param default_username: The default username which should be chosen.
:return: The family, site code and username
- :rtype: tuple of three str
"""
known_families = sorted(pywikibot.config.family_files.keys())
if default_family not in known_families:
@@ -251,11 +251,10 @@
return data
-def copy_sections():
+def copy_sections() -> str:
"""Take config sections and copy them to user-config.py.
:return: config text of all selected sections.
- :rtype: str
"""
result = []
sections = parse_sections()
@@ -382,7 +381,7 @@
raise
-def ask_for_dir_change(force):
+def ask_for_dir_change(force) -> Tuple[bool, bool]:
"""Ask whether the base directory is has to be changed.
Only give option for directory change if user-config.py or user-password
@@ -392,7 +391,6 @@
:param force: Skip asking for directory change
:type force: bool
:return: whether user file or password file exists already
- :rtype: tuple of bool
"""
global base_dir
diff --git a/pywikibot/userinterfaces/gui.py b/pywikibot/userinterfaces/gui.py
index c13e761..b75ba4a 100644
--- a/pywikibot/userinterfaces/gui.py
+++ b/pywikibot/userinterfaces/gui.py
@@ -7,7 +7,7 @@
Python 3.6 or highter is required.
"""
#
-# (C) Pywikibot team, 2003-2021
+# (C) Pywikibot team, 2003-2022
#
# Distributed under the terms of the MIT license.
#
@@ -373,7 +373,7 @@
self.pack()
def edit(self, text: str, jumpIndex: Optional[int] = None,
- highlight: Optional[str] = None):
+ highlight: Optional[str] = None) -> Optional[str]:
"""
Provide user with editor to modify text.
@@ -382,7 +382,6 @@
:param highlight: each occurrence of this substring will be highlighted
:return: the modified text, or None if the user didn't save the text
file in his text editor
- :rtype: str or None
"""
self.text = None
# put given text into our textarea
diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py
index 9aa998a..7417155 100755
--- a/pywikibot/userinterfaces/terminal_interface_base.py
+++ b/pywikibot/userinterfaces/terminal_interface_base.py
@@ -488,7 +488,7 @@
pywikibot.error('Invalid response')
def editText(self, text: str, jumpIndex: Optional[int] = None,
- highlight: Optional[str] = None):
+ highlight: Optional[str] = None) -> Optional[str]:
"""Return the text as edited by the user.
Uses a Tkinter edit box because we don't have a console editor
@@ -498,7 +498,6 @@
:param highlight: each occurrence of this substring will be highlighted
:return: the modified text, or None if the user didn't save the text
file in his text editor
- :rtype: str or None
"""
try:
from pywikibot.userinterfaces import gui
diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py
index 4c67edf..ae7c1f5 100755
--- a/pywikibot/userinterfaces/win32_unicode.py
+++ b/pywikibot/userinterfaces/win32_unicode.py
@@ -231,7 +231,6 @@
Get Unicode console objects.
:return: stdin, stdout, stderr, argv
- :rtype: tuple
"""
# Make Unicode console output work independently of the current code page.
# This also fixes https://bugs.python.org/issue1602
diff --git a/pywikibot/version.py b/pywikibot/version.py
index fa621fb..5032ebe 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -1,6 +1,6 @@
"""Module to determine the pywikibot version (tag, revision and date)."""
#
-# (C) Pywikibot team, 2007-2021
+# (C) Pywikibot team, 2007-2022
#
# Distributed under the terms of the MIT license.
#
@@ -22,7 +22,7 @@
import pywikibot
from pywikibot import config
-from pywikibot.backports import List, cache
+from pywikibot.backports import cache, Dict, List, Tuple
from pywikibot.comms.http import fetch
from pywikibot.exceptions import VersionParseError
@@ -78,7 +78,7 @@
@cache
-def getversiondict():
+def getversiondict() -> Dict[str, str]:
"""Get version info for the package.
:return:
@@ -86,7 +86,6 @@
- rev (current revision identifier),
- date (date of current revision),
- hash (git hash for the current revision)
- :rtype: ``dict`` of four ``str``
"""
_program_dir = _get_program_dir()
exceptions = {}
@@ -305,7 +304,7 @@
return (tag, rev, date, hsh)
-def getversion_package(path=None):
+def getversion_package(path=None) -> Tuple[str, str, str, str]:
"""Get version info for an installed package.
:param path: Unused argument
@@ -314,7 +313,6 @@
- rev: '-1 (unknown)'
- date (date the package was installed locally),
- hash (git hash for the current revision of 'pywikibot/__init__.py')
- :rtype: ``tuple`` of four ``str``
"""
hsh = ''
date = get_module_mtime(pywikibot).timetuple()
diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index 89e8958..30b5ef0 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -2163,7 +2163,7 @@
return True
-def page_empty_check(page):
+def page_empty_check(page) -> bool:
"""
Return True if page should be skipped as it is almost empty.
@@ -2171,8 +2171,6 @@
50 characters, and other pages are considered empty if they are not
category pages and contain less than 4 characters excluding interlanguage
links and categories.
-
- :rtype: bool
"""
txt = page.text
# Check if the page is in content namespace
diff --git a/scripts/protect.py b/scripts/protect.py
index a01947a..985442d 100755
--- a/scripts/protect.py
+++ b/scripts/protect.py
@@ -57,7 +57,7 @@
#
# Created by modifying delete.py
#
-# (C) Pywikibot team, 2008-2021
+# (C) Pywikibot team, 2008-2022
#
# Distributed under the terms of the MIT license.
#
@@ -115,11 +115,10 @@
protections=protections)
-def check_protection_level(operation, level, levels, default=None):
+def check_protection_level(operation, level, levels, default=None) -> str:
"""Check if the protection level is valid or ask if necessary.
:return: a valid protection level
- :rtype: str
"""
if level in levels:
return level
diff --git a/scripts/replace.py b/scripts/replace.py
index f19690f..7e4b2da 100755
--- a/scripts/replace.py
+++ b/scripts/replace.py
@@ -459,11 +459,7 @@
.format(entry.title))
def isTitleExcepted(self, title) -> bool:
- """
- Return True if one of the exceptions applies for the given title.
-
- :rtype: bool
- """
+ """Return True if one of the exceptions applies for the given title."""
if 'title' in self.exceptions:
for exc in self.exceptions['title']:
if exc.search(title):
@@ -475,12 +471,8 @@
return False
- def isTextExcepted(self, text):
- """
- Return True if one of the exceptions applies for the given text.
-
- :rtype: bool
- """
+ def isTextExcepted(self, text) -> bool:
+ """Return True if one of the exceptions applies for the given text."""
if 'text-contains' in self.exceptions:
return any(exc.search(text)
for exc in self.exceptions['text-contains'])
diff --git a/scripts/revertbot.py b/scripts/revertbot.py
index a5435fa..0273267 100755
--- a/scripts/revertbot.py
+++ b/scripts/revertbot.py
@@ -20,12 +20,11 @@
'''Example revert bot.'''
- def callback(self, item):
+ def callback(self, item) -> bool:
'''Sample callback function for 'private' revert bot.
:param item: an item from user contributions
:type item: dict
- :rtype: bool
'''
if 'top' in item:
page = pywikibot.Page(self.site, item['title'])
@@ -36,7 +35,7 @@
"""
#
-# (C) Pywikibot team, 2008-2021
+# (C) Pywikibot team, 2008-2022
#
# Distributed under the terms of the MIT license.
#
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index 41286ca..63e3e2e 100755
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -452,8 +452,6 @@
"""Initializer.
:type disamb_page: pywikibot.Page
- :rtype: None
-
"""
self.disamb_page = disamb_page
self.enabled = enabled
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769476
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: Idd5004b3cd589696b77b6a3b4635ab0e434315e4
Gerrit-Change-Number: 769476
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: D3r1ck01 <xsavitar.wiki(a)aol.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769427 )
Change subject: [tests] TestScriptGenerator to script_tests collector
......................................................................
[tests] TestScriptGenerator to script_tests collector
Change-Id: Icf937c0c9d986d82fe68c41929fce065268fae97
---
M tests/script_tests.py
1 file changed, 12 insertions(+), 0 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/script_tests.py b/tests/script_tests.py
index 2c2aef4..e5614ea 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -168,6 +168,18 @@
test_list += ['tests.script_tests.TestScriptSimulate.' + name
for name in tests]
+ tests = (['test__login']
+ + ['test_' + name
+ for name in sorted(script_list)
+ if name != 'login'
+ and name not in failed_dep_script_set
+ and name not in unrunnable_script_set
+ and (enable_autorun_tests or name not in auto_run_script_list)
+ ])
+
+ test_list += ['tests.script_tests.TestScriptGenerator.' + name
+ for name in tests]
+
tests = loader.loadTestsFromNames(test_list)
suite = unittest.TestSuite()
suite.addTests(tests)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769427
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: Icf937c0c9d986d82fe68c41929fce065268fae97
Gerrit-Change-Number: 769427
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769418 )
Change subject: [tests] Provide generator script tests
......................................................................
[tests] Provide generator script tests
A new TestScriptGenerator script test class is introduced which
runs scripts with '-simulate -page:Foo -always'. This may be
expanded by different arguments and various responses later.
Change-Id: I59de76f371955b5154bd1a68a150b2ca936e80e7
---
M tests/script_tests.py
1 file changed, 52 insertions(+), 4 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/script_tests.py b/tests/script_tests.py
index e66c465..2c2aef4 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -213,8 +213,11 @@
timeout = 5 if do_timeout else None
stdout, error = None, None
- if self._results and script_name in self._results:
- error = self._results[script_name]
+ if self._results:
+ if isinstance(self._results, dict):
+ error = self._results.get(script_name)
+ else:
+ error = self._results
if isinstance(error, tuple):
stdout, error = error
@@ -359,9 +362,9 @@
class TestScriptSimulate(DefaultSiteTestCase, PwbTestCase,
metaclass=TestScriptMeta):
- """Test cases for scripts.
+ """Test cases for running scripts with -siumlate.
- This class sets the'user' attribute on every test, thereby ensuring
+ This class sets the 'user' attribute on every test, thereby ensuring
that the test runner has a username for the default site, and so that
Site.login() is called in the test runner, which means that the scripts
run in pwb can automatically login using the saved cookies.
@@ -386,6 +389,51 @@
_timeout = auto_run_script_list
+class TestScriptGenerator(DefaultSiteTestCase, PwbTestCase,
+ metaclass=TestScriptMeta):
+
+ """Test cases for running scripts with a generator."""
+
+ login = True
+
+ _expected_failures = {
+ 'add_text',
+ 'archivebot',
+ 'category',
+ 'change_pagelang',
+ 'claimit',
+ 'commonscat',
+ 'data_ingestion',
+ 'delete',
+ 'djvutext',
+ 'download_dump',
+ 'harvest_template',
+ 'image',
+ 'interwiki',
+ 'listpages',
+ 'movepages',
+ 'newitem',
+ 'pagefromfile',
+ 'protect',
+ 'redirect',
+ 'replicate_wiki',
+ 'solve_disambiguation',
+ 'speedy_delete',
+ 'template',
+ 'templatecount',
+ 'transferbot',
+ 'weblinkchecker',
+ }
+
+ _allowed_failures = [
+ ]
+
+ _arguments = '-simulate -page:Foo -always'
+ _results = ("Working on 'Foo'", '1 pages read')
+ _skip_results = {}
+ _timeout = True
+
+
if __name__ == '__main__': # pragma: no cover
with suppress(SystemExit):
unittest.main()
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769418
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: I59de76f371955b5154bd1a68a150b2ca936e80e7
Gerrit-Change-Number: 769418
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769407 )
Change subject: [tests] let timeout be set by test class
......................................................................
[tests] let timeout be set by test class
Add _timeout attribute to script_tests test classes to determine
whether a timeout should be used. The value must be either a
boolean or a list of script names.
Change-Id: I415ce72be4f699236285078b72cd42d874d0d3e3
---
M tests/script_tests.py
1 file changed, 7 insertions(+), 1 deletion(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/script_tests.py b/tests/script_tests.py
index 3747db5..e66c465 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -206,7 +206,11 @@
cmd = global_args + [script_name] + args
data_in = script_input.get(script_name)
- timeout = 5 if is_autorun else None
+ if isinstance(self._timeout, bool):
+ do_timeout = self._timeout
+ else:
+ do_timeout = script_name in self._timeout
+ timeout = 5 if do_timeout else None
stdout, error = None, None
if self._results and script_name in self._results:
@@ -349,6 +353,7 @@
_arguments = '-help'
_results = None
_skip_results = {}
+ _timeout = False
class TestScriptSimulate(DefaultSiteTestCase, PwbTestCase,
@@ -378,6 +383,7 @@
_arguments = '-simulate'
_results = no_args_expected_results
_skip_results = skip_on_results
+ _timeout = auto_run_script_list
if __name__ == '__main__': # pragma: no cover
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/769407
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: I415ce72be4f699236285078b72cd42d874d0d3e3
Gerrit-Change-Number: 769407
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged