jenkins-bot submitted this change.
[Bugfix] Return NotImplemented when other is not WbTime
Bug: T325863
Change-Id: I6e40e07a22371b5d5c85bbb62e8ce3a39fe73b90
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 38 insertions(+), 8 deletions(-)
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 9202523..bb91042 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -477,21 +477,29 @@
elapsed_seconds += self.timezone * 60
return elapsed_seconds
- def __lt__(self, other):
+ def __lt__(self, other: object) -> bool:
"""Compare if self is less than other."""
- return self._getSecondsAdjusted() < other._getSecondsAdjusted()
+ if isinstance(other, WbTime):
+ return self._getSecondsAdjusted() < other._getSecondsAdjusted()
+ return NotImplemented
- def __le__(self, other):
+ def __le__(self, other: object) -> bool:
"""Compare if self is less equals other."""
- return self._getSecondsAdjusted() <= other._getSecondsAdjusted()
+ if isinstance(other, WbTime):
+ return self._getSecondsAdjusted() <= other._getSecondsAdjusted()
+ return NotImplemented
- def __gt__(self, other):
+ def __gt__(self, other: object) -> bool:
"""Compare if self is greater than other."""
- return self._getSecondsAdjusted() > other._getSecondsAdjusted()
+ if isinstance(other, WbTime):
+ return self._getSecondsAdjusted() > other._getSecondsAdjusted()
+ return NotImplemented
- def __ge__(self, other):
+ def __ge__(self, other: object) -> bool:
"""Compare if self is greater equals other."""
- return self._getSecondsAdjusted() >= other._getSecondsAdjusted()
+ if isinstance(other, WbTime):
+ return self._getSecondsAdjusted() >= other._getSecondsAdjusted()
+ return NotImplemented
@classmethod
def fromTimestr(cls: Type['WbTime'],
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 3867219..636aa02 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -7,6 +7,7 @@
#
import copy
import json
+import operator
import unittest
from contextlib import suppress
from decimal import Decimal
@@ -409,6 +410,17 @@
self.assertIsInstance(t1.toTimestamp(), pywikibot.Timestamp)
self.assertRaises(ValueError, t2.toTimestamp)
+ def test_comparison_types(self):
+ """Test WbTime comparison with different types."""
+ repo = self.get_repo()
+ t1 = pywikibot.WbTime(site=repo, year=2010, hour=12, minute=43)
+ t2 = pywikibot.WbTime(site=repo, year=-2005, hour=16, minute=45)
+ self.assertGreater(t1, t2)
+ self.assertRaises(TypeError, operator.lt, t1, 5)
+ self.assertRaises(TypeError, operator.gt, t1, 5)
+ self.assertRaises(TypeError, operator.le, t1, 5)
+ self.assertRaises(TypeError, operator.ge, t1, 5)
+
class TestWbQuantity(WbRepresentationTestCase):
To view, visit change 871007. To unsubscribe, or for help writing mail filters, visit settings.