jenkins-bot submitted this change.
[fix] Fix some deepsource issues
https://deepsource.io/gh/xqt/pwb/issues?category=all&analyzer=all&page=1
Change-Id: I2a57f0f726d69c526af2b296e5a3b4856b9499d5
---
M pywikibot/config.py
M pywikibot/data/api.py
M pywikibot/data/sparql.py
M pywikibot/site/_generators.py
M pywikibot/site/_siteinfo.py
M pywikibot/throttle.py
M pywikibot/userinterfaces/win32_unicode.py
M scripts/interwiki.py
M scripts/nowcommons.py
M scripts/patrol.py
M scripts/redirect.py
11 files changed, 121 insertions(+), 102 deletions(-)
diff --git a/pywikibot/config.py b/pywikibot/config.py
index 259520a..09c185f 100644
--- a/pywikibot/config.py
+++ b/pywikibot/config.py
@@ -29,7 +29,7 @@
config2 was renamed to config
"""
#
-# (C) Pywikibot team, 2003-2021
+# (C) Pywikibot team, 2003-2022
#
# Distributed under the terms of the MIT license.
#
@@ -1130,23 +1130,24 @@
_all = False
else:
warning('Unknown arg {} ignored'.format(_arg))
+
for _name in sorted(globals().keys()):
- if _name[0] != '_':
- if not type(globals()[_name]) in [types.FunctionType,
- types.ModuleType]:
- if _all or _name in _modified:
- _value = globals()[_name]
- if _name in _private_values and _value:
- if isinstance(_value, dict):
- _value = '{ ...xxxxxxxx... }'
- elif hasattr(_value, '__dict__'):
- _value = (_value.__class__.__name__
- + '( ...xxxxxxxx... )')
- else:
- _value = repr('xxxxxxxx')
- else:
- _value = repr(_value)
- output('{}={}'.format(_name, _value))
+ if _name[0] != '_' \
+ and not type(globals()[_name]) in [types.FunctionType,
+ types.ModuleType] \
+ and (_all or _name in _modified):
+ _value = globals()[_name]
+
+ if _name not in _private_values or not _value:
+ _value = repr(_value)
+ elif isinstance(_value, dict):
+ _value = '{ ...xxxxxxxx... }'
+ elif hasattr(_value, '__dict__'):
+ _value = (_value.__class__.__name__
+ + '( ...xxxxxxxx... )')
+ else:
+ _value = repr('xxxxxxxx')
+ output('{}={}'.format(_name, _value))
# cleanup all locally-defined variables
for __var in list(globals().keys()):
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index eaad672..87148d5 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1,6 +1,6 @@
"""Interface to Mediawiki's api.php."""
#
-# (C) Pywikibot team, 2007-2021
+# (C) Pywikibot team, 2007-2022
#
# Distributed under the terms of the MIT license.
#
@@ -1233,10 +1233,10 @@
uiprop = self._params.get('uiprop', [])
uiprop = set(uiprop + ['blockinfo', 'hasmsg'])
self['uiprop'] = sorted(uiprop)
- if 'prop' in self._params:
- if self.site.has_extension('ProofreadPage'):
- prop = set(self['prop'] + ['proofread'])
- self['prop'] = sorted(prop)
+ if 'prop' in self._params \
+ and self.site.has_extension('ProofreadPage'):
+ prop = set(self['prop'] + ['proofread'])
+ self['prop'] = sorted(prop)
# When neither 'continue' nor 'rawcontinue' is present and the
# version number is at least 1.25wmf5 we add a dummy rawcontinue
# parameter. Querying siteinfo is save as it adds 'continue'
@@ -2548,9 +2548,9 @@
"""Extract results from resultdata."""
for item in resultdata:
result = self.result(item)
- if self._namespaces:
- if not self._check_result_namespace(result):
- continue
+ if self._namespaces and not self._check_result_namespace(result):
+ continue
+
yield result
if isinstance(item, dict) \
and set(self.continuekey) & set(item.keys()):
@@ -2884,13 +2884,13 @@
takes care of all the cookie stuff. Throws exception on failure.
"""
self.below_mw_1_27 = False
- if hasattr(self, '_waituntil'):
- if datetime.datetime.now() < self._waituntil:
- diff = self._waituntil - datetime.datetime.now()
- pywikibot.warning(
- 'Too many tries, waiting {} seconds before retrying.'
- .format(diff.seconds))
- pywikibot.sleep(diff.seconds)
+ if hasattr(self, '_waituntil') \
+ and datetime.datetime.now() < self._waituntil:
+ diff = self._waituntil - datetime.datetime.now()
+ pywikibot.warning(
+ 'Too many tries, waiting {} seconds before retrying.'
+ .format(diff.seconds))
+ pywikibot.sleep(diff.seconds)
self.site._loginstatus = LoginStatus.IN_PROGRESS
@@ -3017,11 +3017,10 @@
page._pageid = 0 # Non-existent page
else:
# Something is wrong.
- if page.site.sametitle(page.title(), pagedict['title']):
- if 'invalid' in pagedict:
- raise InvalidTitleError('{}: {}'
- .format(page,
- pagedict['invalidreason']))
+ if page.site.sametitle(page.title(), pagedict['title']) \
+ and 'invalid' in pagedict:
+ raise InvalidTitleError('{}: {}'
+ .format(page, pagedict['invalidreason']))
if int(pagedict['ns']) < 0:
raise UnsupportedPageError(page)
raise RuntimeError(
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index 416fb0d..f4b1899 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -1,6 +1,6 @@
"""SPARQL Query interface."""
#
-# (C) Pywikibot team, 2016-2020
+# (C) Pywikibot team, 2016-2022
#
# Distributed under the terms of the MIT license.
#
@@ -11,6 +11,7 @@
from requests.exceptions import Timeout
from pywikibot import Site, config, sleep, warning
+from pywikibot.backports import Dict, List
from pywikibot.comms import http
from pywikibot.exceptions import Error, TimeoutError
@@ -89,8 +90,11 @@
"""
return self.last_response
- def select(self, query: str, full_data: bool = False,
- headers=DEFAULT_HEADERS):
+ def select(self,
+ query: str,
+ full_data: bool = False,
+ headers: Optional[Dict[str, str]] = None
+ ) -> Optional[List[Dict[str, str]]]:
"""
Run SPARQL query and return the result.
@@ -99,37 +103,43 @@
:param query: Query text
:param full_data: Whether return full data objects or only values
- :return: List of query results or None if query failed
"""
- data = self.query(query, headers=headers)
- if data and 'results' in data:
- result = []
- qvars = data['head']['vars']
- for row in data['results']['bindings']:
- values = {}
- for var in qvars:
- if var not in row:
- # var is not available (OPTIONAL is probably used)
- values[var] = None
- elif full_data:
- if row[var]['type'] not in VALUE_TYPES:
- raise ValueError('Unknown type: {}'
- .format(row[var]['type']))
- valtype = VALUE_TYPES[row[var]['type']]
- values[var] = valtype(row[var],
- entity_url=self.entity_url)
- else:
- values[var] = row[var]['value']
- result.append(values)
- return result
- return None
+ if headers is None:
+ headers = DEFAULT_HEADERS
- def query(self, query: str, headers=DEFAULT_HEADERS):
+ data = self.query(query, headers=headers)
+ if not data or 'results' not in data:
+ return None
+
+ result = []
+ qvars = data['head']['vars']
+ for row in data['results']['bindings']:
+ values = {}
+ for var in qvars:
+ if var not in row:
+ # var is not available (OPTIONAL is probably used)
+ values[var] = None
+ elif full_data:
+ if row[var]['type'] not in VALUE_TYPES:
+ raise ValueError('Unknown type: {}'
+ .format(row[var]['type']))
+ valtype = VALUE_TYPES[row[var]['type']]
+ values[var] = valtype(row[var],
+ entity_url=self.entity_url)
+ else:
+ values[var] = row[var]['value']
+ result.append(values)
+ return result
+
+ def query(self, query: str, headers: Optional[Dict[str, str]] = None):
"""
Run SPARQL query and return parsed JSON result.
:param query: Query text
"""
+ if headers is None:
+ headers = DEFAULT_HEADERS
+
url = '{}?query={}'.format(self.endpoint, quote(query))
while True:
try:
@@ -137,12 +147,16 @@
except Timeout:
self.wait()
continue
+
if not self.last_response.text:
- return None
+ break
+
try:
return json.loads(self.last_response.text)
except ValueError:
- return None
+ break
+
+ return None
def wait(self):
"""Determine how long to wait after a failed request."""
@@ -154,12 +168,15 @@
# double the next wait, but do not exceed config.retry_max seconds
self.retry_wait = min(config.retry_max, self.retry_wait * 2)
- def ask(self, query: str, headers=DEFAULT_HEADERS) -> bool:
+ def ask(self, query: str,
+ headers: Optional[Dict[str, str]] = None) -> bool:
"""
Run SPARQL ASK query and return boolean result.
:param query: Query text
"""
+ if headers is None:
+ headers = DEFAULT_HEADERS
data = self.query(query, headers=headers)
return data['boolean']
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index 36872ea..d73e8fa 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-2021
+# (C) Pywikibot team, 2008-2022
#
# Distributed under the terms of the MIT license.
#
@@ -1703,9 +1703,8 @@
"""
# If patrol is not enabled, attr will be set the first time a
# request is done.
- if hasattr(self, '_patroldisabled'):
- if self._patroldisabled:
- return
+ if hasattr(self, '_patroldisabled') and self._patroldisabled:
+ return
if all(_ is None for _ in [rcid, revid, revision]):
raise Error('No rcid, revid or revision provided.')
diff --git a/pywikibot/site/_siteinfo.py b/pywikibot/site/_siteinfo.py
index bc08522..af88274 100644
--- a/pywikibot/site/_siteinfo.py
+++ b/pywikibot/site/_siteinfo.py
@@ -1,6 +1,6 @@
"""Objects representing site info data contents."""
#
-# (C) Pywikibot team, 2008-2020
+# (C) Pywikibot team, 2008-2022
#
# Distributed under the terms of the MIT license.
#
@@ -101,7 +101,7 @@
# query this method to actually get the version number
# Convert boolean props from empty strings to actual boolean values
- if prop in Siteinfo.BOOLEAN_PROPS.keys():
+ if prop in Siteinfo.BOOLEAN_PROPS:
# siprop=namespaces and
# magicwords has properties per item in result
if prop in ('namespaces', 'magicwords'):
diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index 71b7a1d..e9566c8 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -1,6 +1,6 @@
"""Mechanics to slow down wiki read and/or write rate."""
#
-# (C) Pywikibot team, 2008-2021
+# (C) Pywikibot team, 2008-2022
#
# Distributed under the terms of the MIT license.
#
@@ -326,5 +326,4 @@
def get_pid(self, module: str) -> int:
"""Get the global pid if the module is running multiple times."""
- global pid
return pid if self.modules[self._module_hash(module)] > 1 else 0
diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py
index 3101e11..4e12897 100755
--- a/pywikibot/userinterfaces/win32_unicode.py
+++ b/pywikibot/userinterfaces/win32_unicode.py
@@ -1,6 +1,6 @@
"""Stdout, stderr and argv support for unicode."""
#
-# (C) Pywikibot team, 2012-2021
+# (C) Pywikibot team, 2012-2022
#
##############################################
# Support for unicode in Windows cmd.exe
@@ -28,6 +28,9 @@
from ctypes import c_void_p as LPVOID
from ctypes import create_unicode_buffer, sizeof
from io import IOBase, UnsupportedOperation
+from typing import IO
+
+from pywikibot.backports import List, Tuple
OSWIN32 = (sys.platform == 'win32')
@@ -224,7 +227,7 @@
WinError()
-def get_unicode_console():
+def get_unicode_console() -> Tuple[IO, IO, IO, List[str]]:
"""
Get Unicode console objects.
@@ -238,7 +241,7 @@
# and TZOmegaTZIOY
# https://stackoverflow.com/questions/878972/windows-cmd-encoding-change-causes-python-crash/1432462#1432462
- global stdin, stdout, stderr, argv
+ global stdin, stdout, stderr
if not OSWIN32:
return stdin, stdout, stderr, argv
@@ -323,4 +326,4 @@
_complain('exception {!r} while fixing up sys.stdout and sys.stderr'
.format(e))
- return stdin, stdout, stderr, argv
+ return stdin, stdout, stderr
diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index 0d31dc6..ea51ffb 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -328,7 +328,7 @@
"""
#
-# (C) Pywikibot team, 2003-2021
+# (C) Pywikibot team, 2003-2022
#
# Distributed under the terms of the MIT license.
#
@@ -2414,9 +2414,8 @@
elif arg.startswith('-until:'):
until = arg[7:]
else:
- if not genFactory.handle_arg(arg):
- if not singlePageTitle:
- singlePageTitle = arg
+ if not genFactory.handle_arg(arg) and not singlePageTitle:
+ singlePageTitle = arg
# Do not use additional summary with autonomous mode
if iwconf.autonomous:
diff --git a/scripts/nowcommons.py b/scripts/nowcommons.py
index 794f6de..7229c3b 100755
--- a/scripts/nowcommons.py
+++ b/scripts/nowcommons.py
@@ -43,7 +43,7 @@
can be set within a settings file which is scripts.ini by default.
"""
#
-# (C) Pywikibot team, 2006-2021
+# (C) Pywikibot team, 2006-2022
#
# Distributed under the terms of the MIT license.
#
@@ -385,9 +385,11 @@
if arg == '-replacealways':
options['replace'] = True
options['replacealways'] = True
- elif arg.startswith('-'):
- if arg[1:] in ('always', 'replace', 'replaceloose', 'replaceonly'):
- options[arg[1:]] = True
+ elif arg.startswith('-') and arg[1:] in ('always',
+ 'replace',
+ 'replaceloose',
+ 'replaceonly'):
+ options[arg[1:]] = True
bot = NowCommonsDeleteBot(**options)
bot.run()
diff --git a/scripts/patrol.py b/scripts/patrol.py
index f3c5077..0d18317 100755
--- a/scripts/patrol.py
+++ b/scripts/patrol.py
@@ -42,7 +42,7 @@
"""
#
-# (C) Pywikibot team, 2011-2021
+# (C) Pywikibot team, 2011-2022
#
# Distributed under the terms of the MIT license.
#
@@ -307,11 +307,11 @@
.format(username, title))
choice = True
- if not choice and username in self.whitelist:
- if self.in_list(self.whitelist[username], title):
- verbose_output('{} is whitelisted to modify {}'
- .format(username, title))
- choice = True
+ if not choice and username in self.whitelist \
+ and self.in_list(self.whitelist[username], title):
+ verbose_output('{} is whitelisted to modify {}'
+ .format(username, title))
+ choice = True
if self.opt.ask:
choice = pywikibot.input_yn(
@@ -426,10 +426,9 @@
options['whitelist'] = arg[len('-whitelist:'):]
else:
generator = gen_factory.handle_arg(arg)
- if not generator:
- if ':' in arg:
- m = arg.split(':')
- options[m[0]] = m[1]
+ if not generator and ':' in arg:
+ m = arg.split(':')
+ options[m[0]] = m[1]
if usercontribs:
user = pywikibot.User(site, usercontribs)
diff --git a/scripts/redirect.py b/scripts/redirect.py
index a74bb3d..0923db5 100755
--- a/scripts/redirect.py
+++ b/scripts/redirect.py
@@ -66,7 +66,7 @@
¶ms;
"""
#
-# (C) Pywikibot team, 2004-2021
+# (C) Pywikibot team, 2004-2022
#
# Distributed under the terms of the MIT license.
#
@@ -155,10 +155,11 @@
# always print status message after 10000 pages
if readPagesCount % 10000 == 0:
pywikibot.output('{} pages read...'.format(readPagesCount))
- if self.opt.namespaces:
- if pywikibot.Page(self.site, entry.title).namespace() \
- not in self.opt.namespaces:
- continue
+ if self.opt.namespaces and pywikibot.Page(
+ self.site,
+ entry.title).namespace() not in self.opt.namespaces:
+ continue
+
if alsoGetPageTitles:
pageTitles.add(space_to_underscore(pywikibot.Link(entry.title,
self.site)))
To view, visit change 750782. To unsubscribe, or for help writing mail filters, visit settings.