Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
dist
.tox
10 changes: 8 additions & 2 deletions src/datetype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def utctimetuple(self) -> struct_time: ...

def date(self) -> Date: ...

def time(self) -> NaiveDateTime: ...
def time(self) -> NaiveTime: ...

@overload
def replace(
Expand Down Expand Up @@ -591,7 +591,13 @@ def aware(t: _time, tztype: Type[_FuncTZ]) -> Time[_FuncTZ]: ...
def aware(
t: Union[_datetime, _time], tztype: Optional[Type[_FuncTZ]] = None
) -> Union[DateTime[_FuncTZ], Time[_FuncTZ]]:
tzcheck: Type[_tzinfo] = tztype if tztype is not None else _tzinfo
tzcheck: Type[_tzinfo]
if tztype is not None:
tzcheck = tztype
else:
# tzinfo works just fine with isinstance checks, which is why we care
# about matching against type[...] here, so we can cast it away safely
tzcheck = _tzinfo # type:ignore
if not isinstance(t.tzinfo, tzcheck):
raise TypeError(f"{t} is naive, not aware")
return t # type: ignore[return-value]
Expand Down
44 changes: 30 additions & 14 deletions src/datetype/test/test_datetype.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from datetime import date, datetime, time, timezone
from os import popen
from datetime import date, datetime, time, timedelta, timezone
from os import chdir, getcwd, popen
from pathlib import Path
from sys import version_info
from unittest import TestCase
from zoneinfo import ZoneInfo

from datetype import (
AwareDateTime,
NaiveDateTime,
NaiveTime,
Time,
aware,
naive,
)
from datetype import AwareDateTime, NaiveDateTime, NaiveTime, Time, aware, naive

TEST_DATA = (Path(__file__) / "..").resolve()
while not (TEST_DATA / ".git").is_dir():
TEST_DATA = TEST_DATA / ".."
TEST_DATA = TEST_DATA.resolve()


class DateTypeTests(TestCase):
Expand Down Expand Up @@ -57,15 +57,31 @@ def test_mypy_output(self) -> None:
Make sure that we get expected mypy errors.
"""
mypy_command = "mypy"
expected_file_name = "expected_mypy"
expected_file_name = TEST_DATA / "expected_mypy"
if version_info < (3, 9):
mypy_command += " --ignore-missing-imports" # zoneinfo
if version_info[:2] == (3, 7):
expected_file_name += "_37"
expected_file_name = expected_file_name.with_suffix("_37")

with popen(f"{mypy_command} tryit.py") as f:
cwd = getcwd()
try:
chdir(TEST_DATA)
it = popen(f"{mypy_command} tryit.py")
finally:
chdir(cwd)
with it as f:
actual = f.read()
with open(f"{expected_file_name}.txt") as f:
with expected_file_name.with_suffix(".txt").open() as f:
expected = f.read()
self.maxDiff = 9999
self.assertEqual(expected, actual)

def test_none_aware(self) -> None:
"""
L{aware} with no argument will produce a ZoneInfo.
"""
zi = ZoneInfo("US/Pacific")
stddt = datetime(2025, 2, 13, 15, 35, 13, 574354, tzinfo=zi)
awareified = aware(stddt)
self.assertIs(awareified.tzinfo, zi)
self.assertEqual(awareified.tzinfo.dst(stddt), timedelta(0))
Loading