Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/datetype/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
AnyDateTime = TypeVar("AnyDateTime", bound="DateTime[Optional[_tzinfo]]")
AnyTime = TypeVar("AnyTime", bound="Time[Optional[_tzinfo]]")

_missing = object()

if sys.version_info >= (3, 9):

class _IsoCalendarDate(NamedTuple):
Expand Down Expand Up @@ -513,8 +515,10 @@ def combine(
cls,
date: Date,
time: Time[Optional[_tzinfo]],
tzinfo: Optional[_tzinfo] = None,
tzinfo: Optional[_tzinfo] = _missing, # type: ignore[assignment]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some context for the sentinel I used here:

The Python implemention of CPython's datetime module uses True for the sentinel, but that is not part of the API and doesn't work with the builtin module:

❯ uv run --isolated --no-project -p 3.14 python
Python 3.14.0 (main, Oct 28 2025, 12:03:45) [Clang 20.1.4 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _datetime
>>> import _pydatetime
>>> _pydatetime.datetime.combine(_pydatetime.date.today(), _pydatetime.time(tzinfo=_pydatetime.UTC), True)
datetime.datetime(2025, 11, 11, 0, 0, tzinfo=datetime.timezone.utc)
>>> _datetime.datetime.combine(_datetime.date.today(), _datetime.time(tzinfo=_datetime.UTC), True)
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    _datetime.datetime.combine(_datetime.date.today(), _datetime.time(tzinfo=_datetime.UTC), True)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'bool'

) -> DateTime[Optional[_tzinfo]]:
if tzinfo is _missing:
tzinfo = time.tzinfo
return _datetime.combine(
concrete(date), concrete(time), tzinfo
) # type:ignore[return-value]
Expand Down
29 changes: 29 additions & 0 deletions src/datetype/test/test_datetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
aware,
naive,
DateTime,
date_only,
)

TEST_DATA = (Path(__file__) / "..").resolve()
Expand Down Expand Up @@ -109,3 +110,31 @@ def test_differing_zone_subtract(self) -> None:

self.assertEqual(dtzi - dttz, timedelta(0))
self.assertEqual(dttz - dtzi, timedelta(0))

def test_combine(self) -> None:
from zoneinfo import ZoneInfo

d = date_only(date(2025, 2, 13))
aware_time = aware(
time(hour=15, minute=35, second=13, tzinfo=timezone.utc), timezone
)
naive_time = naive(time(hour=15, minute=35, second=13))

combined_aware: DateTime[timezone] = DateTime.combine(d, aware_time)
combined_naive: DateTime[None] = DateTime.combine(d, naive_time)

self.assertIsInstance(combined_naive, NaiveDateTime)
self.assertIsInstance(combined_aware, AwareDateTime)
self.assertIs(combined_aware.tzinfo, timezone.utc)

zi = ZoneInfo("Europe/Berlin")

adt_zi: DateTime[ZoneInfo] = DateTime.combine(d, aware_time, zi)
self.assertIsInstance(adt_zi, AwareDateTime)
adt_naive: DateTime[None] = DateTime.combine(d, aware_time, tzinfo=None)
self.assertIsInstance(adt_naive, NaiveDateTime)

ndt_zi: DateTime[ZoneInfo] = DateTime.combine(d, naive_time, zi)
self.assertIsInstance(ndt_zi, AwareDateTime)
ndt_naive: DateTime[None] = DateTime.combine(d, naive_time, tzinfo=None)
self.assertIsInstance(ndt_naive, NaiveDateTime)