Skip to content

Commit

Permalink
Merge "[flake8] Detect arguments shadowing of Python buildins and ren…
Browse files Browse the repository at this point in the history
…ame them"
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Jan 27, 2024
2 parents fcf3453 + 74b32ab commit bbb1a78
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 53 deletions.
26 changes: 16 additions & 10 deletions pywikibot/data/api/_optionset.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Object representing boolean API option."""
#
# (C) Pywikibot team, 2015-2022
# (C) Pywikibot team, 2015-2024
#
# Distributed under the terms of the MIT license.
#
from __future__ import annotations

from collections.abc import MutableMapping

import pywikibot
from pywikibot.tools import deprecate_arg


__all__ = ['OptionSet']

Expand All @@ -24,30 +27,33 @@ class OptionSet(MutableMapping):
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):
Expand Down
18 changes: 11 additions & 7 deletions pywikibot/date.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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:
Expand Down Expand Up @@ -459,8 +459,9 @@ def _(dec: str, subpattern: str, newpattern: str,


@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::
Expand All @@ -477,6 +478,9 @@ def dh(value: int, pattern: str, encf: encf_type, decf: decf_type,
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
Expand All @@ -490,7 +494,7 @@ def dh(value: int, pattern: str, encf: encf_type, decf: decf_type,
# 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)
Expand All @@ -512,7 +516,7 @@ def dh(value: int, pattern: str, encf: encf_type, decf: decf_type,

@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:
Expand All @@ -525,8 +529,8 @@ def _(value: str, pattern: str, encf: encf_type, decf: decf_type,
'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")
Expand Down
17 changes: 11 additions & 6 deletions pywikibot/site/_apisite.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
from pywikibot.tools import (
MediaWikiVersion,
cached,
deprecate_arg,
deprecated,
issue_deprecation_warning,
merge_unique_dicts,
Expand Down Expand Up @@ -1394,18 +1395,22 @@ def nice_get_address(self, title: str) -> str:
# '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]

Expand Down
26 changes: 16 additions & 10 deletions pywikibot/site/_generators.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand All @@ -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


Expand Down Expand Up @@ -2070,41 +2074,43 @@ def redirectpages(self, total=None):
"""
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):
Expand Down
6 changes: 3 additions & 3 deletions pywikibot/userinterfaces/terminal_interface_base.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down Expand Up @@ -313,10 +313,10 @@ def stream_output(self, text, targetStream=None) -> None:
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.
Expand Down
17 changes: 11 additions & 6 deletions pywikibot/userinterfaces/transliteration.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -1119,15 +1121,18 @@ def __init__(self, encoding: str) -> None:
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
Expand All @@ -1138,7 +1143,7 @@ def transliterate(self, char: str, default: str = '?',
result = prev
# Japanese
elif char == 'ッ':
result = self.transliterate(next)[0]
result = self.transliterate(succ)[0]
elif char in '々仝ヽヾゝゞ〱〲〳〵〴〵':
result = prev
# Lao
Expand Down
4 changes: 2 additions & 2 deletions scripts/blockpageschecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"""
#
# (C) Pywikibot team, 2007-2023
# (C) Pywikibot team, 2007-2024
#
# Distributed under the terms of the MIT license.
#
Expand Down Expand Up @@ -474,7 +474,7 @@ def main(*args: str) -> None:
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()
Expand Down
10 changes: 5 additions & 5 deletions scripts/dataextend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,9 +1282,9 @@ class Analyzer:
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
Expand Down Expand Up @@ -16391,12 +16391,12 @@ def findfloruit(self, html: str):

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

Expand Down
11 changes: 7 additions & 4 deletions tests/site_generators_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,14 +571,15 @@ def test_exturl_usage(self):

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())
Expand All @@ -590,7 +591,8 @@ def test_protectedpages_edit_level(self):
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(
Expand All @@ -601,7 +603,8 @@ def test_protectedpages_edit_level(self):
# 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())
Expand Down

0 comments on commit bbb1a78

Please sign in to comment.