Skip to content

Add runtime generic alias support to datetime #161304

Open

Description

@m-aciek

Feature or enhancement

@srittau has merged a PR with a support of parametrising datetime type with a timezone information in the typeshed. Allowing datetime to be a generic would let use that feature without workarounds in Python.

from datetime import datetime, UTC

global_now: datetime[UTC] = datetime.now(UTC)
local_now: datetime[None] = datetime.now()

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

python/typeshed#11844

Linked PRs

Activity

  1. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 4, 2026
  2. srittau commented on Nov 4, 2026

    @srittau
    Contributor

    A few comments:

    • The typeshed change could be a huge help to detect (sometimes rather subtle) bugs with naïve vs tz-aware datetimes.
    • Nevertheless, the change is new and hasn't made it into any type checkers yet (as far as I'm aware). It might turn out to be too controversial and might need backing out in typeshed (although I hope not). So I would wait maybe 1-2 months with this to make sure we keep the change in typeshed.
    • That said, if we keep it, this would be quite helpful and would be in line with similar changes we've made to CPython.
  3. sobolevn commented on Nov 4, 2026

    @sobolevn
    Member

    What about datetime.time? It also has tzinfo parameter in some places:

    >>> import datetime
    >>> datetime.time(4, 23, 1) == datetime.time(4, 23, 1, tzinfo=datetime.timezone.utc)
    False
  4. srittau commented on Nov 4, 2026

    @srittau
    Contributor

    What about datetime.time? It also has tzinfo parameter in some places:

    That seems ... weird. For consistency's sake, we should probably make it generic in typeshed and CPython, although I'm not sure what a time with a tzinfo parameter is even supposed to represent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions