jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/688242 )
Change subject: [IMPR] replace unbound methods with function or use staticmethod decorator ......................................................................
[IMPR] replace unbound methods with function or use staticmethod decorator
Change-Id: I2aed520eb7327423458f9d9c8844fc021fb8521b --- M tests/textlib_tests.py M tests/thread_tests.py M tests/tools_tests.py M tests/ui_tests.py M tests/uploadscript_tests.py M tests/wikibase_edit_tests.py M tests/xmlreader_tests.py 7 files changed, 59 insertions(+), 50 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py index af6eff3..95a1b83 100644 --- a/tests/textlib_tests.py +++ b/tests/textlib_tests.py @@ -48,7 +48,8 @@ self.catresult1 = '[[Category:Cat1]]\n[[Category:Cat2]]\n' super().setUp()
- def contains(self, fn, sn): + @staticmethod + def contains(fn, sn): """Invoke does_text_contain_section().""" return textlib.does_text_contain_section( files[fn], sn) diff --git a/tests/thread_tests.py b/tests/thread_tests.py index b4c667f..dfc7c07 100644 --- a/tests/thread_tests.py +++ b/tests/thread_tests.py @@ -25,7 +25,8 @@ thd_gen.start() self.assertEqual(list(thd_gen), list(iterable))
- def gen_func(self): + @staticmethod + def gen_func(): """Helper method for generator test.""" iterable = 'abcd' yield from iterable diff --git a/tests/tools_tests.py b/tests/tools_tests.py index d890247..9f40e64 100644 --- a/tests/tools_tests.py +++ b/tests/tools_tests.py @@ -41,7 +41,8 @@ with open(cls.base_file, 'rb') as f: cls.original_content = f.read().replace(b'\r\n', b'\n')
- def _get_content(self, *args, **kwargs): + @staticmethod + def _get_content(*args, **kwargs): """Use open_archive and return content using a with-statement.""" with tools.open_archive(*args, **kwargs) as f: return f.read().replace(b'\r\n', b'\n') diff --git a/tests/ui_tests.py b/tests/ui_tests.py index 35e716c..09e4da6 100644 --- a/tests/ui_tests.py +++ b/tests/ui_tests.py @@ -105,10 +105,12 @@
"""pywikibot wrapper class."""
- def init(self): + @staticmethod + def init(): pywikibot.version._get_program_dir()
- def output(self, *args, **kwargs): + @staticmethod + def output(*args, **kwargs): return pywikibot.output(*args, **kwargs)
def request_input(self, *args, **kwargs): @@ -123,13 +125,16 @@ self.inputthread.join() return self.input
- def set_config(self, key, value): + @staticmethod + def set_config(key, value): setattr(pywikibot.config, key, value)
- def set_ui(self, key, value): + @staticmethod + def set_ui(key, value): setattr(pywikibot.ui, key, value)
- def cls(self): + @staticmethod + def cls(): subprocess.run('cls', shell=True)
class pywikibotManager(BaseManager): @@ -538,12 +543,14 @@ return data time.sleep(0.01)
- def setclip(self, text): + @staticmethod + def setclip(text): win32clipboard.OpenClipboard() win32clipboard.SetClipboardData(win32clipboard.CF_UNICODETEXT, text) win32clipboard.CloseClipboard()
- def getclip(self): + @staticmethod + def getclip(): win32clipboard.OpenClipboard() data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) win32clipboard.CloseClipboard() diff --git a/tests/uploadscript_tests.py b/tests/uploadscript_tests.py index 07c09cc..e01b776 100644 --- a/tests/uploadscript_tests.py +++ b/tests/uploadscript_tests.py @@ -11,33 +11,34 @@ from tests.aspects import TestCase
+def match(value: str = '') -> int: + """Create a match object and call get_chunk_site. + + @param value: a chunk size value + @return: chunk size in bytes + """ + option = '-chunked' + if value: + option += ':' + value + match = CHUNK_SIZE_REGEX.match(option) + return get_chunk_size(match) + + class TestUploadScript(TestCase):
"""Test cases for upload."""
net = False
- def match(self, value: str = '') -> int: - """Create a match object and call get_chunk_site. - - @param value: a chunk size value - @return: chunk size in bytes - """ - option = '-chunked' - if value: - option += ':' + value - match = CHUNK_SIZE_REGEX.match(option) - return get_chunk_size(match) - def test_regex(self): """Test CHUNK_SIZE_REGEX and get_chunk_size function.""" - self.assertEqual(self.match(), 1024 ** 2) - self.assertEqual(self.match('12345'), 12345) - self.assertEqual(self.match('4567k'), 4567 * 1000) - self.assertEqual(self.match('7890m'), 7890 * 10 ** 6) - self.assertEqual(self.match('987ki'), 987 * 1024) - self.assertEqual(self.match('654mi'), 654 * 1024 ** 2) - self.assertEqual(self.match('3mike'), 0) + self.assertEqual(match(), 1024 ** 2) + self.assertEqual(match('12345'), 12345) + self.assertEqual(match('4567k'), 4567 * 1000) + self.assertEqual(match('7890m'), 7890 * 10 ** 6) + self.assertEqual(match('987ki'), 987 * 1024) + self.assertEqual(match('654mi'), 654 * 1024 ** 2) + self.assertEqual(match('3mike'), 0)
if __name__ == '__main__': # pragma: no cover diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py index 9d5b69b..8b5ad44 100644 --- a/tests/wikibase_edit_tests.py +++ b/tests/wikibase_edit_tests.py @@ -205,7 +205,8 @@ login = True write = True
- def _clean_item(self, repo, prop: str): + @staticmethod + def _clean_item(repo, prop: str): """ Return an item without any existing claims of the given property.
diff --git a/tests/xmlreader_tests.py b/tests/xmlreader_tests.py index 4d503bf..ad968b3 100644 --- a/tests/xmlreader_tests.py +++ b/tests/xmlreader_tests.py @@ -12,26 +12,21 @@ from tests.aspects import TestCase
-class XmlReaderTestCase(TestCase): - - """XML Reader test cases.""" - - net = False - - def _get_entries(self, filename, **kwargs): - """Get all entries via XmlDump.""" - entries = list(xmlreader.XmlDump(join_xml_data_path(filename), - **kwargs).parse()) - return entries +def get_entries(filename, **kwargs): + """Get all entries via XmlDump.""" + return list(xmlreader.XmlDump(join_xml_data_path(filename), + **kwargs).parse())
-class ExportDotThreeTestCase(XmlReaderTestCase): +class ExportDotThreeTestCase(TestCase):
"""XML export version 0.3 tests."""
+ net = False + def test_XmlDumpAllRevs(self): """Test loading all revisions.""" - pages = self._get_entries('article-pear.xml', allrevisions=True) + pages = get_entries('article-pear.xml', allrevisions=True) self.assertLength(pages, 4) self.assertEqual('Automated conversion', pages[0].comment) self.assertEqual('Pear', pages[0].title) @@ -42,7 +37,7 @@
def test_XmlDumpFirstRev(self): """Test loading the first revision.""" - pages = self._get_entries('article-pear.xml', allrevisions=False) + pages = get_entries('article-pear.xml', allrevisions=False) self.assertLength(pages, 1) self.assertEqual('Automated conversion', pages[0].comment) self.assertEqual('Pear', pages[0].title) @@ -52,15 +47,15 @@
def test_XmlDumpRedirect(self): """Test XmlDump correctly parsing whether a page is a redirect.""" - self._get_entries('article-pyrus.xml', allrevisions=True) + get_entries('article-pyrus.xml', allrevisions=True) pages = list(xmlreader.XmlDump( join_xml_data_path('article-pyrus.xml')).parse()) self.assertTrue(pages[0].isredirect)
def _compare(self, previous, variant, all_revisions): """Compare the tested variant with the previous (if not None).""" - entries = self._get_entries('article-pyrus' + variant, - allrevisions=all_revisions) + entries = get_entries('article-pyrus' + variant, + allrevisions=all_revisions) result = [entry.__dict__ for entry in entries] if previous: self.assertEqual(previous, result) @@ -83,13 +78,15 @@ self._compare_variants(False)
-class ExportDotTenTestCase(XmlReaderTestCase): +class ExportDotTenTestCase(TestCase):
"""XML export version 0.10 tests."""
+ net = False + def test_pair(self): """Test reading the main page/user talk page pair file.""" - entries = self._get_entries('pair-0.10.xml', allrevisions=True) + entries = get_entries('pair-0.10.xml', allrevisions=True) self.assertLength(entries, 4) self.assertTrue(all(entry.username == 'Carlossuarez46' for entry in entries)) @@ -115,7 +112,7 @@
def test_edit_summary_decoding(self): """Test edit summaries are decoded.""" - entries = self._get_entries('pair-0.10.xml', allrevisions=True) + entries = get_entries('pair-0.10.xml', allrevisions=True) articles = [entry for entry in entries if entry.ns == '0']
# It does not decode the edit summary
pywikibot-commits@lists.wikimedia.org