jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/991917?usp=email )
Change subject: [flake8] Detect arguments shadowing of Python buildins and rename them ......................................................................
[flake8] Detect arguments shadowing of Python buildins and rename them
Change-Id: Ia3bc1df5bde80e90bceede3500421a9e530fad4b --- M pywikibot/data/api/_optionset.py M pywikibot/date.py M pywikibot/site/_apisite.py M pywikibot/site/_generators.py M pywikibot/userinterfaces/terminal_interface_base.py M pywikibot/userinterfaces/transliteration.py M scripts/blockpageschecker.py M scripts/dataextend.py M tests/site_generators_tests.py 9 files changed, 93 insertions(+), 55 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api/_optionset.py b/pywikibot/data/api/_optionset.py index 500da9c..d31f0e4 100644 --- a/pywikibot/data/api/_optionset.py +++ b/pywikibot/data/api/_optionset.py @@ -1,6 +1,6 @@ """Object representing boolean API option.""" # -# (C) Pywikibot team, 2015-2022 +# (C) Pywikibot team, 2015-2024 # # Distributed under the terms of the MIT license. # @@ -8,6 +8,9 @@
from collections.abc import MutableMapping
+import pywikibot +from pywikibot.tools import deprecate_arg +
__all__ = ['OptionSet']
@@ -24,30 +27,33 @@ None and after setting it, any site (even None) will fail. """
- def __init__(self, site=None, + @deprecate_arg('dict', 'data') # since 9.0 + def __init__(self, + site: pywikibot.site.APISite | None = None, module: str | None = None, param: str | None = None, - dict: dict | None = None) -> None: - """ - Initializer. + data: dict | None = None) -> None: + """Initializer.
If a site is given, the module and param must be given too.
+ .. versionchanged:: 9.0 + *dict* parameter was renamed to *data*. + :param site: The associated site - :type site: pywikibot.site.APISite or None :param module: The module name which is used by paraminfo. (Ignored when site is None) :param param: The parameter name inside the module. That parameter must have a 'type' entry. (Ignored when site is None) - :param dict: The initializing dict which is used for - :py:obj:`from_dict` + :param data: The initializing data dict which is used for + :meth:`from_dict` """ self._site_set = False self._enabled = set() self._disabled = set() self._set_site(site, module, param) - if dict: - self.from_dict(dict) + if data: + self.from_dict(data)
def _set_site(self, site, module: str, param: str, clear_invalid: bool = False): diff --git a/pywikibot/date.py b/pywikibot/date.py index d79aa47..4a85afc 100644 --- a/pywikibot/date.py +++ b/pywikibot/date.py @@ -1,6 +1,6 @@ """Date data and manipulation module.""" # -# (C) Pywikibot team, 2003-2023 +# (C) Pywikibot team, 2003-2024 # # Distributed under the terms of the MIT license. # @@ -26,7 +26,7 @@ ) from pywikibot.site import BaseSite from pywikibot.textlib import NON_LATIN_DIGITS -from pywikibot.tools import first_lower, first_upper +from pywikibot.tools import deprecate_arg, first_lower, first_upper
if TYPE_CHECKING: @@ -459,8 +459,9 @@
@singledispatch +@deprecate_arg('filter', 'filter_func') # since 9.0 def dh(value: int, pattern: str, encf: encf_type, decf: decf_type, - filter: Callable[[int], bool] | None = None) -> str: + filter_func: Callable[[int], bool] | None = None) -> str: """Function to help with year parsing.
Usually it will be used as a lambda call in a map:: @@ -477,6 +478,9 @@
This function is a complement of decf.
+ .. versionchanged:: 9.0 + *filter* parameter was renamed to *filter_func* + :param decf: Converts a tuple/list of non-negative integers found in the original value string @@ -490,7 +494,7 @@ # Encode an integer value into a textual form. # This will be called from outside as well as recursivelly to verify # parsed value - if filter and not filter(value): + if filter_func and not filter_func(value): raise ValueError(f'value {value} is not allowed')
params = encf(value) @@ -512,7 +516,7 @@
@dh.register(str) def _(value: str, pattern: str, encf: encf_type, decf: decf_type, - filter: Callable[[int], bool] | None = None) -> int: + filter_func: Callable[[int], bool] | None = None) -> int: compPattern, _strPattern, decoders = escapePattern2(pattern) m = compPattern.match(value) if m: @@ -525,8 +529,8 @@ 'Decoder must not return a string!'
# recursive call to re-encode and see if we get the original - # (may through filter exception) - if value == dh(decValue, pattern, encf, decf, filter): + # (may through filter_func exception) + if value == dh(decValue, pattern, encf, decf, filter_func): return decValue
raise ValueError("reverse encoding didn't match") diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py index 9aa95d4..53ec3a3 100644 --- a/pywikibot/site/_apisite.py +++ b/pywikibot/site/_apisite.py @@ -1,6 +1,6 @@ """Objects representing API interface to MediaWiki site.""" # -# (C) Pywikibot team, 2008-2023 +# (C) Pywikibot team, 2008-2024 # # Distributed under the terms of the MIT license. # @@ -73,6 +73,7 @@ from pywikibot.tools import ( MediaWikiVersion, cached, + deprecate_arg, deprecated, issue_deprecation_warning, merge_unique_dicts, @@ -1394,18 +1395,22 @@ # 'title' is expected to be URL-encoded already return self.siteinfo['articlepath'].replace('$1', title)
- def namespace(self, num: int, all: bool = False) -> str | Namespace: + @deprecate_arg('all', 'all_ns') # since 9.0 + def namespace(self, num: int, all_ns: bool = False) -> str | Namespace: """Return string containing local name of namespace 'num'.
- If optional argument 'all' is true, return all recognized + If optional argument *all_ns* is true, return all recognized values for this namespace.
+ .. versionchanged:: 9.0 + *all* parameter was renamed to *all_ns*. + :param num: Namespace constant. - :param all: If True return a Namespace object. Otherwise - return the namespace name. - :return: local name or Namespace object + :param all_ns: If True return a :class:`Namespace` object. + Otherwise return the namespace name. + :return: local name or :class:`Namespace` object """ - if all: + if all_ns: return self.namespaces[num] return self.namespaces[num][0]
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py index 96be68e..1b36d19 100644 --- a/pywikibot/site/_generators.py +++ b/pywikibot/site/_generators.py @@ -1,6 +1,6 @@ """Objects representing API generators to MediaWiki site.""" # -# (C) Pywikibot team, 2008-2023 +# (C) Pywikibot team, 2008-2024 # # Distributed under the terms of the MIT license. # @@ -26,7 +26,11 @@ ) from pywikibot.site._decorators import need_right from pywikibot.site._namespace import NamespaceArgType -from pywikibot.tools import is_ip_address, issue_deprecation_warning +from pywikibot.tools import ( + deprecate_arg, + is_ip_address, + issue_deprecation_warning, +) from pywikibot.tools.itertools import filter_unique
@@ -2070,41 +2074,43 @@ """ return self.querypage('Listredirects', total)
+ @deprecate_arg('type', 'protect_type') def protectedpages( self, namespace=0, - type: str = 'edit', + protect_type: str = 'edit', level: str | bool = False, total=None ): - """ - Return protected pages depending on protection level and type. + """Return protected pages depending on protection level and type.
For protection types which aren't 'create' it uses :py:obj:`APISite.allpages`, while it uses for 'create' the 'query+protectedtitles' module.
+ .. versionchanged:: 9.0 + *type* parameter was renamed to *protect_type*. .. seealso:: :api:`Protectedtitles`
:param namespace: The searched namespace. :type namespace: int or Namespace or str - :param type: The protection type to search for (default 'edit'). - :type type: str + :param protect_type: The protection type to search for + (default 'edit'). :param level: The protection level (like 'autoconfirmed'). If False it shows all protection levels. :return: The pages which are protected. :rtype: typing.Iterable[pywikibot.Page] """ namespaces = self.namespaces.resolve(namespace) - # always assert that, so we are be sure that type could be 'create' + # always assert, so we are be sure that protect_type could be 'create' assert 'create' in self.protection_types(), \ "'create' should be a valid protection type." - if type == 'create': + if protect_type == 'create': return self._generator( api.PageGenerator, type_arg='protectedtitles', namespaces=namespaces, gptlevel=level, total=total) return self.allpages(namespace=namespaces[0], protect_level=level, - protect_type=type, total=total) + protect_type=protect_type, total=total)
def pages_with_property(self, propname: str, *, total: int | None = None): diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py index 8346abb..83f7f4a 100644 --- a/pywikibot/userinterfaces/terminal_interface_base.py +++ b/pywikibot/userinterfaces/terminal_interface_base.py @@ -1,6 +1,6 @@ """Base for terminal user interfaces.""" # -# (C) Pywikibot team, 2003-2023 +# (C) Pywikibot team, 2003-2024 # # Distributed under the terms of the MIT license. # @@ -313,10 +313,10 @@ if char == '?' and text[i] != '?': try: transliterated = transliterator.transliterate( - text[i], default='?', prev=prev, next=text[i + 1]) + text[i], default='?', prev=prev, succ=text[i + 1]) except IndexError: transliterated = transliterator.transliterate( - text[i], default='?', prev=prev, next=' ') + text[i], default='?', prev=prev, succ=' ') # transliteration was successful. The replacement # could consist of multiple letters. # mark the transliterated letters in yellow. diff --git a/pywikibot/userinterfaces/transliteration.py b/pywikibot/userinterfaces/transliteration.py index 85755fa..0bbc083 100644 --- a/pywikibot/userinterfaces/transliteration.py +++ b/pywikibot/userinterfaces/transliteration.py @@ -1,11 +1,13 @@ """Module to transliterate text.""" # -# (C) Pywikibot team, 2006-2022 +# (C) Pywikibot team, 2006-2024 # # Distributed under the terms of the MIT license. # from __future__ import annotations
+from pywikibot.tools import deprecate_arg +
#: Non latin digits used by the framework NON_LATIN_DIGITS = { @@ -1119,15 +1121,18 @@ trans[char] = value self.trans = trans
+ @deprecate_arg('next', 'succ') # since 9.0 def transliterate(self, char: str, default: str = '?', - prev: str = '-', next: str = '-') -> str: - """ - Transliterate the character. + prev: str = '-', succ: str = '-') -> str: + """Transliterate the character. + + .. versionchanged:: 9.0 + *next* parameter was renamed to *succ*.
:param char: The character to transliterate. :param default: The character used when there is no transliteration. :param prev: The previous character - :param next: The next character + :param succ: The succeeding character :return: The transliterated character which may be an empty string """ result = default @@ -1138,7 +1143,7 @@ result = prev # Japanese elif char == 'ッ': - result = self.transliterate(next)[0] + result = self.transliterate(succ)[0] elif char in '々仝ヽヾゝゞ〱〲〳〵〴〵': result = prev # Lao diff --git a/scripts/blockpageschecker.py b/scripts/blockpageschecker.py index 438cbc8..154dba0 100755 --- a/scripts/blockpageschecker.py +++ b/scripts/blockpageschecker.py @@ -44,7 +44,7 @@
""" # -# (C) Pywikibot team, 2007-2023 +# (C) Pywikibot team, 2007-2024 # # Distributed under the terms of the MIT license. # @@ -474,7 +474,7 @@ elif arg in ('-protectedpages', '-moveprotected'): protect_type = 'move' if option.startswith('move') else 'edit' generator = site.protectedpages(namespace=int(value or 0), - type=protect_type) + protect_type=protect_type)
if not generator: generator = gen_factory.getCombinedGenerator() diff --git a/scripts/dataextend.py b/scripts/dataextend.py index 0c97fe9..6c03a22 100755 --- a/scripts/dataextend.py +++ b/scripts/dataextend.py @@ -1282,9 +1282,9 @@ TAGRE = re.compile('<[^<>]*>') SCRIPTRE = re.compile('(?s)<script.*?</script>')
- def __init__(self, id, data=None, item=None, bot=None): + def __init__(self, ident, data=None, item=None, bot=None): """Initializer.""" - self.id = id + self.id = ident self.data = defaultdict(dict) if data is None else data self.dbname = None self.urlbase = None @@ -16388,12 +16388,12 @@
class UrlAnalyzer(Analyzer):
- def __init__(self, id, data=None, item=None, bot=None): + def __init__(self, ident, data=None, item=None, bot=None): """Initializer.""" if data is None: data = defaultdict(dict) - super().__init__(id.split('/', 3)[-1], data, item, bot) - self.urlbase = id + super().__init__(ident.split('/', 3)[-1], data, item, bot) + self.urlbase = ident self.dbproperty = None self.isurl = True
diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py index f73189a..1f74014 100755 --- a/tests/site_generators_tests.py +++ b/tests/site_generators_tests.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """Tests for generators of the site module.""" # -# (C) Pywikibot team, 2008-2023 +# (C) Pywikibot team, 2008-2024 # # Distributed under the terms of the MIT license. # @@ -571,14 +571,15 @@
def test_protectedpages_create(self): """Test that protectedpages returns protected page titles.""" - pages = list(self.get_site().protectedpages(type='create', total=10)) + pages = list(self.get_site().protectedpages(protect_type='create', + total=10)) # Do not check for the existence of pages as they might exist (T205883) self.assertLessEqual(len(pages), 10)
def test_protectedpages_edit(self): """Test that protectedpages returns protected pages.""" site = self.get_site() - pages = list(site.protectedpages(type='edit', total=10)) + pages = list(site.protectedpages(protect_type='edit', total=10)) for page in pages: self.assertTrue(page.exists()) self.assertIn('edit', page.protection()) @@ -590,7 +591,8 @@ levels = set() all_levels = site.protection_levels().difference(['']) for level in all_levels: - if list(site.protectedpages(type='edit', level=level, total=1)): + if list(site.protectedpages(protect_type='edit', level=level, + total=1)): levels.add(level) if not levels: self.skipTest( @@ -602,7 +604,8 @@ # if only one level found, then use any other except that level = next(iter(all_levels.difference([level]))) invalid_levels = all_levels.difference([level]) - pages = list(site.protectedpages(type='edit', level=level, total=10)) + pages = list(site.protectedpages(protect_type='edit', level=level, + total=10)) for page in pages: self.assertTrue(page.exists()) self.assertIn('edit', page.protection())
pywikibot-commits@lists.wikimedia.org