Add abstract methods to BaseTzInfo (#6579)

While these abstract methods don't exist at runtime, all sub-classes of
BaseTzInfo implement them. It can be useful to annotate variables with
BaseTzInfo and being able to call these methods on it.
This commit is contained in:
Sebastian Rittau
2021-12-14 14:14:21 +01:00
committed by GitHub
parent f3026dc3ab
commit 968fd6d01d
2 changed files with 18 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
# "Abstract" methods, see the .pyi file for more details.
pytz.BaseTzInfo.localize
pytz.BaseTzInfo.normalize
pytz.tzinfo.BaseTzInfo.localize
pytz.tzinfo.BaseTzInfo.normalize

View File

@@ -1,8 +1,21 @@
import datetime
from abc import abstractmethod
from typing import Any
class BaseTzInfo(datetime.tzinfo):
zone: str | None # Actually None but should be set on concrete subclasses
# The following abstract methods don't exist in the implementation, but
# are implemented by all sub-classes.
@abstractmethod
def localize(self, dt: datetime.datetime) -> datetime.datetime: ...
@abstractmethod
def normalize(self, dt: datetime.datetime) -> datetime.datetime: ...
@abstractmethod
def tzname(self, dt: datetime.datetime | None) -> str: ...
@abstractmethod
def utcoffset(self, dt: datetime.datetime | None) -> datetime.timedelta | None: ...
@abstractmethod
def dst(self, dt: datetime.datetime | None) -> datetime.timedelta | None: ...
class StaticTzInfo(BaseTzInfo):
def fromutc(self, dt: datetime.datetime) -> datetime.datetime: ...