Skip to content

Commit

Permalink
[doc] Use Python 3.7 as basepython for doc tests
Browse files Browse the repository at this point in the history
doc tests is running sphinx and rstcheck but rstcheck throws a warning:
/src/.tox/doc/lib/python3.6/site-packages/rstcheck.py:51:
FutureWarning: Python versions prior 3.7 are deprecated.
Please update your python

- Therefore use Python 3.7 for doc tests
  note: this is different from doctest test which runs Python doctest)
- but also determine whether sphinx is running in backports.py
  to include its changes.
- update documentation
- update config string
- update requirements

Change-Id: I47f103e9a2f34ce819fda41959f7bc69e801a2da
  • Loading branch information
xqt committed Nov 5, 2022
1 parent e706d32 commit 01d1499
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
1 change: 0 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ coverage:
enabled: yes

ignore:
- pywikibot/backports.py
- pywikibot/daemonize.py
- pywikibot/families/__init__.py
- pywikibot/scripts/preload_sites.py
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# requirements.txt is also needed

sphinx >= 5.2.3
rstcheck >=3.5.0
rstcheck >=6.1.0
53 changes: 39 additions & 14 deletions pywikibot/backports.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


PYTHON_VERSION = sys.version_info[:3]
SPHINX_RUNNING = 'sphinx' in sys.modules

# functools.cache
if PYTHON_VERSION >= (3, 9):
Expand All @@ -19,19 +20,24 @@


# context
if PYTHON_VERSION < (3, 7):
if PYTHON_VERSION < (3, 7) or SPHINX_RUNNING:

class nullcontext: # noqa: N801

"""Dummy context manager for Python 3.5/3.6 that does nothing."""
"""Context manager that does no additional processing.
.. seealso:: :python:`contextlib.nullcontext
<library/contextlib.html#contextlib.nullcontext>`,
backported from Python 3.7.
"""

def __init__(self, result: Any = None) -> None: # noqa: D107
self.result = result
def __init__(self, enter_result: Any = None) -> None: # noqa: D107
self.enter_result = enter_result

def __enter__(self) -> Any:
return self.result
return self.enter_result

def __exit__(self, *args: Any) -> None:
def __exit__(self, *excinfo: Any) -> None:
pass
else:
from contextlib import nullcontext # type: ignore[misc]
Expand Down Expand Up @@ -102,40 +108,59 @@ def __exit__(self, *args: Any) -> None:


# PEP 616 string methods
if PYTHON_VERSION >= (3, 9):
removeprefix = str.removeprefix # type: ignore[attr-defined]
removesuffix = str.removesuffix # type: ignore[attr-defined]
else:
if PYTHON_VERSION < (3, 9) or SPHINX_RUNNING:
def removeprefix(string: str, prefix: str) -> str: # skipcq: TYP-053
"""Remove prefix from a string or return a copy otherwise.
>>> removeprefix('TestHook', 'Test')
'Hook'
>>> removeprefix('BaseTestCase', 'Test')
'BaseTestCase'
.. seealso:: :python:`str.removeprefix
<library/stdtypes.html#str.removeprefix>`,
backported from Python 3.9.
.. versionadded:: 5.4
"""
if string.startswith(prefix):
return string[len(prefix):]
return string

def removesuffix(string: str, suffix: str) -> str: # skipcq: TYP-053
"""Remove prefix from a string or return a copy otherwise.
"""Remove suffix from a string or return a copy otherwise.
>>> removesuffix('MiscTests', 'Tests')
'Misc'
>>> removesuffix('TmpDirMixin', 'Tests')
'TmpDirMixin'
.. seealso:: :python:`str.removesuffix
<library/stdtypes.html#str.removesuffix>`,
backported from Python 3.9.
.. versionadded:: 5.4
"""
if string.endswith(suffix):
return string[:-len(suffix)]
return string
else:
removeprefix = str.removeprefix # type: ignore[attr-defined]
removesuffix = str.removesuffix # type: ignore[attr-defined]


# bpo-38200
if PYTHON_VERSION >= (3, 10):
from itertools import pairwise
else:
if PYTHON_VERSION < (3, 10) or SPHINX_RUNNING:
from itertools import tee

def pairwise(iterable):
"""Return successive overlapping pairs taken from the input iterable.
.. seealso:: :python:`itertools.pairwise
<library/itertools.html#itertools.pairwise>`,
backported from Python 3.10.
.. versionadded:: 7.6
"""
a, b = tee(iterable)
next(b, None)
return zip(a, b)
else:
from itertools import pairwise
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ commands = {posargs}
[testenv:doc]
commands =
sphinx-build -M html ./docs ./docs/_build
rstcheck --recursive --report warning --ignore-roles api,phab .
basepython = python3.6
rstcheck -r --report-level WARNING --ignore-roles api,phab .
basepython = python3.7
deps =
-rrequirements.txt
-rdocs/requirements.txt
Expand Down

0 comments on commit 01d1499

Please sign in to comment.