Skip to content

Commit e9a90eb

Browse files
filiplajszczakmkurnikov
authored andcommitted
More precise annotations of utils.timezone functions that return instances of tzinfo subclasses. (#209)
* fix annotations of utils.timezone * use intermediary tzinfo subclass exposed in pytz typeshed * fix annotations of get_fixed_timezone as it returns datetime.timezone in Django 2.2 * add explanatory comment to get_current_timezone annotations * black utils.timezone.pyi
1 parent 7b74a69 commit e9a90eb

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

django-stubs/utils/timezone.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import types
22
from contextlib import ContextDecorator
3-
from datetime import date, datetime as datetime, time, timedelta as timedelta, tzinfo as tzinfo
3+
from datetime import date, datetime as datetime, time, timedelta as timedelta, tzinfo as tzinfo, timezone
44
from typing import Optional, Union, Type
55

6+
from pytz import BaseTzInfo
7+
68
_AnyTime = Union[time, datetime]
79

810
class UTC(tzinfo):
@@ -30,10 +32,14 @@ class LocalTimezone(ReferenceLocalTimezone):
3032

3133
utc: UTC = ...
3234

33-
def get_fixed_timezone(offset: Union[timedelta, int]) -> tzinfo: ...
34-
def get_default_timezone() -> tzinfo: ...
35+
def get_fixed_timezone(offset: Union[timedelta, int]) -> timezone: ...
36+
def get_default_timezone() -> BaseTzInfo: ...
3537
def get_default_timezone_name() -> str: ...
36-
def get_current_timezone() -> tzinfo: ...
38+
39+
# Strictly speaking, it is possible to activate() a non-pytz timezone,
40+
# in which case BaseTzInfo is incorrect. However, this is unlikely,
41+
# so we use it anyway, to keep things ergonomic for most users.
42+
def get_current_timezone() -> BaseTzInfo: ...
3743
def get_current_timezone_name() -> str: ...
3844
def activate(timezone: Union[tzinfo, str]) -> None: ...
3945
def deactivate() -> None: ...

0 commit comments

Comments
 (0)