jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/236257 )
Change subject: BuiltinNamespace IntEnum ......................................................................
BuiltinNamespace IntEnum
Move the hardcoded namespace identifiers to an IntEnum.
Change-Id: I5576def03c834c0bc180ab4c3d290da746417201 --- M pywikibot/site/_namespace.py M tests/namespace_tests.py 2 files changed, 43 insertions(+), 21 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site/_namespace.py b/pywikibot/site/_namespace.py index 8d5bee2..ebc7735 100644 --- a/pywikibot/site/_namespace.py +++ b/pywikibot/site/_namespace.py @@ -1,10 +1,11 @@ """Objects representing Namespaces of MediaWiki site.""" # -# (C) Pywikibot team, 2008-2020 +# (C) Pywikibot team, 2008-2021 # # Distributed under the terms of the MIT license. # from collections.abc import Iterable, Mapping +from enum import IntEnum from typing import Optional, Union
from pywikibot.backports import List @@ -16,6 +17,30 @@ )
+class BuiltinNamespace(IntEnum): + + """Builtin namespace enum.""" + + MEDIA = -2 + SPECIAL = -1 + MAIN = 0 + TALK = 1 + USER = 2 + USER_TALK = 3 + PROJECT = 4 + PROJECT_TALK = 5 + FILE = 6 + FILE_TALK = 7 + MEDIAWIKI = 8 + MEDIAWIKI_TALK = 9 + TEMPLATE = 10 + TEMPLATE_TALK = 11 + HELP = 12 + HELP_TALK = 13 + CATEGORY = 14 + CATEGORY_TALK = 15 + + class Namespace(Iterable, ComparableMixin):
""" @@ -36,25 +61,6 @@ properties will have the same value. """
- MEDIA = -2 - SPECIAL = -1 - MAIN = 0 - TALK = 1 - USER = 2 - USER_TALK = 3 - PROJECT = 4 - PROJECT_TALK = 5 - FILE = 6 - FILE_TALK = 7 - MEDIAWIKI = 8 - MEDIAWIKI_TALK = 9 - TEMPLATE = 10 - TEMPLATE_TALK = 11 - HELP = 12 - HELP_TALK = 13 - CATEGORY = 14 - CATEGORY_TALK = 15 - # These are the MediaWiki built-in names for MW 1.14+. # Namespace prefixes are always case-insensitive, but the # canonical forms are capitalized. @@ -299,6 +305,11 @@ return False
+# Set Namespace.FOO to be BuiltinNamespace.FOO for each builtin namespace +for item in BuiltinNamespace: + setattr(Namespace, item.name, item) + + class NamespacesDict(Mapping, SelfCallMixin):
""" diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py index c7a834a..b6ea9ac 100644 --- a/tests/namespace_tests.py +++ b/tests/namespace_tests.py @@ -1,6 +1,6 @@ """Tests for the Namespace class.""" # -# (C) Pywikibot team, 2014-2020 +# (C) Pywikibot team, 2014-2021 # # Distributed under the terms of the MIT license. # @@ -8,6 +8,7 @@ from contextlib import suppress
from pywikibot.site import Namespace, NamespacesDict +from pywikibot.site._namespace import BuiltinNamespace
from tests.aspects import TestCase, unittest
@@ -51,6 +52,16 @@
net = False
+ def test_enums(self): + """Test builtin namespace enum in Namespace.""" + self.assertEqual(BuiltinNamespace.MAIN, 0) + self.assertEqual(Namespace.MAIN, BuiltinNamespace.MAIN) + self.assertEqual(Namespace.MEDIA, -2) + self.assertGreater(Namespace.MAIN, Namespace.MEDIA) + self.assertLess(Namespace.MEDIA, Namespace.MAIN) + self.assertEqual(Namespace.CATEGORY, 14) + self.assertGreater(Namespace.CATEGORY, Namespace.HELP_TALK) + def testNamespaceTypes(self): """Test cases for methods manipulating Namespace names.""" ns = Namespace.builtin_namespaces()