jenkins-bot submitted this change.

View Change

Approvals: JJMC89: Looks good to me, approved jenkins-bot: Verified
sort import and ugrade syntax

using isort and pyupgrade

Change-Id: I54c41e0edc98c3de1d14e44d6380a45645e4538f
---
M docs/conf.py
M pwb.py
M pywikibot/__init__.py
M pywikibot/_wbtypes.py
M pywikibot/backports.py
M pywikibot/bot.py
M pywikibot/bot_choice.py
M pywikibot/comms/eventstreams.py
M pywikibot/config.py
M pywikibot/cosmetic_changes.py
M pywikibot/data/api.py
M pywikibot/date.py
M pywikibot/diff.py
M pywikibot/echo.py
M pywikibot/families/wikisource_family.py
M pywikibot/i18n.py
M pywikibot/interwiki_graph.py
M pywikibot/logentries.py
M pywikibot/logging.py
M pywikibot/login.py
M pywikibot/page/__init__.py
M pywikibot/page/_decorators.py
M pywikibot/pagegenerators.py
M pywikibot/proofreadpage.py
M pywikibot/scripts/generate_family_file.py
M pywikibot/scripts/generate_user_files.py
M pywikibot/scripts/version.py
M pywikibot/site/__init__.py
M pywikibot/site/_apisite.py
M pywikibot/site/_datasite.py
M pywikibot/site/_tokenwallet.py
M pywikibot/site_detect.py
M pywikibot/specialbots/_unlink.py
M pywikibot/specialbots/_upload.py
M pywikibot/textlib.py
M pywikibot/throttle.py
M pywikibot/tools/__init__.py
M pywikibot/tools/chars.py
M pywikibot/tools/djvu.py
M pywikibot/tools/formatter.py
M pywikibot/userinterfaces/_interface_base.py
M pywikibot/userinterfaces/buffer_interface.py
M pywikibot/userinterfaces/terminal_interface_base.py
M pywikibot/userinterfaces/win32_unicode.py
M pywikibot/version.py
M scripts/add_text.py
M scripts/archive/casechecker.py
M scripts/archive/makecat.py
M scripts/archive/match_images.py
M scripts/archive/piper.py
M scripts/archivebot.py
M scripts/blockpageschecker.py
M scripts/category.py
M scripts/category_redirect.py
M scripts/clean_sandbox.py
M scripts/commonscat.py
M scripts/coordinate_import.py
M scripts/data_ingestion.py
M scripts/delete.py
M scripts/fixing_redirects.py
M scripts/image.py
M scripts/interwiki.py
M scripts/maintenance/cache.py
M scripts/nowcommons.py
M scripts/pagefromfile.py
M scripts/redirect.py
M scripts/reflinks.py
M scripts/revertbot.py
M scripts/weblinkchecker.py
M scripts/welcome.py
M setup.py
M tests/add_text_tests.py
M tests/archive/isbn_tests.py
M tests/aspects.py
M tests/bot_tests.py
M tests/cosmetic_changes_tests.py
M tests/data_ingestion_tests.py
M tests/date_tests.py
M tests/family_tests.py
M tests/generate_family_file_tests.py
M tests/generate_user_files_tests.py
M tests/http_tests.py
M tests/i18n_tests.py
M tests/l10n_tests.py
M tests/link_tests.py
M tests/plural_tests.py
M tests/script_tests.py
M tests/site_detect_tests.py
M tests/site_login_logout_tests.py
M tests/site_tests.py
M tests/textlib_tests.py
M tests/token_tests.py
M tests/tools_tests.py
M tests/ui_tests.py
M tests/utils.py
M tests/wikibase_tests.py
96 files changed, 172 insertions(+), 205 deletions(-)

diff --git a/docs/conf.py b/docs/conf.py
index d4e3770..2a89d4f 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -24,7 +24,6 @@
import re
import sys
import warnings
-
from os.path import abspath, dirname, join


diff --git a/pwb.py b/pwb.py
index 76f3614..fd1ae8a 100755
--- a/pwb.py
+++ b/pwb.py
@@ -44,12 +44,12 @@
import os
import sys
import types
-
from difflib import get_close_matches
from importlib import import_module
from time import sleep
from warnings import warn

+
try:
from pathlib import Path
except ImportError as e:
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 04799fd..c6d21a4 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -11,7 +11,6 @@
import sys
import threading
import time
-
from contextlib import suppress
from decimal import Decimal
from queue import Queue
@@ -34,12 +33,12 @@
)
from pywikibot._wbtypes import WbRepresentation as _WbRepresentation
from pywikibot.backports import (
- cache,
- removesuffix,
Callable,
Dict,
List,
Tuple,
+ cache,
+ removesuffix,
)
from pywikibot.bot import (
Bot,
@@ -67,8 +66,7 @@
warning,
)
from pywikibot.site import APISite, BaseSite, DataSite
-from pywikibot.tools import classproperty
-from pywikibot.tools import normalize_username
+from pywikibot.tools import classproperty, normalize_username
from pywikibot.tools.formatter import color_format


diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py
index 744d8af..8484d5c 100644
--- a/pywikibot/_wbtypes.py
+++ b/pywikibot/_wbtypes.py
@@ -6,10 +6,11 @@
#
import abc
import json
-from typing import Any, Optional, TYPE_CHECKING
+from typing import TYPE_CHECKING, Any, Optional

from pywikibot.backports import Dict

+
if TYPE_CHECKING:
from pywikibot.site import DataSite

diff --git a/pywikibot/backports.py b/pywikibot/backports.py
index 54c15e0..8897401 100644
--- a/pywikibot/backports.py
+++ b/pywikibot/backports.py
@@ -7,6 +7,7 @@
import sys
from typing import Any

+
PYTHON_VERSION = sys.version_info[:3]

# functools.cache
@@ -48,7 +49,7 @@
from typing import DefaultDict # type: ignore[misc]
else:
from collections import ( # type: ignore[misc] # noqa: N812
- defaultdict as DefaultDict
+ defaultdict as DefaultDict,
)


@@ -78,7 +79,12 @@
)
else:
from collections.abc import (
- Container, Generator, Iterable, Iterator, Mapping, Sequence,
+ Container,
+ Generator,
+ Iterable,
+ Iterator,
+ Mapping,
+ Sequence,
)
from re import Match, Pattern
Dict = dict # type: ignore[misc]
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 00fe1fb..eeb1edb 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -98,7 +98,6 @@
import time
import warnings
import webbrowser
-
from collections import Counter
from collections.abc import Generator
from contextlib import closing
@@ -106,7 +105,7 @@
from importlib import import_module
from pathlib import Path
from textwrap import fill
-from typing import Any, Optional, TYPE_CHECKING, Union
+from typing import TYPE_CHECKING, Any, Optional, Union
from warnings import warn

import pywikibot
@@ -181,6 +180,7 @@
from pywikibot.tools._logging import LoggingFormatter
from pywikibot.tools.formatter import color_format

+
if TYPE_CHECKING:
from pywikibot.site import BaseSite

@@ -1084,7 +1084,7 @@
command_log_filename = config.datafilepath('logs', 'commands.log')
try:
command_log_file = codecs.open(command_log_filename, 'a', 'utf-8')
- except IOError:
+ except OSError:
command_log_file = codecs.open(command_log_filename, 'w', 'utf-8')

with closing(command_log_file):
diff --git a/pywikibot/bot_choice.py b/pywikibot/bot_choice.py
index 9fb1660..309fb94 100755
--- a/pywikibot/bot_choice.py
+++ b/pywikibot/bot_choice.py
@@ -5,15 +5,12 @@
# Distributed under the terms of the MIT license.
#
import re
-
from abc import ABC, abstractmethod
from textwrap import fill
from typing import Any, Optional

import pywikibot
-
from pywikibot.backports import Iterable, Sequence
-
from pywikibot.tools import deprecated, issue_deprecation_warning


diff --git a/pywikibot/comms/eventstreams.py b/pywikibot/comms/eventstreams.py
index 3776e96..71f6d94 100644
--- a/pywikibot/comms/eventstreams.py
+++ b/pywikibot/comms/eventstreams.py
@@ -15,7 +15,6 @@
# Distributed under the terms of the MIT license.
#
import json
-import socket
from functools import partial
from typing import Optional

@@ -295,7 +294,7 @@
"Install it with 'pip install \"sseclient>=0.0.18\"'")
try:
event = next(self.source)
- except (ProtocolError, socket.error, httplib.IncompleteRead) as e:
+ except (ProtocolError, OSError, httplib.IncompleteRead) as e:
warning('Connection error: {}.\n'
'Try to re-establish connection.'.format(e))
del self.source
diff --git a/pywikibot/config.py b/pywikibot/config.py
index 7548832..c2f6788 100644
--- a/pywikibot/config.py
+++ b/pywikibot/config.py
@@ -50,8 +50,15 @@
from zipfile import ZipFile, is_zipfile

from pywikibot.__metadata__ import __version__ as pwb_version
-from pywikibot.backports import (DefaultDict, Dict, FrozenSet, List, Mapping,
- Tuple, removesuffix)
+from pywikibot.backports import (
+ DefaultDict,
+ Dict,
+ FrozenSet,
+ List,
+ Mapping,
+ Tuple,
+ removesuffix,
+)
from pywikibot.logging import error, output, warning
from pywikibot.tools import deprecated

@@ -330,7 +337,7 @@
elif win_version in (6, 10):
sub_dir = ['AppData', 'Roaming']
else:
- raise WindowsError( # type: ignore[name-defined]
+ raise OSError( # type: ignore[name-defined]
'Windows version {} not supported yet.'
.format(win_version)
)
@@ -911,7 +918,7 @@
# Remove any trailing character, which should be a quote or space
# and then remove all whitespace.
return cmd[:-1].strip()
- except WindowsError as e: # type: ignore[name-defined]
+ except OSError as e: # type: ignore[name-defined]
# Catch any key lookup errors
output('Unable to detect program for file extension "{}": {!r}'
.format(extension, e))
diff --git a/pywikibot/cosmetic_changes.py b/pywikibot/cosmetic_changes.py
index 621da14..09f12dc 100755
--- a/pywikibot/cosmetic_changes.py
+++ b/pywikibot/cosmetic_changes.py
@@ -55,7 +55,6 @@
# Distributed under the terms of the MIT license.
#
import re
-
from enum import IntEnum
from typing import Any, Union
from urllib.parse import urlparse, urlunparse
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index ca9f047..be4b0a1 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -12,7 +12,6 @@
import pprint
import re
import traceback
-
from collections.abc import Container, MutableMapping, Sized
from email.generator import BytesGenerator
from email.mime.multipart import MIMEMultipart as MIMEMultipartOrig
@@ -1096,7 +1095,7 @@
args |= set(getfullargspec(super_cls.__init__).args)
else:
raise ValueError('Request was not a super class of '
- '{0!r}'.format(cls))
+ '{!r}'.format(cls))
args -= {'self'}
old_kwargs = set(kwargs)
# all kwargs defined above but not in args indicate 'kwargs' mode
@@ -1933,7 +1932,7 @@
:return: base directory path for cache entries
"""
path = os.path.join(config.base_dir,
- 'apicache-py{0:d}'.format(PYTHON_VERSION[0]))
+ 'apicache-py{:d}'.format(PYTHON_VERSION[0]))
cls._make_dir(path)
cls._get_cache_dir = classmethod(lambda c: path) # cache the result
return path
@@ -2011,7 +2010,7 @@
.format(self.__class__.__name__, filename,
uniquedescr), _logger)
return True
- except IOError:
+ except OSError:
# file not found
return False
except Exception as e:
diff --git a/pywikibot/date.py b/pywikibot/date.py
index 1763f63..48d646a 100644
--- a/pywikibot/date.py
+++ b/pywikibot/date.py
@@ -7,7 +7,6 @@
import calendar
import datetime
import re
-
from collections import abc, defaultdict
from contextlib import suppress
from functools import singledispatch
@@ -16,9 +15,6 @@

import pywikibot.site
from pywikibot import Site
-from pywikibot.textlib import NON_LATIN_DIGITS
-from pywikibot.tools import first_lower, first_upper
-
from pywikibot.backports import (
Any,
Callable,
@@ -30,6 +26,9 @@
Sequence,
Tuple,
)
+from pywikibot.textlib import NON_LATIN_DIGITS
+from pywikibot.tools import first_lower, first_upper
+

#
# Different collections of well known formats
@@ -407,7 +406,7 @@

if len(subpattern) == 3:
# enforce mandatory field size
- newpattern += '([%s]{%s})' % (dec[0], subpattern[1])
+ newpattern += '([{}]{{{}}})'.format(dec[0], subpattern[1])
# add the number of required digits as the last (4th)
# part of the tuple
decoders.append(dec + (int(s[1]),)) # type: ignore
diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index d4eec47..8b5c0d2 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -8,7 +8,7 @@
import math
from collections import abc
from difflib import ( # type: ignore[attr-defined]
- _format_range_unified as format_range_unified
+ _format_range_unified as format_range_unified,
)
from itertools import zip_longest
from typing import Optional, Union
@@ -530,7 +530,7 @@
else: # choice == '?':
pywikibot.output(color_format(
'{purple}{0}{default}', '\n'.join(
- '{0} -> {1}'.format(answer, help_msg[answer])
+ '{} -> {}'.format(answer, help_msg[answer])
for answer in answers)))

def apply(self) -> List[str]:
diff --git a/pywikibot/echo.py b/pywikibot/echo.py
index 62a22e4..8b0c7b1 100644
--- a/pywikibot/echo.py
+++ b/pywikibot/echo.py
@@ -9,6 +9,7 @@
import pywikibot
from pywikibot.backports import Dict

+
NOTIFICATION_CLASS_TYPE = Type['Notification']


diff --git a/pywikibot/families/wikisource_family.py b/pywikibot/families/wikisource_family.py
index 66fb5e6..7f32b48 100644
--- a/pywikibot/families/wikisource_family.py
+++ b/pywikibot/families/wikisource_family.py
@@ -51,7 +51,7 @@
# the main page, so using 'wikisource.org'
@classproperty
def langs(cls):
- cls.langs = super(Family, cls).langs
+ cls.langs = super().langs
cls.langs['mul'] = cls.domain
cls.langs['beta'] = 'en.wikisource.beta.wmflabs.org'
return cls.langs
@@ -59,7 +59,7 @@
# Need to explicitly inject the beta domain
@classproperty
def domains(cls):
- cls.domains = super(Family, cls).domains
+ cls.domains = super().domains
cls.domains.append(cls.langs['beta'])
return cls.domains

@@ -67,7 +67,7 @@
# the main page, so using mul alias, see T114574 and T241413
@classproperty
def code_aliases(cls):
- cls.code_aliases = super(Family, cls).code_aliases.copy()
+ cls.code_aliases = super().code_aliases.copy()
aliases = cls.alphabetic + ['-', 'www']
for code in aliases:
if (code not in cls.languages_by_size
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index cb92de3..4a34ebb 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -23,7 +23,6 @@
import os
import pkgutil
import re
-
from collections import abc, defaultdict
from contextlib import suppress
from pathlib import Path
@@ -31,10 +30,8 @@
from typing import Optional, Union

import pywikibot
-
from pywikibot import __url__, config
from pywikibot.backports import (
- cache,
Dict,
Generator,
Iterable,
@@ -43,9 +40,11 @@
Mapping,
Match,
Sequence,
+ cache,
)
from pywikibot.plural import plural_rule

+
STR_OR_SITE_TYPE = Union[str, 'pywikibot.site.BaseSite']

PLURAL_PATTERN = r'{{PLURAL:(?:%\()?([^\)]*?)(?:\)d)?\|(.*?)}}'
@@ -382,7 +381,7 @@
if _messages_available is not None:
return _messages_available
try:
- mod = __import__(_messages_package_name, fromlist=[str('__path__')])
+ mod = __import__(_messages_package_name, fromlist=['__path__'])
except ImportError:
_messages_available = False
return False
@@ -799,7 +798,7 @@
"""
# obtain the directory containing all the json files for this package
package = twtitle.split('-')[0]
- mod = __import__(_messages_package_name, fromlist=[str('__file__')])
+ mod = __import__(_messages_package_name, fromlist=['__file__'])
pathname = os.path.join(next(iter(mod.__path__)), package)

# build a list of languages in that directory
diff --git a/pywikibot/interwiki_graph.py b/pywikibot/interwiki_graph.py
index 15d416e..2149693 100644
--- a/pywikibot/interwiki_graph.py
+++ b/pywikibot/interwiki_graph.py
@@ -13,6 +13,7 @@
from pywikibot import config
from pywikibot.backports import Dict, List, Set

+
try:
import pydot
PYDOT_ERROR = None
diff --git a/pywikibot/logentries.py b/pywikibot/logentries.py
index 3df2838..11c94cd 100644
--- a/pywikibot/logentries.py
+++ b/pywikibot/logentries.py
@@ -4,9 +4,9 @@
#
# Distributed under the terms of the MIT license.
#
+import datetime
from collections import UserDict
from contextlib import suppress
-import datetime
from typing import Any, Optional, Type, Union

import pywikibot
diff --git a/pywikibot/logging.py b/pywikibot/logging.py
index ae4f433..63c17a9 100644
--- a/pywikibot/logging.py
+++ b/pywikibot/logging.py
@@ -27,11 +27,10 @@
import logging
import os
import sys
-from typing import Any

# logging levels
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING
-from typing import Optional
+from typing import Any, Optional

from pywikibot.backports import Callable, List

diff --git a/pywikibot/login.py b/pywikibot/login.py
index c52568f..e383206 100644
--- a/pywikibot/login.py
+++ b/pywikibot/login.py
@@ -17,10 +17,7 @@
from pywikibot.backports import Dict, Tuple
from pywikibot.comms import http
from pywikibot.exceptions import APIError, NoUsernameError
-from pywikibot.tools import (
- file_mode_checker,
- normalize_username,
-)
+from pywikibot.tools import file_mode_checker, normalize_username


try:
diff --git a/pywikibot/page/__init__.py b/pywikibot/page/__init__.py
index e86ed10..d4d3095 100644
--- a/pywikibot/page/__init__.py
+++ b/pywikibot/page/__init__.py
@@ -1264,10 +1264,7 @@
pywikibot.log('Cosmetic changes for {}-{} enabled.'
.format(family, self.site.lang))
# cc depends on page directly and via several other imports
- from pywikibot.cosmetic_changes import (
- CANCEL,
- CosmeticChangesToolkit,
- )
+ from pywikibot.cosmetic_changes import CANCEL, CosmeticChangesToolkit
cc_toolkit = CosmeticChangesToolkit(self, ignore=CANCEL.MATCH)
self.text = cc_toolkit.change(old)

@@ -2501,7 +2498,7 @@
with open(filename, 'wb') as f:
for chunk in req.iter_content(chunk_size):
f.write(chunk)
- except IOError as e:
+ except OSError as e:
raise e

sha1 = compute_file_hash(filename)
@@ -5094,7 +5091,7 @@
assert isinstance(self._items, tuple)
assert all(isinstance(item, (bytes, str)) for item in self._items)

- attrs = ('{0!r}'.format(getattr(self, attr)) for attr in self._items)
+ attrs = ('{!r}'.format(getattr(self, attr)) for attr in self._items)
return 'pywikibot.page.{}({})'.format(
self.__class__.__name__, ', '.join(attrs))

diff --git a/pywikibot/page/_decorators.py b/pywikibot/page/_decorators.py
index 2f287ea..ddd6dc8 100644
--- a/pywikibot/page/_decorators.py
+++ b/pywikibot/page/_decorators.py
@@ -1,4 +1,3 @@
-
"""Decorators for Page objects."""
#
# (C) Pywikibot team, 2017-2021
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index 072247e..128a4d2 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -45,8 +45,8 @@
Iterator,
List,
Pattern,
- Set,
Sequence,
+ Set,
Tuple,
Type,
)
diff --git a/pywikibot/proofreadpage.py b/pywikibot/proofreadpage.py
index 8fca578..b37b9a1 100644
--- a/pywikibot/proofreadpage.py
+++ b/pywikibot/proofreadpage.py
@@ -51,6 +51,7 @@
from pywikibot.exceptions import Error, OtherPageSaveError
from pywikibot.page import PageSourceType

+
try:
from bs4 import BeautifulSoup
except ImportError as e:
diff --git a/pywikibot/scripts/generate_family_file.py b/pywikibot/scripts/generate_family_file.py
index 98a681f..ad004d8 100755
--- a/pywikibot/scripts/generate_family_file.py
+++ b/pywikibot/scripts/generate_family_file.py
@@ -35,7 +35,6 @@
import os
import string
import sys
-
from typing import Optional
from urllib.parse import urlparse

@@ -70,6 +69,7 @@
to keep it enabled.
"""
from pywikibot.scripts import _import_with_no_user_config
+
# from pywikibot.site_detect import MWSite and
# from pywikibot.config import base_dir
# when required but disable user-config checks
@@ -219,7 +219,7 @@
.format(fn)).lower() == 'n':
print('Terminating.')
sys.exit(1)
- except IOError: # file not found
+ except OSError: # file not found
pass

code_hostname_pairs = '\n '.join(
diff --git a/pywikibot/scripts/generate_user_files.py b/pywikibot/scripts/generate_user_files.py
index 16dcd10..b918aef 100755
--- a/pywikibot/scripts/generate_user_files.py
+++ b/pywikibot/scripts/generate_user_files.py
@@ -13,7 +13,6 @@
import os
import re
import sys
-
from collections import namedtuple
from pathlib import Path
from textwrap import fill
@@ -21,6 +20,7 @@

from pywikibot.scripts import _import_with_no_user_config

+
PYTHON_VERSION = sys.version_info[:2]


@@ -368,8 +368,8 @@
with codecs.open(_fncpass, 'w', 'utf-8') as f:
f.write(PASSFILE_CONFIG.format(botpasswords=botpasswords))
file_mode_checker(_fncpass, mode=0o600)
- pywikibot.output("'{0}' written.".format(_fncpass))
- except EnvironmentError:
+ pywikibot.output("'{}' written.".format(_fncpass))
+ except OSError:
os.remove(_fncpass)
raise

diff --git a/pywikibot/scripts/version.py b/pywikibot/scripts/version.py
index 58fbbec..69759cb 100755
--- a/pywikibot/scripts/version.py
+++ b/pywikibot/scripts/version.py
@@ -74,7 +74,7 @@
if WMF_CACERT in text:
has_wikimedia_cert = True
pywikibot.output(' certificate test: {}'
- .format(('ok' if has_wikimedia_cert else 'not ok')))
+ .format('ok' if has_wikimedia_cert else 'not ok'))
if not has_wikimedia_cert:
pywikibot.output(' Please reinstall requests!')

diff --git a/pywikibot/site/__init__.py b/pywikibot/site/__init__.py
index 55ddc66..04f3c49 100644
--- a/pywikibot/site/__init__.py
+++ b/pywikibot/site/__init__.py
@@ -7,8 +7,8 @@
from pywikibot.site._apisite import APISite
from pywikibot.site._basesite import BaseSite
from pywikibot.site._datasite import DataSite
-from pywikibot.site._namespace import Namespace, NamespacesDict
from pywikibot.site._namespace import NamespaceArgType # noqa: F401
+from pywikibot.site._namespace import Namespace, NamespacesDict
from pywikibot.site._obsoletesites import ClosedSite, RemovedSite
from pywikibot.site._siteinfo import Siteinfo
from pywikibot.site._tokenwallet import TokenWallet
diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index e69f8b6..3589ad2 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -2873,11 +2873,11 @@
if 'offset' in data:
new_offset = int(data['offset'])
if offset + len(chunk) != new_offset:
- pywikibot.log('Old offset: {0}; Returned '
- 'offset: {1}; Chunk size: '
- '{2}'.format(offset,
- new_offset,
- len(chunk)))
+ pywikibot.log('Old offset: {}; Returned '
+ 'offset: {}; Chunk size: '
+ '{}'.format(offset,
+ new_offset,
+ len(chunk)))
pywikibot.warning('Unexpected offset.')
offset = new_offset
else:
diff --git a/pywikibot/site/_datasite.py b/pywikibot/site/_datasite.py
index 1b4e827..56756cf 100644
--- a/pywikibot/site/_datasite.py
+++ b/pywikibot/site/_datasite.py
@@ -70,7 +70,7 @@
if entity_type in self._entity_namespaces:
return self._entity_namespaces[entity_type]
raise EntityTypeUnknownError(
- '{0!r} does not support entity type "{1}"'
+ '{!r} does not support entity type "{}"'
.format(self, entity_type))

@property
diff --git a/pywikibot/site/_tokenwallet.py b/pywikibot/site/_tokenwallet.py
index dd9f292..53b4d06 100644
--- a/pywikibot/site/_tokenwallet.py
+++ b/pywikibot/site/_tokenwallet.py
@@ -7,6 +7,7 @@
from pywikibot import debug
from pywikibot.exceptions import Error

+
_logger = 'site.tokenwallet'


diff --git a/pywikibot/site_detect.py b/pywikibot/site_detect.py
index 0181f65..60fbe42 100644
--- a/pywikibot/site_detect.py
+++ b/pywikibot/site_detect.py
@@ -6,7 +6,6 @@
#
import json
import re
-
from contextlib import suppress
from html.parser import HTMLParser
from http import HTTPStatus
diff --git a/pywikibot/specialbots/_unlink.py b/pywikibot/specialbots/_unlink.py
index ad79349..cf61dd0 100644
--- a/pywikibot/specialbots/_unlink.py
+++ b/pywikibot/specialbots/_unlink.py
@@ -15,8 +15,8 @@
InteractiveReplace,
NoRedirectPageBot,
)
-from pywikibot.editor import TextEditor
from pywikibot.bot_choice import UnhandledAnswer
+from pywikibot.editor import TextEditor
from pywikibot.textlib import replace_links


diff --git a/pywikibot/specialbots/_upload.py b/pywikibot/specialbots/_upload.py
index fe93944..bf6350a 100644
--- a/pywikibot/specialbots/_upload.py
+++ b/pywikibot/specialbots/_upload.py
@@ -19,7 +19,6 @@

import pywikibot
import pywikibot.comms.http as http
-
from pywikibot import config
from pywikibot.backports import List
from pywikibot.bot import BaseBot, QuitKeyboardInterrupt
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 48ecf41..3fa934e 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -19,9 +19,10 @@
from typing import NamedTuple, Optional, Union

import pywikibot
-from pywikibot.backports import Container, Iterable, List, Tuple
+from pywikibot.backports import Container, Iterable, List
from pywikibot.backports import OrderedDict as OrderedDictType
from pywikibot.backports import Sequence as SequenceType
+from pywikibot.backports import Tuple
from pywikibot.exceptions import InvalidTitleError, SiteDefinitionError
from pywikibot.family import Family
from pywikibot.tools import deprecated
@@ -1769,7 +1770,7 @@
for items in params.items():
text += '|{}={}\n'.format(*items)

- return '{{%s\n%s}}' % (template, text)
+ return '{{{{{}\n{}}}}}'.format(template, text)


# --------------------------
diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index e9566c8..55dce81 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -8,16 +8,14 @@
import math
import threading
import time
-
-from collections import namedtuple, Counter
+from collections import Counter, namedtuple
from contextlib import suppress
from typing import Optional, Union

import pywikibot
-
from pywikibot import config
+from pywikibot.tools import PYTHON_VERSION, deprecated

-from pywikibot.tools import deprecated, PYTHON_VERSION

if PYTHON_VERSION < (3, 6):
from hashlib import md5
@@ -111,9 +109,9 @@
def _read_file(self, raise_exc=False):
"""Yield process entries from file."""
try:
- with open(self.ctrlfilename, 'r') as f:
+ with open(self.ctrlfilename) as f:
lines = f.readlines()
- except IOError:
+ except OSError:
if raise_exc and pid:
raise
return
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index c201689..9e19104 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -17,7 +17,6 @@
import sys
import threading
import time
-
from collections.abc import Container, Iterable, Iterator, Mapping, Sized
from contextlib import suppress
from functools import total_ordering, wraps
@@ -30,21 +29,21 @@

from pywikibot.logging import debug
from pywikibot.tools._deprecate import ( # noqa: F401
+ ModuleDeprecationWrapper,
add_decorated_full_name,
add_full_name,
- deprecated,
deprecate_arg,
+ deprecated,
deprecated_args,
get_wrapper_depth,
issue_deprecation_warning,
manage_wrapping,
- ModuleDeprecationWrapper,
redirect_func,
remove_last_args,
)
-
from pywikibot.tools._unidata import _first_upper_exception

+
pkg_Version = pkg_resources.packaging.version.Version # noqa: N816

try:
diff --git a/pywikibot/tools/chars.py b/pywikibot/tools/chars.py
index a2fd898..8cc36dd 100644
--- a/pywikibot/tools/chars.py
+++ b/pywikibot/tools/chars.py
@@ -6,7 +6,6 @@
#
import re
import sys
-
from contextlib import suppress
from typing import Union
from urllib.parse import unquote_to_bytes
@@ -38,7 +37,7 @@
codepoint = (ord(match[0]) & mask) << 10 | (ord(match[1]) & mask)
else:
codepoint = ord(match)
- return '<{0:x}>'.format(codepoint)
+ return '<{:x}>'.format(codepoint)

return INVISIBLE_REGEX.sub(replace, text)

diff --git a/pywikibot/tools/djvu.py b/pywikibot/tools/djvu.py
index 83c2873..012478f 100644
--- a/pywikibot/tools/djvu.py
+++ b/pywikibot/tools/djvu.py
@@ -78,13 +78,13 @@

def __repr__(self) -> str:
"""Return a more complete string representation."""
- return str("{}.{}('{}')").format(self.__module__,
- self.__class__.__name__,
- self._filename)
+ return "{}.{}('{}')".format(self.__module__,
+ self.__class__.__name__,
+ self._filename)

def __str__(self) -> str:
"""Return a string representation."""
- return str("{}('{}')").format(self.__class__.__name__, self._filename)
+ return "{}('{}')".format(self.__class__.__name__, self._filename)

def check_cache(fn):
"""Decorator to check if cache shall be cleared."""
diff --git a/pywikibot/tools/formatter.py b/pywikibot/tools/formatter.py
index 26bfff2..a3375b3 100644
--- a/pywikibot/tools/formatter.py
+++ b/pywikibot/tools/formatter.py
@@ -6,7 +6,6 @@
#
import math
import platform
-
from string import Formatter
from typing import Any, Mapping, Sequence

diff --git a/pywikibot/userinterfaces/_interface_base.py b/pywikibot/userinterfaces/_interface_base.py
index 422496e..0a966cd 100644
--- a/pywikibot/userinterfaces/_interface_base.py
+++ b/pywikibot/userinterfaces/_interface_base.py
@@ -8,7 +8,6 @@
# Distributed under the terms of the MIT license.
#
import sys
-
from abc import ABC, abstractmethod
from typing import Any, Union

diff --git a/pywikibot/userinterfaces/buffer_interface.py b/pywikibot/userinterfaces/buffer_interface.py
index d4b683e..7a58c2a 100755
--- a/pywikibot/userinterfaces/buffer_interface.py
+++ b/pywikibot/userinterfaces/buffer_interface.py
@@ -12,6 +12,7 @@
from pywikibot.logging import INFO, VERBOSE
from pywikibot.userinterfaces._interface_base import ABUIC

+
BAD_BUFFER_TYPE = 'BUG: bufffer can only contain logs and strings, had {}'


diff --git a/pywikibot/userinterfaces/terminal_interface_base.py b/pywikibot/userinterfaces/terminal_interface_base.py
index c288bdc..eb1808f 100755
--- a/pywikibot/userinterfaces/terminal_interface_base.py
+++ b/pywikibot/userinterfaces/terminal_interface_base.py
@@ -50,7 +50,8 @@
]

_color_pat = '{}|previous'.format('|'.join(colors))
-colorTagR = re.compile('\03{((:?%s);?(:?%s)?)}' % (_color_pat, _color_pat))
+colorTagR = re.compile('\03{{((:?{});?(:?{})?)}}'.format(_color_pat,
+ _color_pat))


class UI(ABUIC):
diff --git a/pywikibot/userinterfaces/win32_unicode.py b/pywikibot/userinterfaces/win32_unicode.py
index 668abcd..cbc4864 100755
--- a/pywikibot/userinterfaces/win32_unicode.py
+++ b/pywikibot/userinterfaces/win32_unicode.py
@@ -22,7 +22,6 @@
#
################################################
import sys
-
from contextlib import suppress
from ctypes import Structure, byref
from ctypes import c_void_p as LPVOID
@@ -136,7 +135,7 @@
if 0 in (retval, n.value):
msg = 'WriteConsoleW returned {!r}, n.value = {!r}' \
.format(retval, n.value)
- raise IOError(msg)
+ raise OSError(msg)
remaining -= n.value
if remaining == 0:
break
diff --git a/pywikibot/version.py b/pywikibot/version.py
index d759c18..5e606dd 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -245,7 +245,7 @@
# some Windows git versions provide git.cmd instead of git.exe
cmd = 'git.cmd'

- with open(os.path.join(_program_dir, '.git/config'), 'r') as f:
+ with open(os.path.join(_program_dir, '.git/config')) as f:
tag = f.read()
# Try 'origin' and then 'gerrit' as remote name; bail if can't find either.
remote_pos = tag.find('[remote "origin"]')
diff --git a/scripts/add_text.py b/scripts/add_text.py
index dfe01c4..903c6a5 100755
--- a/scripts/add_text.py
+++ b/scripts/add_text.py
@@ -63,11 +63,9 @@
#
import codecs
import re
-
from typing import Union

import pywikibot
-
from pywikibot import config, pagegenerators, textlib
from pywikibot.backports import Dict, Sequence
from pywikibot.bot import (
@@ -76,6 +74,7 @@
NoRedirectPageBot,
)

+
ARGS_TYPE = Dict[str, Union[bool, str]]
DEFAULT_ARGS = {
'text': '',
diff --git a/scripts/archive/casechecker.py b/scripts/archive/casechecker.py
index 19e1005..c1cd68d 100755
--- a/scripts/archive/casechecker.py
+++ b/scripts/archive/casechecker.py
@@ -751,7 +751,7 @@
"""Open logfile."""
try:
return codecs.open(filename, 'a', 'utf-8')
- except IOError:
+ except OSError:
return codecs.open(filename, 'w', 'utf-8')

def AppendLineToLog(self, filename, text):
diff --git a/scripts/archive/makecat.py b/scripts/archive/makecat.py
index 2d9682d..aed65ec 100755
--- a/scripts/archive/makecat.py
+++ b/scripts/archive/makecat.py
@@ -327,7 +327,7 @@
checked.add(pl)

excludefile = codecs.open(filename, 'a', encoding=mysite.encoding())
- except IOError:
+ except OSError:
# File does not exist
excludefile = codecs.open(filename, 'w', encoding=mysite.encoding())

diff --git a/scripts/archive/match_images.py b/scripts/archive/match_images.py
index 6104271..9b252e0 100755
--- a/scripts/archive/match_images.py
+++ b/scripts/archive/match_images.py
@@ -84,14 +84,14 @@
averageScore = (wholeScore + topleftScore + toprightScore
+ bottomleftScore + bottomrightScore + centerScore) / 6

- pywikibot.output('Whole image {0:>7.2%}\n'
- 'Top left of image {1:>7.2%}\n'
- 'Top right of image {2:>7.2%}\n'
- 'Bottom left of image {3:>7.2%}\n'
- 'Bottom right of image {4:>7.2%}\n'
- 'Center of image {5:>7.2%}\n'
+ pywikibot.output('Whole image {:>7.2%}\n'
+ 'Top left of image {:>7.2%}\n'
+ 'Top right of image {:>7.2%}\n'
+ 'Bottom left of image {:>7.2%}\n'
+ 'Bottom right of image {:>7.2%}\n'
+ 'Center of image {:>7.2%}\n'
' -------\n'
- 'Average {6:>7.2%}'.format(
+ 'Average {:>7.2%}'.format(
wholeScore, topleftScore, toprightScore,
bottomleftScore, bottomrightScore, centerScore,
averageScore))
diff --git a/scripts/archive/piper.py b/scripts/archive/piper.py
index ef1d7b1..5918448 100755
--- a/scripts/archive/piper.py
+++ b/scripts/archive/piper.py
@@ -93,7 +93,7 @@
file.write(text)

# Now retrieve the munged text
- with open(temp_filename, 'r') as file:
+ with open(temp_filename) as file:
text = file.read()

# clean up
diff --git a/scripts/archivebot.py b/scripts/archivebot.py
index 4eb7094..411e430 100755
--- a/scripts/archivebot.py
+++ b/scripts/archivebot.py
@@ -111,10 +111,10 @@
from pywikibot.date import apply_month_delta
from pywikibot.exceptions import Error, NoPageError
from pywikibot.textlib import (
+ TimeStripper,
case_escape,
extract_sections,
findmarker,
- TimeStripper,
to_local_digits,
)

@@ -300,7 +300,7 @@
title = tpl_page.title(with_ns=False)
title = case_escape(ns.case, title)

- return re.compile(r'(?:(?:%s):)%s%s' % ('|'.join(ns), marker, title))
+ return re.compile(r'(?:(?:{}):){}{}'.format('|'.join(ns), marker, title))


def calc_md5_hexdigest(txt, salt) -> str:
@@ -591,8 +591,8 @@

def load_config(self) -> None:
"""Load and validate archiver template."""
- pywikibot.output('Looking for: {{%s}} in %s' % (self.tpl.title(),
- self.page))
+ pywikibot.output('Looking for: {{{{{}}}}} in {}'.format(
+ self.tpl.title(), self.page))
for tpl, params in self.page.raw_extracted_templates:
try: # Check tpl name before comparing; it might be invalid.
tpl_page = pywikibot.Page(self.site, tpl, ns=10)
@@ -893,7 +893,7 @@
follow_redirects=False,
namespaces=ns))
if filename:
- for pg in open(filename, 'r').readlines():
+ for pg in open(filename).readlines():
pagelist.append(pywikibot.Page(site, pg, ns=10))
if pagename:
pagelist.append(pywikibot.Page(site, pagename, ns=3))
diff --git a/scripts/blockpageschecker.py b/scripts/blockpageschecker.py
index 45c7ba8..e72f9b1 100755
--- a/scripts/blockpageschecker.py
+++ b/scripts/blockpageschecker.py
@@ -50,13 +50,11 @@
#
import re
import webbrowser
-
from collections import namedtuple
from itertools import chain
from typing import Optional

import pywikibot
-
from pywikibot import i18n, pagegenerators
from pywikibot.bot import ConfigParserBot, ExistingPageBot, SingleSiteBot
from pywikibot.editor import TextEditor
diff --git a/scripts/category.py b/scripts/category.py
index e5addb1..cae9495 100755
--- a/scripts/category.py
+++ b/scripts/category.py
@@ -129,7 +129,6 @@
import os
import pickle
import re
-
from contextlib import suppress
from operator import methodcaller
from textwrap import fill
diff --git a/scripts/category_redirect.py b/scripts/category_redirect.py
index b632085..4089cb3 100755
--- a/scripts/category_redirect.py
+++ b/scripts/category_redirect.py
@@ -293,7 +293,7 @@
try:
with open(self.datafile, 'rb') as inp:
self.record = pickle.load(inp)
- except IOError:
+ except OSError:
self.record = {}
if self.record:
with open(self.datafile + '.bak', 'wb') as f:
@@ -303,15 +303,15 @@
# note that any templates containing optional "category:" are
# incorrect and will be fixed by the bot
template_regex = re.compile(
- r"""{{\s*(?:%(prefix)s\s*:\s*)? # optional "template:"
- (?:%(template)s)\s*\| # catredir template name
- (\s*%(catns)s\s*:\s*)? # optional "category:"
- ([^|}]+) # redirect target cat
- (?:\|[^|}]*)*}} # optional arguments 2+, ignored
- """ % {'prefix': self.site.namespace(10).lower(),
- 'template': '|'.join(item.replace(' ', '[ _]+')
- for item in self.template_list),
- 'catns': self.site.namespace(14)},
+ r"""{{{{\s*(?:{prefix}\s*:\s*)? # optional "template:"
+ (?:{template})\s*\| # catredir template name
+ (\s*{catns}\s*:\s*)? # optional "category:"
+ ([^|}}]+) # redirect target cat
+ (?:\|[^|}}]*)*}}}} # optional arguments 2+, ignored
+ """.format(prefix=self.site.namespace(10).lower(),
+ template='|'.join(item.replace(' ', '[ _]+')
+ for item in self.template_list),
+ catns=self.site.namespace(14)),
re.I | re.X)

self.check_hard_redirect()
diff --git a/scripts/clean_sandbox.py b/scripts/clean_sandbox.py
index b9098d1..d9fec7f 100755
--- a/scripts/clean_sandbox.py
+++ b/scripts/clean_sandbox.py
@@ -233,7 +233,7 @@
'sandbox cleaned.')
else: # wait for the rest
pywikibot.output(
- 'Sandbox edited {0:.1f} minutes ago...'
+ 'Sandbox edited {:.1f} minutes ago...'
.format(edit_delta.seconds / 60.0))
pywikibot.output('Sleeping for {} minutes.'
.format(delta.seconds // 60))
diff --git a/scripts/commonscat.py b/scripts/commonscat.py
index f935393..8149224 100755
--- a/scripts/commonscat.py
+++ b/scripts/commonscat.py
@@ -45,7 +45,6 @@
import re

import pywikibot
-
from pywikibot import i18n, pagegenerators
from pywikibot.bot import ConfigParserBot, ExistingPageBot, NoRedirectPageBot
from pywikibot.exceptions import InvalidTitleError
@@ -325,8 +324,8 @@
if commonscatLink == page.title():
text_to_add = '{{%s}}' % primaryCommonscat
else:
- text_to_add = '{{%s|%s}}' % (primaryCommonscat,
- commonscatLink)
+ text_to_add = '{{{{{}|{}}}}}'.format(primaryCommonscat,
+ commonscatLink)
summary = self.opt.summary or i18n.twtranslate(
page.site, 'add_text-adding', {'adding': text_to_add})
self.put_current(add_text(page.text, text_to_add),
@@ -346,7 +345,8 @@
if linktitle and newcat != page.title(with_ns=False):
newtext = re.sub(r'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
% oldtemplate,
- '{{%s|%s|%s}}' % (newtemplate, newcat, linktitle),
+ '{{{{{}|{}|{}}}}}'.format(newtemplate, newcat,
+ linktitle),
page.get())
elif newcat == page.title(with_ns=False):
newtext = re.sub(r'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
@@ -356,7 +356,7 @@
elif oldcat.strip() != newcat: # strip trailing white space
newtext = re.sub(r'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
% oldtemplate,
- '{{%s|%s}}' % (newtemplate, newcat),
+ '{{{{{}|{}}}}}'.format(newtemplate, newcat),
page.get())
else: # nothing left to do
return
diff --git a/scripts/coordinate_import.py b/scripts/coordinate_import.py
index 364e86a..26230fe 100755
--- a/scripts/coordinate_import.py
+++ b/scripts/coordinate_import.py
@@ -50,7 +50,6 @@
from typing import Optional

import pywikibot
-
from pywikibot import pagegenerators
from pywikibot.bot import ConfigParserBot, WikidataBot
from pywikibot.exceptions import CoordinateGlobeUnknownError
diff --git a/scripts/data_ingestion.py b/scripts/data_ingestion.py
index 920ef0a..084e252 100755
--- a/scripts/data_ingestion.py
+++ b/scripts/data_ingestion.py
@@ -104,7 +104,6 @@
import io
import os
import posixpath
-
from typing import Any, BinaryIO, Optional
from urllib.parse import urlparse

diff --git a/scripts/delete.py b/scripts/delete.py
index ead90aa..9e278e9 100755
--- a/scripts/delete.py
+++ b/scripts/delete.py
@@ -175,7 +175,7 @@
ns_name, ns_id, n_pages_in_ns, width=width, pl=plural))
if show_n_pages: # do not show marker if 0 pages are requested.
for page in islice_with_ellipsis(refs[ns], show_n_pages):
- pywikibot.output(' {0!s}'.format(page.title()))
+ pywikibot.output(' {!s}'.format(page.title()))

def skip_page(self, page) -> bool:
"""Skip the page under some conditions."""
diff --git a/scripts/fixing_redirects.py b/scripts/fixing_redirects.py
index 857dd54..d489d69 100755
--- a/scripts/fixing_redirects.py
+++ b/scripts/fixing_redirects.py
@@ -20,7 +20,7 @@
# Distributed under the terms of the MIT license.
#
import re
-from concurrent.futures import as_completed, ThreadPoolExecutor
+from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import suppress

import pywikibot
diff --git a/scripts/image.py b/scripts/image.py
index 4eff8f7..a40300d 100755
--- a/scripts/image.py
+++ b/scripts/image.py
@@ -47,7 +47,6 @@
from pywikibot import i18n, pagegenerators
from pywikibot.bot import SingleSiteBot
from pywikibot.textlib import case_escape
-
from scripts.replace import ReplaceRobot as ReplaceBot


diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index 0f0a1c7..7696438 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -335,7 +335,6 @@
import codecs
import os
import re
-import socket
import sys
from collections import Counter, defaultdict
from contextlib import suppress
@@ -1756,7 +1755,7 @@
pywikibot.output('ERROR putting page: {}'
.format(error.args,))
raise SaveError('PageSaveRelatedError')
- except (socket.error, IOError) as error:
+ except OSError as error:
if timeout > 3600:
raise
pywikibot.output('ERROR putting page: {}'
@@ -1834,7 +1833,7 @@
.format(page.site.family.name,
page, linkedPage))

- except (socket.error, IOError):
+ except OSError:
pywikibot.output('ERROR: could not report backlinks')


diff --git a/scripts/maintenance/cache.py b/scripts/maintenance/cache.py
index 91b1ad3..7c8df52 100755
--- a/scripts/maintenance/cache.py
+++ b/scripts/maintenance/cache.py
@@ -229,7 +229,7 @@
"""
if not cache_path:
cache_path = os.path.join(pywikibot.config.base_dir,
- 'apicache-py{0:d}'.format(PYTHON_VERSION[0]))
+ 'apicache-py{:d}'.format(PYTHON_VERSION[0]))

if not os.path.exists(cache_path):
pywikibot.error('{}: no such file or directory'.format(cache_path))
diff --git a/scripts/nowcommons.py b/scripts/nowcommons.py
index c89fb25..bbb9038 100755
--- a/scripts/nowcommons.py
+++ b/scripts/nowcommons.py
@@ -52,8 +52,8 @@

import pywikibot
from pywikibot import i18n
-from pywikibot.bot import Bot, ConfigParserBot
from pywikibot import pagegenerators as pg
+from pywikibot.bot import Bot, ConfigParserBot
from pywikibot.exceptions import IsRedirectPageError, NoPageError
from pywikibot.tools import filter_unique
from pywikibot.tools.formatter import color_format
diff --git a/scripts/pagefromfile.py b/scripts/pagefromfile.py
index f8c2145..f5dff52 100755
--- a/scripts/pagefromfile.py
+++ b/scripts/pagefromfile.py
@@ -75,6 +75,7 @@
from pywikibot.bot import CurrentPageBot, OptionHandler, SingleSiteBot
from pywikibot.pagegenerators import PreloadingGenerator

+
CTX_ATTR = '_content_ctx'


@@ -208,7 +209,7 @@
encoding=config.textfile_encoding) as f:
text = f.read()

- except IOError:
+ except OSError:
pywikibot.exception()
return

diff --git a/scripts/redirect.py b/scripts/redirect.py
index a490bca..9f71e8a 100755
--- a/scripts/redirect.py
+++ b/scripts/redirect.py
@@ -79,7 +79,11 @@
from pywikibot import i18n, pagegenerators, xmlreader
from pywikibot.backports import Dict, List, Set, Tuple
from pywikibot.bot import (
- ExistingPageBot, OptionHandler, RedirectPageBot, suggest_help)
+ ExistingPageBot,
+ OptionHandler,
+ RedirectPageBot,
+ suggest_help,
+)
from pywikibot.exceptions import (
CircularRedirectError,
InterwikiRedirectPageError,
diff --git a/scripts/reflinks.py b/scripts/reflinks.py
index 9cf0a51..5b4183e 100755
--- a/scripts/reflinks.py
+++ b/scripts/reflinks.py
@@ -51,10 +51,8 @@
import itertools
import os
import re
-import socket
import subprocess
import tempfile
-
from contextlib import suppress
from enum import IntEnum
from functools import partial
@@ -79,9 +77,8 @@
XMLDumpPageGenerator as _XMLDumpPageGenerator,
)
from pywikibot.textlib import replaceExcept
-from pywikibot.tools.formatter import color_format
from pywikibot.tools.chars import string2html
-
+from pywikibot.tools.formatter import color_format
from scripts import noreferences


@@ -540,7 +537,7 @@
try:
with codecs.open(listof404pages, 'r', 'latin_1') as f:
self.dead_links = f.read()
- except IOError:
+ except OSError:
raise NotImplementedError(
'404-links.txt is required for reflinks.py\n'
'You need to download\n'
@@ -641,8 +638,7 @@
continue

except (ValueError, # urllib3.LocationParseError derives from it
- socket.error,
- IOError,
+ OSError,
httplib.error,
FatalServerError,
Server414Error,
diff --git a/scripts/revertbot.py b/scripts/revertbot.py
index 0616401..a5435fa 100755
--- a/scripts/revertbot.py
+++ b/scripts/revertbot.py
@@ -43,7 +43,6 @@
from typing import Union

import pywikibot
-
from pywikibot import i18n
from pywikibot.bot import OptionHandler
from pywikibot.date import format_date, formatYear
diff --git a/scripts/weblinkchecker.py b/scripts/weblinkchecker.py
index 9130775..9f11b04 100755
--- a/scripts/weblinkchecker.py
+++ b/scripts/weblinkchecker.py
@@ -381,7 +381,7 @@
try:
with open(self.datfilename, 'rb') as datfile:
self.history_dict = pickle.load(datfile)
- except (IOError, EOFError):
+ except (OSError, EOFError):
# no saved history exists yet, or history dump broken
self.history_dict = {}

diff --git a/scripts/welcome.py b/scripts/welcome.py
index 347b67f..7b9b7d3 100755
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -637,7 +637,8 @@
rep_text += i18n.translate(self.site,
report_text) % username
if self.site.code == 'it':
- rep_text = '%s%s}}' % (rep_text, self.bname[username])
+ rep_text = '{}{}}}}}'.format(rep_text,
+ self.bname[username])

com = i18n.twtranslate(self.site, 'welcome-bad_username')
if rep_text != '':
@@ -759,7 +760,7 @@
except LookupError:
f = codecs.open(pywikibot.config.datafilepath(
globalvar.sign_file_name), 'r', encoding='utf-8')
- except IOError:
+ except OSError:
pywikibot.error('No fileName!')
raise FilenameNotSet('No signature filename specified.')

diff --git a/setup.py b/setup.py
index 4606e00..45bf661 100644
--- a/setup.py
+++ b/setup.py
@@ -36,6 +36,7 @@
import os
import sys

+
VERSIONS_REQUIRED_MESSAGE = """
Pywikibot is not available on:
{version}
@@ -166,9 +167,10 @@
return version

# validate version for sdist
- from pkg_resources import parse_version, safe_version
from contextlib import suppress
from subprocess import PIPE, run
+
+ from pkg_resources import parse_version, safe_version
try:
tags = run(['git', 'tag'], check=True, stdout=PIPE,
universal_newlines=True).stdout.splitlines()
diff --git a/tests/add_text_tests.py b/tests/add_text_tests.py
index a323f82..adbfe21 100644
--- a/tests/add_text_tests.py
+++ b/tests/add_text_tests.py
@@ -10,9 +10,7 @@

import pywikibot
import pywikibot.pagegenerators
-
from scripts.add_text import AddTextBot, main, parse
-
from tests.aspects import TestCase


diff --git a/tests/archive/isbn_tests.py b/tests/archive/isbn_tests.py
index 5cd1e77..92550c5 100644
--- a/tests/archive/isbn_tests.py
+++ b/tests/archive/isbn_tests.py
@@ -8,7 +8,6 @@
import pywikibot
from pywikibot import Bot, Claim, ItemPage
from pywikibot.tools import has_module
-
from scripts.isbn import InvalidIsbnException as IsbnExc
from scripts.isbn import convertIsbn10toIsbn13, hyphenateIsbnNumbers, main
from tests.aspects import (
@@ -17,7 +16,6 @@
WikibaseTestCase,
unittest,
)
-
from tests.bot_tests import TWNBotTestCase
from tests.utils import empty_sites

diff --git a/tests/aspects.py b/tests/aspects.py
index ac67b43..f9d67a4 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -84,7 +84,7 @@
self.test_completed = time.time()
duration = self.test_completed - self.test_start
if duration > self.test_duration_warning_interval:
- unittest_print(' {0:.3f}s'.format(duration), end=' ')
+ unittest_print(' {:.3f}s'.format(duration), end=' ')
sys.stdout.flush()


diff --git a/tests/bot_tests.py b/tests/bot_tests.py
index d950ab5..c06cee0 100644
--- a/tests/bot_tests.py
+++ b/tests/bot_tests.py
@@ -11,7 +11,6 @@
import pywikibot
import pywikibot.bot
from pywikibot import i18n
-
from tests.aspects import (
DefaultSiteTestCase,
SiteAttributeTestCase,
diff --git a/tests/cosmetic_changes_tests.py b/tests/cosmetic_changes_tests.py
index 6c90fe4..de22979 100644
--- a/tests/cosmetic_changes_tests.py
+++ b/tests/cosmetic_changes_tests.py
@@ -10,10 +10,8 @@

from pywikibot import Page
from pywikibot.cosmetic_changes import CANCEL, CosmeticChangesToolkit
-from pywikibot.tools import has_module
-
from pywikibot.site._namespace import NamespacesDict
-
+from pywikibot.tools import has_module
from tests.aspects import TestCase


@@ -585,6 +583,7 @@
def test_invalid_isbn(self):
"""Test that it'll fail when the ISBN is invalid."""
from stdnum.exceptions import ValidationError
+
# Invalid characters
with self.assertRaisesRegex(
ValidationError,
diff --git a/tests/data_ingestion_tests.py b/tests/data_ingestion_tests.py
index ee19246..8dd315f 100644
--- a/tests/data_ingestion_tests.py
+++ b/tests/data_ingestion_tests.py
@@ -8,7 +8,6 @@
import unittest

from scripts import data_ingestion
-
from tests import join_data_path, join_images_path
from tests.aspects import ScriptMainTestCase, TestCase
from tests.utils import empty_sites
@@ -64,14 +63,14 @@
def test_get_description(self):
"""Test getDescription()."""
self.assertEqual(self.obj.get_description('CrystalTemplate'),
- str("""{{CrystalTemplate
+ """{{CrystalTemplate
|author=KDE artists {{!}} Silstor
|description.en="Sounds" icon
|license=LGPL
|name=Sound icon
|set=Crystal SVG icon set
|source=http://commons.wikimedia.org/wiki/File:Sound-icon.svg
-}}"""))
+}}""")


class TestCSVReader(TestCase):
@@ -103,7 +102,7 @@
def test_get_description(self):
"""Test getDescription()."""
self.assertEqual(self.obj.get_description('CrystalTemplate'),
- str("""{{CrystalTemplate
+ """{{CrystalTemplate
|author=KDE artists {{!}} Silstor
|description.en="Sounds" icon
|license=LGPL
@@ -111,7 +110,7 @@
|set=Crystal SVG icon set
|source=http://commons.wikimedia.org/wiki/File:Sound-icon.svg
|url=http://upload.wikimedia.org/wikipedia/commons/f/fc/MP_sounds.png
-}}"""))
+}}""")


class TestDataIngestionBot(ScriptMainTestCase):
diff --git a/tests/date_tests.py b/tests/date_tests.py
index 00fe072..a859b77 100644
--- a/tests/date_tests.py
+++ b/tests/date_tests.py
@@ -50,7 +50,7 @@
cls.add_method(dct, 'test_' + formatname, test_method(formatname),
doc_suffix='using {} format'.format(formatname))

- return super(TestDateMeta, cls).__new__(cls, name, bases, dct)
+ return super().__new__(cls, name, bases, dct)


class TestDate(TestCase, metaclass=TestDateMeta):
diff --git a/tests/family_tests.py b/tests/family_tests.py
index d048431..b212c20 100644
--- a/tests/family_tests.py
+++ b/tests/family_tests.py
@@ -113,7 +113,7 @@
def test_set_obsolete(self):
"""Test obsolete can be set."""
# Construct a temporary family and instantiate it
- family = type(str('TempFamily'), (Family,), {})()
+ family = type('TempFamily', (Family,), {})()

self.assertEqual(family.obsolete, {})
self.assertEqual(family.interwiki_replacements, {})
diff --git a/tests/generate_family_file_tests.py b/tests/generate_family_file_tests.py
index 079fe7f..58aa969 100644
--- a/tests/generate_family_file_tests.py
+++ b/tests/generate_family_file_tests.py
@@ -10,9 +10,8 @@
from random import sample
from urllib.parse import urlparse

-from pywikibot.scripts import generate_family_file
from pywikibot import Site
-
+from pywikibot.scripts import generate_family_file
from tests.aspects import DefaultSiteTestCase


diff --git a/tests/generate_user_files_tests.py b/tests/generate_user_files_tests.py
index 982eeab..eed07cc 100644
--- a/tests/generate_user_files_tests.py
+++ b/tests/generate_user_files_tests.py
@@ -9,7 +9,6 @@
import unittest

from pywikibot.scripts import generate_user_files as guf
-
from tests.aspects import TestCase


diff --git a/tests/http_tests.py b/tests/http_tests.py
index ba23dfb..384bd91 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -380,7 +380,7 @@
charset = None
resp = CharsetTestCase._create_response(
headers={'content-type': 'application/xml'},
- data='<?xml version="1.0" encoding="UTF-8"?>'.encode('utf-8'))
+ data=b'<?xml version="1.0" encoding="UTF-8"?>')
resp.encoding = http._decide_encoding(resp, charset)
self.assertEqual('UTF-8', resp.encoding)

@@ -389,8 +389,8 @@
charset = None
resp = CharsetTestCase._create_response(
headers={'content-type': 'application/xml'},
- data='<?xml version="1.0" encoding="UTF-8" '
- 'someparam="ignored"?>'.encode('utf-8'))
+ data=b'<?xml version="1.0" encoding="UTF-8" '
+ b'someparam="ignored"?>')
resp.encoding = http._decide_encoding(resp, charset)
self.assertEqual('UTF-8', resp.encoding)

@@ -399,7 +399,7 @@
charset = None
resp = CharsetTestCase._create_response(
headers={'content-type': 'application/xml'},
- data="<?xml version='1.0' encoding='latin1'?>".encode('latin1'))
+ data=b"<?xml version='1.0' encoding='latin1'?>")
resp.encoding = http._decide_encoding(resp, charset)
self.assertEqual('latin1', resp.encoding)

diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 616147f..9fa1e94 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -10,12 +10,7 @@
import pywikibot
from pywikibot import bot, config, i18n
from pywikibot.exceptions import TranslationError
-from tests.aspects import (
- DefaultSiteTestCase,
- PwbTestCase,
- TestCase,
- unittest,
-)
+from tests.aspects import DefaultSiteTestCase, PwbTestCase, TestCase, unittest


class Site:
diff --git a/tests/l10n_tests.py b/tests/l10n_tests.py
index e52e5ba..ef6e1d6 100644
--- a/tests/l10n_tests.py
+++ b/tests/l10n_tests.py
@@ -81,7 +81,7 @@
doc_suffix='{} and language {}'.format(
package, code))

- return super(TestValidTemplateMeta, cls).__new__(cls, name, bases, dct)
+ return super().__new__(cls, name, bases, dct)


class TestValidTemplate(TestCase, metaclass=TestValidTemplateMeta):
diff --git a/tests/link_tests.py b/tests/link_tests.py
index 10a43cc..cbb0cd0 100644
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -13,8 +13,8 @@
from pywikibot.exceptions import InvalidTitleError, SiteDefinitionError
from pywikibot.page import Link, Page, SiteLink
from pywikibot.site import Namespace
-from tests.aspects import AlteredDefaultSiteTestCase
from tests.aspects import (
+ AlteredDefaultSiteTestCase,
DefaultDrySiteTestCase,
TestCase,
WikimediaDefaultSiteTestCase,
diff --git a/tests/plural_tests.py b/tests/plural_tests.py
index 6ee7891..e397d86 100644
--- a/tests/plural_tests.py
+++ b/tests/plural_tests.py
@@ -48,7 +48,7 @@
cls.add_method(dct, 'test_{}'.format(lang.replace('-', '_')),
create_test(rule),
doc_suffix='for "{}"'.format(lang))
- return super(MetaPluralRulesTest, cls).__new__(cls, name, bases, dct)
+ return super().__new__(cls, name, bases, dct)


class TestPluralRules(TestCase, metaclass=MetaPluralRulesTest):
diff --git a/tests/script_tests.py b/tests/script_tests.py
index baa8aab..3747db5 100644
--- a/tests/script_tests.py
+++ b/tests/script_tests.py
@@ -11,7 +11,6 @@
from contextlib import suppress

from pywikibot.tools import has_module
-
from tests import join_root_path, unittest_print
from tests.aspects import DefaultSiteTestCase, MetaTestCaseClass, PwbTestCase
from tests.utils import execute_pwb
@@ -330,7 +329,7 @@
dct[test_name] = unittest.expectedFailure(dct[test_name])
dct[test_name].__test__ = False

- return super(TestScriptMeta, cls).__new__(cls, name, bases, dct)
+ return super().__new__(cls, name, bases, dct)


class TestScriptHelp(PwbTestCase, metaclass=TestScriptMeta):
diff --git a/tests/site_detect_tests.py b/tests/site_detect_tests.py
index 8015f66..0b3b67f 100644
--- a/tests/site_detect_tests.py
+++ b/tests/site_detect_tests.py
@@ -282,7 +282,7 @@
self.assertEqual(parsed_url.netloc, self.NETLOC)
self.assertIn(parsed_url.path, self._responses)

- return type(str('Response'),
+ return type('Response',
(object,),
{'status_code': HTTPStatus.OK.value,
'text': self._responses[parsed_url.path],
diff --git a/tests/site_login_logout_tests.py b/tests/site_login_logout_tests.py
index 751b243..829b8dc 100644
--- a/tests/site_login_logout_tests.py
+++ b/tests/site_login_logout_tests.py
@@ -16,7 +16,6 @@

import pywikibot
from pywikibot.exceptions import APIError
-
from tests.aspects import DefaultSiteTestCase, TestCase


diff --git a/tests/site_tests.py b/tests/site_tests.py
index d52528e..c9973ad 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -1073,7 +1073,7 @@
page = pywikibot.Page(mysite, mysite.siteinfo['mainpage'])
with skipping(
StopIteration,
- msg='No images on the main page of site {0!r}'.format(mysite)):
+ msg='No images on the main page of site {!r}'.format(mysite)):
imagepage = next(iter(page.imagelinks())) # 1st image of page

pywikibot.output('site_tests.TestImageUsage found {} on {}'
@@ -1242,7 +1242,7 @@
# 1st image on main page
imagepage = next(iter(mysite.allimages()))
except StopIteration:
- unittest_print('No images on site {0!r}'.format(mysite))
+ unittest_print('No images on site {!r}'.format(mysite))
imagepage = None
cls.imagepage = imagepage

diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index e56ff46..64a1666 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -735,9 +735,9 @@
self._count += 1
if link.section:
return pywikibot.Link(
- '{0}#{1}'
+ '{}#{}'
.format(self._count, link.section), link.site)
- return pywikibot.Link('{0}'.format(self._count), link.site)
+ return pywikibot.Link('{}'.format(self._count), link.site)

return None

diff --git a/tests/token_tests.py b/tests/token_tests.py
index e31d9ec..f9ffadb 100644
--- a/tests/token_tests.py
+++ b/tests/token_tests.py
@@ -8,8 +8,8 @@
from contextlib import suppress

from pywikibot.exceptions import APIError, Error
-from pywikibot.tools import MediaWikiVersion
from pywikibot.site import TokenWallet
+from pywikibot.tools import MediaWikiVersion
from tests.aspects import DefaultSiteTestCase, TestCase, TestCaseBase, unittest


diff --git a/tests/tools_tests.py b/tests/tools_tests.py
index c6fb4c0..30ec3b7 100644
--- a/tests/tools_tests.py
+++ b/tests/tools_tests.py
@@ -9,12 +9,11 @@
import subprocess
import tempfile
import unittest
-from unittest import mock
-
from collections import Counter, OrderedDict
from collections.abc import Mapping
from contextlib import suppress
from importlib import import_module
+from unittest import mock

from pywikibot import tools
from pywikibot.tools import (
diff --git a/tests/ui_tests.py b/tests/ui_tests.py
index e34fb3f..8e78ea3 100644
--- a/tests/ui_tests.py
+++ b/tests/ui_tests.py
@@ -10,7 +10,6 @@
import os
import sys
import unittest
-
from contextlib import suppress

import pywikibot
@@ -30,8 +29,7 @@
terminal_interface_unix,
terminal_interface_win32,
)
-from pywikibot.userinterfaces.transliteration import _trans, NON_LATIN_DIGITS
-
+from pywikibot.userinterfaces.transliteration import NON_LATIN_DIGITS, _trans
from tests.aspects import TestCase, TestCaseBase
from tests.utils import FakeModule

diff --git a/tests/utils.py b/tests/utils.py
index e925285..5731aae 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -20,7 +20,6 @@
from pywikibot.exceptions import APIError
from pywikibot.login import LoginStatus
from pywikibot.site import Namespace
-
from tests import _pwb_py


diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index edf2541..29cd12b 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -25,7 +25,6 @@
from pywikibot.page import ItemPage, Page, PropertyPage, WikibasePage
from pywikibot.site import Namespace, NamespacesDict
from pywikibot.tools import MediaWikiVersion, suppress_warnings
-
from tests import WARN_SITE_CODE, join_pages_path
from tests.aspects import TestCase, WikidataTestCase
from tests.basepage import (

To view, visit change 763388. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I54c41e0edc98c3de1d14e44d6380a45645e4538f
Gerrit-Change-Number: 763388
Gerrit-PatchSet: 1
Gerrit-Owner: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: JJMC89 <JJMC89.Wikimedia@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged