jenkins-bot submitted this change.

View Change


Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[cleanup] move __add__ and __sub__ definition to Python 3.7 section

Python 3.7 does nor upcast the right Timestamp class. Move these
methods to the other methods which have the same problem.

Change-Id: I2b69fa940c4b4e9744dbe3f5a02b88902894f1a0
---
M pywikibot/time.py
1 file changed, 40 insertions(+), 27 deletions(-)

diff --git a/pywikibot/time.py b/pywikibot/time.py
index 2612cd8..c26082b 100644
--- a/pywikibot/time.py
+++ b/pywikibot/time.py
@@ -360,31 +360,6 @@
"""Return a string format recognized by the API."""
return self.isoformat()

- def __add__(self, other: datetime.timedelta) -> Timestamp:
- """Perform addition, returning a Timestamp instead of datetime."""
- newdt = super().__add__(other)
- if isinstance(newdt, datetime.datetime):
- return self._from_datetime(newdt)
- return newdt
-
- @overload # type: ignore[override]
- def __sub__(self, other: datetime.timedelta) -> Timestamp:
- ...
-
- @overload
- def __sub__(self, other: datetime.datetime) -> datetime.timedelta:
- ...
-
- def __sub__(
- self,
- other: datetime.datetime | datetime.timedelta,
- ) -> datetime.timedelta | Timestamp:
- """Perform subtraction, returning a Timestamp instead of datetime."""
- newdt = super().__sub__(other) # type: ignore[operator]
- if isinstance(newdt, datetime.datetime):
- return self._from_datetime(newdt)
- return newdt
-
@classmethod
def nowutc(cls, *, with_tz: bool = True) -> 'Timestamp':
"""Return the current UTC date and time.
@@ -464,8 +439,34 @@
return cls.nowutc(with_tz=False)

if PYTHON_VERSION < (3, 8) or SPHINX_RUNNING:
- # methods which does not upcast the right class if tz is given
- # but return a datetime.datetime object
+ # methods which does not upcast the right class but return a
+ # datetime.datetime object in Python 3.7
+
+ def __add__(self, other: datetime.timedelta) -> Timestamp:
+ """Perform addition, returning a Timestamp instead of datetime."""
+ newdt = super().__add__(other)
+ if isinstance(newdt, datetime.datetime):
+ return self._from_datetime(newdt)
+ return newdt
+
+ @overload # type: ignore[override]
+ def __sub__(self, other: datetime.timedelta) -> Timestamp:
+ ...
+
+ @overload
+ def __sub__(self, other: datetime.datetime) -> datetime.timedelta:
+ ...
+
+ def __sub__(
+ self,
+ other: datetime.datetime | datetime.timedelta,
+ ) -> datetime.timedelta | Timestamp:
+ """Perform subtraction, returning Timestamp instead of datetime."""
+ newdt = super().__sub__(other) # type: ignore[operator]
+ if isinstance(newdt, datetime.datetime):
+ return self._from_datetime(newdt)
+ return newdt
+
@classmethod
def fromtimestamp(cls, timestamp: int | float, tz=None) -> Timestamp:
"""Return the local date and time corresponding to the POSIX ts.

To view, visit change 1008125. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I2b69fa940c4b4e9744dbe3f5a02b88902894f1a0
Gerrit-Change-Number: 1008125
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged