Skip to content

Fix datetime.combine inference when tzinfo is omitted - #16811

Merged
srittau merged 1 commit into
python:mainfrom
carljm:cjm/datetime-combine-tzinfo
Nov 15, 2026
Merged

srittau merged 1 commit into
python:mainfrom
carljm:cjm/datetime-combine-tzinfo

Conversation

@carljm

@carljm carljm commented Nov 15, 2026

Copy link
Copy Markdown
Member

Time objects may carry a timezone, and typeshed currently doesn't model the distinction between a timezone-aware vs timezone-naive time.

If a date is combined with a timezone-aware time using datetime.combine, the resulting datetime will have the same timezone. So it is incorrect for the overloads for datetime.combine to claim that combining a date and a time (with no explicit timezone in the combine call) necessarily returns a naive datetime.

We choose datetime[tzinfo] | datetime[None] because invariance of datetime makes this a more flexible type, compared to datetime[tzinfo | None]. This parallels the existing annotation of datetime.fromisoformat().

@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

core (https://github.com/home-assistant/core)
+ homeassistant/components/hassio/auto_update.py:62: error: Need type annotation for "length"  [var-annotated]

@srittau
srittau merged commit 1084116 into python:main Nov 15, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants