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
bschnurr added a commit to bschnurr/pyright that referenced this pull request Nov 16, 2026
Category A: python/typeshed#11844 (6e76a91ed3fc9a2c41b46ac6c3ae1673d0a3c2a1) loses datetime subclass identity and incorrectly infers an omitted combine timezone as None. python/typeshed#16811 fixes combine, but subclass preservation remains unresolved.

Retain the exact pre-PR datetime stub and original OverloadCall1 reveal rather than carrying an incomplete generic migration. Add focused regression coverage for constructors, factories, combine timezone forms, and invalid arguments. Full suite: 3731 tests passed; lint and typecheck passed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
bschnurr added a commit to microsoft/pyright that referenced this pull request Nov 16, 2026
* Update typeshed to 0abbaf725bf8439af222272e14326037fa5d4b2b

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Adjust datetime reveal for generic typeshed annotation

Typeshed made datetime generic over tzinfo in python/typeshed#11844 (6e76a91ed3fc9a2c41b46ac6c3ae1673d0a3c2a1). The existing unparameterized annotation now displays as datetime[Any].

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Defer generic datetime stubs to preserve subclass behavior

Category A: python/typeshed#11844 (6e76a91ed3fc9a2c41b46ac6c3ae1673d0a3c2a1) loses datetime subclass identity and incorrectly infers an omitted combine timezone as None. python/typeshed#16811 fixes combine, but subclass preservation remains unresolved.

Retain the exact pre-PR datetime stub and original OverloadCall1 reveal rather than carrying an incomplete generic migration. Add focused regression coverage for constructors, factories, combine timezone forms, and invalid arguments. Full suite: 3731 tests passed; lint and typecheck passed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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