glyph / DateType

A type wrapper for the standard library `datetime` that supplies stricter checks, such as making 'datetime' not substitutable for 'date', and separating out Naive and Aware datetimes into separate, mutually-incompatible types.
Other
73 stars 6 forks source link

`datetime.astimezone()` incorrectly requires a `tz` argument #8

Closed me-and closed 11 months ago

me-and commented 11 months ago

The datetime.astimezone method, according to DateType, requires a tz argument. This doesn't match the behaviour in the datetime module: per https://docs.python.org/3/library/datetime.html#datetime.datetime.astimezone as of Python v3.3, omitting tz or specifying tz=None will obtain the timezone to use from the OS.

Test case below; everything works as expected at runtime, but mypy complains about the lack of tz argument when type checking:

from typing import reveal_type

import datetime

import datetype

print('Using datetype:')
n = datetype.NaiveDateTime.now()
print(n)
reveal_type(n)
a = n.astimezone()
print(a)
reveal_type(a)

print()
print('Using datetime:')
nn = datetime.datetime.now()
print(nn)
reveal_type(nn)
aa = nn.astimezone()
print(aa)
reveal_type(aa)
$ python3 test.py
Using datetype:
2023-09-24 23:12:09.278491
Runtime type is 'datetime'
2023-09-24 23:12:09.278491+01:00
Runtime type is 'datetime'

Using datetime:
2023-09-24 23:12:09.278858
Runtime type is 'datetime'
2023-09-24 23:12:09.278858+01:00
Runtime type is 'datetime'
 $ mypy --strict test.py
test.py:10: note: Revealed type is "datetype.DateTime[None]"
test.py:11: error: Missing positional argument "tz" in call to "astimezone" of "DateTime"  [call-arg]
test.py:11: error: Need type annotation for "a"  [var-annotated]
test.py:13: note: Revealed type is "datetype.DateTime[Any]"
test.py:19: note: Revealed type is "datetime.datetime"
test.py:22: note: Revealed type is "datetime.datetime"
Found 2 errors in 1 file (checked 1 source file)
glyph commented 11 months ago

Thank you for reporting this issue! I'll try to fix it soon, with an appropriately qualified open-source definition of "soon"

me-and commented 11 months ago

No worries, thank you! I am painfully familiar with that set of qualifiers for my own OSS work…

glyph commented 11 months ago

@me-and Fixed & released.

Since I haven't got my commit-message automated script going yet, I will manually say: This fix was sponsored by Jason Walker, Moshez, and Matt Campbell, and other patrons over at https://www.patreon.com/creatorglyph. Remember to like and subscribe ;-)