jenkins-bot submitted this change.
[IMPR] Rewrite len and bool function for Namespace class
- len() is defined as length of the iterable. The iterator uses
Namespace._distinct(). Thus len should use it too instead of calculate it.
This prevents the method could break when Namespace._distinct() or
Namespace.aliases or any other properties will be changed further.
- bool() is derived from len() method but it should be independent from it
and always return True like a generic object class.
- Test added.
Change-Id: I9f37e3d511042317c2c895655c2bb138ef46c18c
---
M pywikibot/site/_namespace.py
M tests/namespace_tests.py
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/pywikibot/site/_namespace.py b/pywikibot/site/_namespace.py
index bafcb76..4f7adba 100644
--- a/pywikibot/site/_namespace.py
+++ b/pywikibot/site/_namespace.py
@@ -144,11 +144,20 @@
return self._contains_lowercase_name(name.lower())
+ def __bool__(self) -> bool:
+ """Obtain boolean method for Namepace class.
+
+ This method is implemented to be independent from __len__ method.
+
+ .. versionadded:: 7.0
+
+ :return: Always return True like generic object class.
+ """
+ return True
+
def __len__(self):
"""Obtain length of the iterable."""
- if self.custom_name == self.canonical_name:
- return len(self.aliases) + 1
- return len(self.aliases) + 2
+ return len(self._distinct())
def __iter__(self):
"""Return an iterator."""
diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py
index 177e8ff..e1cb2d7 100644
--- a/tests/namespace_tests.py
+++ b/tests/namespace_tests.py
@@ -77,6 +77,9 @@
for val in ns.values()
for name in val))
+ # test boolean
+ self.assertTrue(all(x for x in ns.values()))
+
# Use a namespace object as a dict key
self.assertEqual(ns[ns[6]], ns[6])
To view, visit change 279755. To unsubscribe, or for help writing mail filters, visit settings.