Skip to content

Commit

Permalink
pywikibot.time: add typing
Browse files Browse the repository at this point in the history
Change-Id: I260113b7dfa66918a019e6edce044272d341d408
  • Loading branch information
JJMC89 committed Dec 31, 2023
1 parent ee54f01 commit 60f7525
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions pywikibot/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import types
from contextlib import suppress
from typing import overload

import pywikibot
from pywikibot.tools import classproperty, deprecated
Expand Down Expand Up @@ -212,7 +213,7 @@ def _from_string(cls, timestr: str) -> Timestamp:

raise ValueError(f'time data {timestr!r} does not match any format.')

@deprecated('replace method', since='8.0.0')
@deprecated('replace method', since='8.0.0') # type: ignore[misc]
def clone(self) -> Timestamp:
"""Clone this instance.
Expand Down Expand Up @@ -250,7 +251,7 @@ def fromISOformat(cls, # noqa: N802
# to create a clone.
if isinstance(ts, cls):
return ts.replace()

assert isinstance(ts, str)
return cls._from_iso8601(f'{ts[:10]}{sep}{ts[11:]}')

@classmethod
Expand Down Expand Up @@ -288,7 +289,7 @@ def fromtimestampformat(cls,
# to create a clone.
if isinstance(ts, cls):
return ts.replace()

assert isinstance(ts, str)
if len(ts) == 8 and not strict:
# year, month and day are given only
ts += '000000'
Expand Down Expand Up @@ -344,10 +345,20 @@ def __add__(self, other: datetime.timedelta) -> Timestamp:
return self._from_datetime(newdt)
return newdt

def __sub__(self, other: datetime.timedelta # type: ignore[override]
) -> Timestamp:
@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)
newdt = super().__sub__(other) # type: ignore[operator]
if isinstance(newdt, datetime.datetime):
return self._from_datetime(newdt)
return newdt
Expand All @@ -367,15 +378,15 @@ def __init__(self, offset: int, name: str) -> None:
self._offset = datetime.timedelta(minutes=offset)
self._name = name

def utcoffset(self, dt):
def utcoffset(self, dt: datetime.datetime | None) -> datetime.timedelta:
"""Return the offset to UTC."""
return self._offset

def tzname(self, dt):
def tzname(self, dt: datetime.datetime | None) -> str:
"""Return the name of the timezone."""
return self._name

def dst(self, dt):
def dst(self, dt: datetime.datetime | None) -> datetime.timedelta:
"""Return no daylight savings time."""
return datetime.timedelta(0)

Expand All @@ -388,7 +399,10 @@ def __repr__(self) -> str:
)


def str2timedelta(string: str, timestamp=None) -> datetime.timedelta:
def str2timedelta(
string: str,
timestamp: datetime.datetime | None = None,
) -> datetime.timedelta:
"""
Return a timedelta for a shorthand duration.
Expand Down

0 comments on commit 60f7525

Please sign in to comment.