jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1175523?usp=email )

Change subject: Tests: Fix watch() to return False if page missing and no 
expiry set
......................................................................

Tests: Fix watch() to return False if page missing and no expiry set

- Return False when watching pages without expiry if any page is missing,
  reflecting that missing pages are not added to the watchlist.
- Adjust test_watch() to assert this behavior.
- Improve condition checking for 'missing', 'watched', and absence of 'expiry'.
- Update docstring and add a note about this case.

Bug: T330839
Change-Id: I0183f12879495814c31d68d1b00a96580b9a3b6c
---
M pywikibot/site/_apisite.py
M tests/page_tests.py
2 files changed, 25 insertions(+), 5 deletions(-)

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




diff --git a/pywikibot/site/_apisite.py b/pywikibot/site/_apisite.py
index 29ca580..34550dd 100644
--- a/pywikibot/site/_apisite.py
+++ b/pywikibot/site/_apisite.py
@@ -2934,6 +2934,10 @@
            Passing *unwatch* as a positional parameter is deprecated;
            it must be passed as keyword argument.

+           .. note:: When watching a page without *expiry*, the function
+              returns False if any page does not exist, because it was
+              not added to the watchlist.
+
         .. seealso::
            - :api:`Watch`
            - :meth:`BasePage.watch`
@@ -2950,7 +2954,9 @@
             For no expiry, use ``infinite``, ``indefinite``, ``infinity``
             or `never`. For absolute timestamps the :class:`Timestamp`
             class can be used.
-        :return: True if API returned expected response; False otherwise
+        :return: True if API returns expected response; False otherwise.
+            If *unwatch* is False, *expiry* is None or specifies no
+            defined end date, return False if the page does not exist.
         :raises APIError: badexpiry: Invalid value for expiry parameter
         :raises KeyError: 'watch' isn't in API response
         :raises TypeError: unexpected keyword argument
@@ -2962,16 +2968,26 @@
             'unwatch': unwatch,
             'expiry': expiry or None,
         }
+
         if not unwatch:
             parameters['expiry'] = expiry or None
         elif expiry:
             msg = (f'\nexpiry parameter ({expiry!r}) is ignored when '
                    f"unwatch=True.\nPlease omit 'expiry' when unwatching.")
             warn(msg, category=UserWarning, stacklevel=2)
+
         req = self.simple_request(**parameters)
         results = req.submit()
-        unwatch_s = 'unwatched' if unwatch else 'watched'
-        return all(unwatch_s in r for r in results['watch'])
+        watchtype = 'unwatched' if unwatch else 'watched'
+
+        for r in results['watch']:
+            if watchtype not in r:
+                return False
+
+            if 'missing' in r and 'watched' in r and 'expiry' not in r:
+                return False
+
+        return True

     def purgepages(
         self,
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 813fb3b..3baebee 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -1088,11 +1088,15 @@
         # watched_pages parameters
         wp_params = {'force': True, 'with_talkpage': False}
         rv = userpage.watch()
-        self.assertTrue(rv)
-        self.assertIn(userpage, userpage.site.watched_pages(**wp_params))
+
+        self.assertEqual(userpage.exists(), rv)
+        if rv:
+            self.assertIn(userpage, userpage.site.watched_pages(**wp_params))
+
         with self.assertWarnsRegex(UserWarning,
                                    r"expiry parameter \('.+'\) is ignored"):
             rv = userpage.watch(unwatch=True, expiry='indefinite')
+
         self.assertTrue(rv)
         rv = userpage.watch(expiry='5 seconds')
         self.assertTrue(rv)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1175523?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: I0183f12879495814c31d68d1b00a96580b9a3b6c
Gerrit-Change-Number: 1175523
Gerrit-PatchSet: 1
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