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

Change subject: Cleanup: Remove warning in QueryGenerator.set_namespace
......................................................................

Cleanup: Remove warning in QueryGenerator.set_namespace

Remove warning in QueryGenerator.set_namespace if namespace is not
supported; raise TypeError instead. Also check whether module has
a prefix i.e. self.limited_module is not None.

Update tests.

Bug: T196619
Bug: T198452
Change-Id: I4f078c43646b3aae472c8372ccb818de99f02114
---
M pywikibot/data/api/_generators.py
M tests/api_tests.py
2 files changed, 31 insertions(+), 36 deletions(-)

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




diff --git a/pywikibot/data/api/_generators.py 
b/pywikibot/data/api/_generators.py
index 4f6297f..77ef85a 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -5,7 +5,7 @@
    They are subclassed from :class:`tools.collections.GeneratorWrapper`
 """
 #
-# (C) Pywikibot team, 2008-2025
+# (C) Pywikibot team, 2008-2026
 #
 # Distributed under the terms of the MIT license.
 #
@@ -481,55 +481,53 @@
     def support_namespace(self) -> bool:
         """Check if namespace is a supported parameter on this query.

-        .. note:: this function will be removed when
-           :meth:`set_namespace` will throw TypeError() instead of just
-           giving a warning. See :phab:`T196619`.
+        .. versionadded:: 3.0.20190430
+        .. versionchanged:: 11.1
+           Return False if module has no prefix instead raising
+           AttributeError.

         :return: True if yes, False otherwise
         """
-        assert self.limited_module  # some modules do not have a prefix
+        if not self.limited_module:
+            return False  # some modules do not have a prefix
+
         return bool(
             self.site._paraminfo.parameter('query+' + self.limited_module,
                                            'namespace'))

-    def set_namespace(self, namespaces) -> bool | None:
+    def set_namespace(self, namespaces) -> None:
         """Set a namespace filter on this query.

+        .. versionchanged:: 3.0.20190430
+           No longer raises TypeError if module does not support a
+           namespace parameter bug gives a FutureWarning. Return False
+           in that case.
+        .. versionchanged:: 11.1
+           Again raises TypeError if module does not support a namespace
+           parameter. Check it with :meth:`support_namespace` first.
+           No longer raises AttributeError if module has no prefix.
+
         :param namespaces: namespace identifiers to limit query results
         :type namespaces: iterable of str or Namespace key, or a single
             instance of those types. May be a '|' separated list of
             namespace identifiers. An empty iterator clears any
             namespace restriction.
         :raises KeyError: a namespace identifier was not resolved
+        :raises TypeError: module does not support a namespace parameter
+            or a namespace identifier has an inappropriate type such as
+            NoneType or bool, or more than one namespace if the API
+            module does not support multiple namespaces
         """
-        # TODO: T196619
-        # :raises TypeError: module does not support a namespace parameter
-        #    or a namespace identifier has an inappropriate
-        #    type such as NoneType or bool, or more than one namespace
-        #    if the API module does not support multiple namespaces
-        assert self.limited_module  # some modules do not have a prefix
+        if not self.support_namespace():
+            raise TypeError(f'{self.limited_module or self.modules} module'
+                            ' does not support a namespace parameter')
         param = self.site._paraminfo.parameter('query+' + self.limited_module,
                                                'namespace')
-        if not param:
-            pywikibot.warning(f'{self.limited_module} module does not support'
-                              ' a namespace parameter')
-            warn('set_namespace() will be modified to raise TypeError '
-                 'when namespace parameter is not supported. '
-                 'It will be a Breaking Change, please update your code '
-                 'ASAP, due date July, 31st 2019.', FutureWarning, 2)
-
-            # TODO: T196619
-            # raise TypeError('{} module does not support a namespace '
-            #                 'parameter'.format(self.limited_module))
-
-            return False
-
         if isinstance(namespaces, str):
             namespaces = namespaces.split('|')

         # Use Namespace id (int) here; Request will cast int to str
-        namespaces = [ns.id for ns in
-                      self.site.namespaces.resolve(namespaces)]
+        namespaces = [ns.id for ns in self.site.namespaces.resolve(namespaces)]

         if 'multi' not in param and len(namespaces) != 1:
             if self._check_result_namespace is NotImplemented:
@@ -543,8 +541,6 @@
         elif self.prefix + 'namespace' in self.request:
             del self.request[self.prefix + 'namespace']

-        return None
-
     def continue_update(self) -> None:
         """Update query with continue parameters.

diff --git a/tests/api_tests.py b/tests/api_tests.py
index 1ece3e1..eeeea24 100755
--- a/tests/api_tests.py
+++ b/tests/api_tests.py
@@ -29,6 +29,7 @@
     r'int\(\) argument must be a string, a bytes-like object '
     r"or (?:a real number|a number), not '?NoneType'?"
 )
+SET_NAMESPACE_MODULE = 'module does not support a namespace parameter'


 class TestApiFunctions(DefaultSiteTestCase):
@@ -464,7 +465,7 @@
         """Test PageGenerator set_namespace."""
         for namespace in (0, 1, None):
             with self.subTest(namespace=namespace), \
-                    self.assertRaises(AssertionError):
+                 self.assertRaisesRegex(TypeError, SET_NAMESPACE_MODULE):
                 self.gen.set_namespace(namespace)


@@ -621,17 +622,15 @@
                                      parameters={'titles': 'test'})
         for namespace in (0, 1, None):
             with self.subTest(namespace=namespace), \
-                    self.assertRaises(AssertionError):
+                    self.assertRaises(TypeError, ):
                 self.gen.set_namespace(namespace)

-    @suppress_warnings(
-        r'^set_namespace\(\) will be modified to raise TypeError*',
-        FutureWarning)
     def test_namespace_param_is_not_settable(self) -> None:
         """Test ListGenerator support_namespace."""
         self.gen = api.ListGenerator(listaction='querypage', site=self.site)
         self.assertFalse(self.gen.support_namespace())
-        self.assertFalse(self.gen.set_namespace([0, 1]))
+        with self.assertRaisesRegex(TypeError, SET_NAMESPACE_MODULE):
+            self.gen.set_namespace([0, 1])

     def test_namespace_none(self) -> None:
         """Test ListGenerator set_namespace with None."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1239528?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: I4f078c43646b3aae472c8372ccb818de99f02114
Gerrit-Change-Number: 1239528
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Mpaa <[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