From 968fd6d01d23470e0c8368e7ee7c43f54aaedc0e Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Tue, 14 Dec 2021 14:14:21 +0100 Subject: [PATCH] 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. --- stubs/pytz/@tests/stubtest_allowlist.txt | 5 +++++ stubs/pytz/pytz/tzinfo.pyi | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 stubs/pytz/@tests/stubtest_allowlist.txt diff --git a/stubs/pytz/@tests/stubtest_allowlist.txt b/stubs/pytz/@tests/stubtest_allowlist.txt new file mode 100644 index 000000000..c25eb8283 --- /dev/null +++ b/stubs/pytz/@tests/stubtest_allowlist.txt @@ -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 diff --git a/stubs/pytz/pytz/tzinfo.pyi b/stubs/pytz/pytz/tzinfo.pyi index b02f560a1..d60864ec0 100644 --- a/stubs/pytz/pytz/tzinfo.pyi +++ b/stubs/pytz/pytz/tzinfo.pyi @@ -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: ...