Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1311797?usp=email )

Change subject: IMPR: Improvements for Site.assert_valid_iter_params
......................................................................

IMPR: Improvements for Site.assert_valid_iter_params

- minor interface changes
- overwrite is_ts if start/end parameter are datetime objects
- raise ValueError instead of AssertionError if start/end are in wrong
  order
- use fstring for ValueError message
- update documentation
- update tests

Change-Id: I1d1b44c4bb155f96e587bd7e43f565962d024401
---
M pywikibot/site/_apisite.py
M tests/site_generators_tests.py
2 files changed, 40 insertions(+), 27 deletions(-)

Approvals:
  jenkins-bot: Verified
  Xqt: Looks good to me, approved




diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 4e193d0..4159c97 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -915,44 +915,57 @@
         return f'[{pattern}]*'

     @staticmethod
+    @deprecated_signature(since='11.7.0')
     def assert_valid_iter_params(
         msg_prefix: str,
+        /,
         start: datetime.datetime | int | str,
         end: datetime.datetime | int | str,
         reverse: bool,
+        *,
         is_ts: bool = True
     ) -> None:
         """Validate iterating API parameters.

+        .. version-changed:: 11.7
+           *msg_prefix* is now positional-only and *is_ts* is now
+           keyword-only. If *start* and *end* are ``datetime`` objects,
+           *is_ts* is always treated as ``True``. A ``ValueError`` is
+           raised instead of ``AssertionError`` when *start* and *end*
+           are in wrong order.
+
         :param msg_prefix: The calling method name
         :param start: The start value to compare
         :param end: The end value to compare
         :param reverse: The reverse option
-        :param is_ts: When comparing timestamps (with is_ts=True) the
-            start is usually greater than end. Comparing titles this is
-            vice versa.
-        :raises AssertionError: Start/end values are not comparable
-            types or are in the wrong order
+        :param is_ts: When comparing timestamps (with :code:`is_ts=True`)
+            the start is usually greater than end. Comparing titles this
+            is vice versa.
+        :raises TypeError: The *start* and *end* values are not
+            comparable types.
+        :raises ValueError: The *start* and *end* values are in the wrong
+            order.
         """
         if not (isinstance(end, type(start)) or isinstance(start, type(end))):
             raise TypeError(
                 f'start ({start!r}) and end ({end!r}) must be comparable')
+
+        if isinstance(start, datetime.datetime):
+            is_ts = True
+
         if reverse ^ is_ts:
             low, high = end, start
             order = 'follow'
         else:
             low, high = start, end
             order = 'precede'
-        msg = ('{method}: "start" must {order} "end" '
-               'with reverse={reverse} and is_ts={is_ts} '
-               'but "start" is "{start}" and "end" is "{end}".')
-        assert low < high, fill(msg.format(  # type: ignore[operator]
-            method=msg_prefix,
-            order=order,
-            start=start,
-            end=end,
-            reverse=reverse,
-            is_ts=is_ts))
+
+        if low >= high:  # type: ignore[operator]
+            msg = fill(
+                f"{msg_prefix}: 'start' must {order} 'end' with {reverse=} "
+                f"and {is_ts=} but 'start' is '{start}' and 'end' is '{end}'."
+            )
+            raise ValueError(msg)

     def has_right(self, right: str) -> bool:
         """Return true if and only if the user has a specific right.
diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py
index eb5d493..a5449ee 100755
--- a/tests/site_generators_tests.py
+++ b/tests/site_generators_tests.py
@@ -529,12 +529,12 @@

         # starttime earlier than endtime
         with self.subTest(starttime=low, endtime=high, reverse=False), \
-                self.assertRaises(AssertionError):
+                self.assertRaises(ValueError):
             mysite.blocks(total=5, starttime=low, endtime=high)

         # reverse: endtime earlier than starttime
         with self.subTest(starttime=high, endtime=low, reverse=True), \
-                self.assertRaises(AssertionError):
+                self.assertRaises(ValueError):
             mysite.blocks(total=5, starttime=high, endtime=low, reverse=True)

     def test_exturlusage(self) -> None:
@@ -623,22 +623,22 @@

         # reverse=False, is_ts=False
         self.assertIsNone(func('m', 1, 2, False, False))
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             func('m', 2, 1, False, False)

         # reverse=False, is_ts=True
         self.assertIsNone(func('m', 2, 1, False, True))
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             func('m', 1, 2, False, True)

         # reverse=True, is_ts=False
         self.assertIsNone(func('m', 2, 1, True, False))
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             func('m', 1, 2, True, False)

         # reverse=True, is_ts=True
         self.assertIsNone(func('m', 1, 2, True, True))
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             func('m', 2, 1, True, True)


@@ -871,13 +871,13 @@
                 '2008-02-03T00:00:01Z' <= str(entry.timestamp())
                 <= '2008-02-03T23:59:59Z')
         # starttime earlier than endtime
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.logevents(start=pywikibot.Timestamp.fromISOformat(
                              '2008-02-03T00:00:01Z'),
                              end=pywikibot.Timestamp.fromISOformat(
                              '2008-02-03T23:59:59Z'), total=5)
         # reverse: endtime earlier than starttime
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.logevents(start=pywikibot.Timestamp.fromISOformat(
                              '2008-02-03T23:59:59Z'),
                              end=pywikibot.Timestamp.fromISOformat(
@@ -943,11 +943,11 @@
                 '2008-10-05T06:00:01Z' <= change['timestamp']
                 <= '2008-10-05T23:59:59Z')
         # start earlier than end
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.recentchanges(start='2008-02-03T00:00:01Z',
                                  end='2008-02-03T23:59:59Z', total=5)
         # reverse: end earlier than start
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.recentchanges(start=pywikibot.Timestamp.fromISOformat(
                                  '2008-02-03T23:59:59Z'),
                                  end=pywikibot.Timestamp.fromISOformat(
@@ -1278,12 +1278,12 @@
         """Test the site.usercontribs() method with invalid parameters."""
         mysite = self.get_site()
         # start earlier than end
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.usercontribs(userprefix='Jim',
                                 start='2008-10-03T00:00:01Z',
                                 end='2008-10-03T23:59:59Z', total=5)
         # reverse: end earlier than start
-        with self.assertRaises(AssertionError):
+        with self.assertRaises(ValueError):
             mysite.usercontribs(userprefix='Jim',
                                 start='2008-10-03T23:59:59Z',
                                 end='2008-10-03T00:00:01Z',

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1311797?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I1d1b44c4bb155f96e587bd7e43f565962d024401
Gerrit-Change-Number: 1311797
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to