Skip to content

Commit d1666ed

Browse files
authored
Merge pull request #12 from mttbernardini/patch-1
fix: typo in return type of `DateTime.time()`
2 parents 7a09422 + 716e650 commit d1666ed

File tree

8 files changed

+54
-21
lines changed

8 files changed

+54
-21
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
python: ["3.12", "3.11", "3.10", "3.9", "3.8", "3.7"]
21+
python: ["3.13", "3.12", "3.11", "3.10", "3.9"]
2222
TOX_ENV: ["lint", "py", "mypy"]
2323
exclude:
2424
- TOX_ENV: "lint"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
build
2+
dist
23
.tox

expected_mypy.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ tryit.py:53: note: def timetz(self) -> Time[None]
4343
tryit.py:53: note: Got:
4444
tryit.py:53: note: def timetz(self) -> Time[timezone]
4545
tryit.py:53: note: tzinfo: expected "None", got "timezone"
46-
Found 9 errors in 1 file (checked 1 source file)
46+
tryit.py:56: error: Incompatible types in assignment (expression has type "NaiveTime", variable has type "DateTime[timezone]") [assignment]
47+
Found 10 errors in 1 file (checked 1 source file)

expected_mypy_37.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ tryit.py:53: note: def timetz(self) -> Time[None]
2929
tryit.py:53: note: Got:
3030
tryit.py:53: note: def timetz(self) -> Time[timezone]
3131
tryit.py:53: note: tzinfo: expected "None", got "timezone"
32-
Found 7 errors in 1 file (checked 1 source file)
32+
tryit.py:56: error: Incompatible types in assignment (expression has type "NaiveTime", variable has type "DateTime[timezone]") [assignment]
33+
Found 8 errors in 1 file (checked 1 source file)

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ classifiers = [
1414
"Development Status :: 5 - Production/Stable",
1515
# Indicate who your project is intended for
1616
"Intended Audience :: Developers",
17+
"Programming Language :: Python :: 3.13",
1718
"Programming Language :: Python :: 3.12",
1819
"Programming Language :: Python :: 3.11",
1920
"Programming Language :: Python :: 3.10",
2021
"Programming Language :: Python :: 3.9",
2122
"Programming Language :: Python :: 3.8",
2223
"Programming Language :: Python :: 3.7",
2324
]
25+
dependencies = [
26+
"typing_extensions ; python_version < '3.8'"
27+
]
2428

2529
[build-system]
2630
build-backend = "setuptools.build_meta"

src/datetype/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def utctimetuple(self) -> struct_time: ...
341341

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

344-
def time(self) -> NaiveDateTime: ...
344+
def time(self) -> NaiveTime: ...
345345

346346
@overload
347347
def replace(
@@ -591,7 +591,13 @@ def aware(t: _time, tztype: Type[_FuncTZ]) -> Time[_FuncTZ]: ...
591591
def aware(
592592
t: Union[_datetime, _time], tztype: Optional[Type[_FuncTZ]] = None
593593
) -> Union[DateTime[_FuncTZ], Time[_FuncTZ]]:
594-
tzcheck: Type[_tzinfo] = tztype if tztype is not None else _tzinfo
594+
tzcheck: Type[_tzinfo]
595+
if tztype is not None:
596+
tzcheck = tztype
597+
else:
598+
# tzinfo works just fine with isinstance checks, which is why we care
599+
# about matching against type[...] here, so we can cast it away safely
600+
tzcheck = _tzinfo # type:ignore
595601
if not isinstance(t.tzinfo, tzcheck):
596602
raise TypeError(f"{t} is naive, not aware")
597603
return t # type: ignore[return-value]

src/datetype/test/test_datetype.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
from datetime import date, datetime, time, timezone
2-
from os import popen
1+
from datetime import date, datetime, time, timedelta, timezone
2+
from os import chdir, getcwd, popen
3+
from pathlib import Path
34
from sys import version_info
4-
from unittest import TestCase
5+
from unittest import TestCase, skipIf
56

6-
from datetype import (
7-
AwareDateTime,
8-
NaiveDateTime,
9-
NaiveTime,
10-
Time,
11-
aware,
12-
naive,
13-
)
7+
from datetype import AwareDateTime, NaiveDateTime, NaiveTime, Time, aware, naive
8+
9+
TEST_DATA = (Path(__file__) / "..").resolve()
10+
while not (TEST_DATA / ".git").is_dir():
11+
TEST_DATA = TEST_DATA / ".."
12+
TEST_DATA = TEST_DATA.resolve()
1413

1514

1615
class DateTypeTests(TestCase):
@@ -57,15 +56,34 @@ def test_mypy_output(self) -> None:
5756
Make sure that we get expected mypy errors.
5857
"""
5958
mypy_command = "mypy"
60-
expected_file_name = "expected_mypy"
59+
expected_file_name = (
60+
TEST_DATA / f"expected_mypy{'_37' if (version_info < (3, 8)) else ''}"
61+
)
6162
if version_info < (3, 9):
6263
mypy_command += " --ignore-missing-imports" # zoneinfo
63-
if version_info[:2] == (3, 7):
64-
expected_file_name += "_37"
6564

66-
with popen(f"{mypy_command} tryit.py") as f:
65+
cwd = getcwd()
66+
try:
67+
chdir(TEST_DATA)
68+
it = popen(f"{mypy_command} tryit.py")
69+
finally:
70+
chdir(cwd)
71+
with it as f:
6772
actual = f.read()
68-
with open(f"{expected_file_name}.txt") as f:
73+
with expected_file_name.with_suffix(".txt").open() as f:
6974
expected = f.read()
7075
self.maxDiff = 9999
7176
self.assertEqual(expected, actual)
77+
78+
@skipIf(version_info < (3, 9), "ZoneInfo")
79+
def test_none_aware(self) -> None:
80+
"""
81+
L{aware} with no argument will produce a ZoneInfo.
82+
"""
83+
from zoneinfo import ZoneInfo
84+
85+
zi = ZoneInfo("US/Pacific")
86+
stddt = datetime(2025, 2, 13, 15, 35, 13, 574354, tzinfo=zi)
87+
awareified = aware(stddt)
88+
self.assertIs(awareified.tzinfo, zi)
89+
self.assertEqual(awareified.tzinfo.dst(stddt), timedelta(0))

tryit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@
5252

5353
not_naive: NaiveDateTime = a.astimezone()
5454
is_aware: DateTime[timezone] = a.astimezone(None)
55+
56+
not_aware_method_time: DateTime[timezone] = a.time() # nope; actually naive

0 commit comments

Comments
 (0)