jenkins-bot submitted this change.

View Change


Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] backport itertools.batched from Python 3.13

Python 3.13 batched has a strict option like our own itertools.itergroup
function.

Change-Id: Id3a334ac60ad20128ff14224fc1fc0eb2f5ac52b
---
M pywikibot/backports.py
M pywikibot/tools/itertools.py
2 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/pywikibot/backports.py b/pywikibot/backports.py
index b7a2dab..e49e815 100644
--- a/pywikibot/backports.py
+++ b/pywikibot/backports.py
@@ -5,7 +5,7 @@
instead. The *SimpleQueue* queue; use ``queue.SimpleQueue`` instead.
"""
#
-# (C) Pywikibot team, 2014-2023
+# (C) Pywikibot team, 2014-2024
#
# Distributed under the terms of the MIT license.
#
@@ -135,11 +135,13 @@


# gh-98363
-if PYTHON_VERSION < (3, 12) or SPHINX_RUNNING:
- def batched(iterable, n: int) -> Generator[tuple, None, None]:
+if PYTHON_VERSION < (3, 13) or SPHINX_RUNNING:
+ def batched(iterable, n: int, *,
+ strict: bool = False) -> Generator[tuple, None, None]:
"""Batch data from the *iterable* into tuples of length *n*.

- .. note:: The last batch may be shorter than *n*.
+ .. note:: The last batch may be shorter than *n* if *strict* is
+ True or raise a ValueError otherwise.

Example:

@@ -159,17 +161,32 @@
<library/itertools.html#itertools.batched>`,
backported from Python 3.12.
.. versionadded:: 8.2
+ .. versionchanged:: 9.0
+ Added *strict* option, backported from Python 3.13

:param n: How many items of the iterable to get in one chunk
+ :param strict: raise a ValueError if the final batch is shorter
+ than *n*.
+ :raise ValueError: the final batch is shorter than *n*.
"""
- group = []
- for item in iterable:
- group.append(item)
- if len(group) == n:
+ msg = f'The final batch is shorter than n={n}'
+ if PYTHON_VERSION < (3, 12):
+ group = []
+ for item in iterable:
+ group.append(item)
+ if len(group) == n:
+ yield tuple(group)
+ group.clear()
+ if group:
+ if strict:
+ raise ValueError(msg)
yield tuple(group)
- group.clear()
- if group:
- yield tuple(group)
+ else: # PYTHON_VERSION == (3, 12)
+ from itertools import batched as _batched
+ for group in _batched(iterable, n):
+ if strict and len(group) < n:
+ raise ValueError(msg)
+ yield group
else:
from itertools import batched # type: ignore[no-redef]

diff --git a/pywikibot/tools/itertools.py b/pywikibot/tools/itertools.py
index 4a17c43..41fc5ca 100644
--- a/pywikibot/tools/itertools.py
+++ b/pywikibot/tools/itertools.py
@@ -60,9 +60,7 @@
not divisible by `size`.
:raises ValueError: iterable is not divisible by size
"""
- for group in batched(iterable, size):
- if strict and len(group) < size:
- raise ValueError('iterable is not divisible by size.')
+ for group in batched(iterable, size, strict=strict):
yield list(group)



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

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Id3a334ac60ad20128ff14224fc1fc0eb2f5ac52b
Gerrit-Change-Number: 1007565
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged