Pywikibot 2.7 and Pywikibot 3.4 has been dropped due to T239542 and T213287. Rewrite all scripts in scripts folder to support Python 3 code only.
Which scripts should be converted?
--
- All rPWBC scripts inside the scripts folder except scripts inside following subfolders:
-- archive: archived scripts only which are desupported
-- i18n: json translation files only
-- maintenance: already done with https://gerrit.wikimedia.org/r/c/pywikibot/core/+/610076 which can be used as an example
-- userscripts: empty folder for bot owners scripts
- the `generate_family_file.py` script
- the `generate_user_files.py` script
How to convert?
--
- remove all `__future__` imports
- remove all `future_builtins` imports
- remove all Python 2 related code. Most of them are something like:
```lang=python
if PY2:
# Python 2 related code
else:
# Python 3 code
```
(don't forget to remove the line `from pywikibot.tools import PY2` in such case)
```lang=python
try:
from pathlib import Path
# Python 3 code
except ImportError:
from pathlib2 import Path
# Python 2 code
```
- replace class `MyClass(object):` by `class MyClass:`. This looks like old style but is new style class.
- replace `super(MyClass, self).my_method()` by `super().my_method()`
- use contextlib.suppress context manager to suppress passed exceptions like
```lang=python
try:
1/0
except ZeroDivisionError:
pass
```
should become:
```lang=python
with suppress(ZeroDivisionError):
1/0
```
- replace `unicode` usage by `str`
- replace `tools.UnicodeType` by `str`
- replace `tools.StringType` by `(str, bytes)` but check whether it can be just `str`
- replace `tools.py2_encode_utf_8(func)` by `func`
- remove `tools.UnicodeMixin` from class inheritance and replace their `__unicode__` method by `__str__`
- replace `for item in iterator: yield item` by `yield from iterator`
- use iterator unpacking for multiple ignoring values, e.g. `x, _, _, _ = range(4)` should become` x, *_ = range(4)`
- add annotation type hints if appropriate
- use `re.fullmatch` instead of `re.match(r'...$')` or `re.search(r'^...$')`
- use `str.format_map` instead of `str.format(**kwargs)`
- consider `os.scandir` instead of `os.listdir`
- see also https://www.asmeurer.com/python3-presentation/slides.html
Other things to know?
--
- convert step by step, one single patchset for one script file. Or maybe a few scripts. This makes review easier.
- Refer https://www.mediawiki.org/wiki/Manual:Pywikibot/Development for further information
- follow guidelines https://www.mediawiki.org/wiki/Manual:Pywikibot/Development/Guidelines
- use https://codesearch.wmflabs.org/pywikibot/
- do not hesitate to ask for support
- have fun