GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Make datetime generic over tzinfo by srittau · Pull Request #11844 · python/typeshed

Make datetime generic over tzinfo #11844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
srittau merged 47 commits into python:main from srittau:datetime-tzinfo
Nov 3, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

Make datetime generic over tzinfo #11844

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
4c32d0c
Make datetime generic over tzinfo
srittau Apr 29, 2024
e2a3800
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 29, 2024
814df90
Ignore stubtest error
srittau Apr 29, 2024
a5492ba
Overload operators
srittau Apr 29, 2024
d176a1d
Update some type ignores
srittau Apr 29, 2024
040dbcb
Ignore some mypy errors
srittau Apr 29, 2024
7ce96ea
Make pyright a bit happier
srittau Apr 29, 2024
ce3ee77
Add another type ignore
srittau Apr 29, 2024
c59db10
Make pyright mostly happy
srittau Apr 29, 2024
c0d5cc1
Set default to `Any`
srittau Apr 29, 2024
eb4f7d8
Make both, pyright and mypy happy
srittau Apr 29, 2024
14aa5d2
Annotate datetime.today()
srittau Apr 29, 2024
d8d9d41
Use explicit datetime annotation in test
srittau Apr 29, 2024
f656dbb
Revert bound, use covariance
srittau Apr 29, 2024
25a3e48
Annotate datetime.strptime()
srittau Apr 29, 2024
ce3c470
Reshuffle annotations; use Any as default again
srittau Apr 29, 2024
b148970
Add some type ignores
srittau Apr 29, 2024
f9d5875
Merge branch 'main' into datetime-tzinfo
srittau Nov 10, 2025
605f598
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 10, 2025
510c87f
Revert unittest tests
srittau Nov 10, 2025
2b2c314
Fix a test
srittau Nov 10, 2025
cbf66a8
Add another type ignore
srittau Nov 10, 2025
4974026
Restore changes to check_unittest
srittau Nov 10, 2025
d7b69b1
Fix grouping
srittau Nov 10, 2025
9b8d191
Merge branch 'main' into datetime-tzinfo
srittau Dec 20, 2025
a903506
Merge branch 'main' into datetime-tzinfo
srittau Jan 23, 2026
c57ace3
Merge branch 'main' into datetime-tzinfo
srittau Oct 28, 2026
94cd215
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2026
ae727f4
Fix merge problems
srittau Oct 28, 2026
99c7433
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2026
2683f1b
Change `_TzInfoT` default to `tzinfo | None`
srittau Oct 28, 2026
86b5ced
Use `datetime[None]` for `datetime.{min,max}`
srittau Oct 28, 2026
73d99dc
Remove datetime.fromisoformat() override
srittau Oct 28, 2026
147cfa8
Return `datetime[_TzInfo]` from `astimezone()`
srittau Oct 28, 2026
2df8134
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2026
e2efb0e
Revert `Final` change
srittau Oct 28, 2026
79e966e
Use `datetime[_TzInfo] | datetime[None]` as strptime() return type
srittau Oct 28, 2026
9d1f57a
Use `Any` default for `_TzInfoT`
srittau Oct 28, 2026
15cd3aa
Return `datetime` from `fromisoformat`
srittau Oct 28, 2026
45db0fb
Split fromisoformat datetime union
srittau Oct 29, 2026
d95f348
Update a test
srittau Oct 29, 2026
0e3099c
Remove special casing of datetime.strptime()
srittau Oct 29, 2026
ebf7984
Restore explicit datetime.strptime with changed return type
srittau Oct 29, 2026
d515efb
Fix missing import
srittau Oct 29, 2026
8b0f3a4
Add an extra overload to datetime.fromtimestamp
srittau Oct 29, 2026
637848c
Fix date.fromisoformat
srittau Nov 1, 2026
8656ee9
Remove obsolete comment
srittau Nov 1, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Overload operators
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@srittau
srittau committed Apr 29, 2024
commit a5492ba0a1842d64e9552b23a7da823332ed38c0

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

36 changes: 30 additions & 6 deletions 36 stdlib/datetime.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
from abc import abstractmethod
from time import struct_time
from typing import ClassVar, Generic, Literal, NamedTuple, NoReturn, SupportsIndex, TypeVar, final, overload
from typing import Any, ClassVar, Generic, Literal, NamedTuple, NoReturn, SupportsIndex, TypeVar, final, overload
from typing_extensions import Self, TypeAlias, deprecated

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -388,13 +388,37 @@ class datetime(date, Generic[_TzInfoT]):
def utcoffset(self) -> timedelta | None: ...
def tzname(self) -> str | None: ...
def dst(self) -> timedelta | None: ...
def __le__(self, value: datetime, /) -> bool: ... # type: ignore[override]
def __lt__(self, value: datetime, /) -> bool: ... # type: ignore[override]
def __ge__(self, value: datetime, /) -> bool: ... # type: ignore[override]
def __gt__(self, value: datetime, /) -> bool: ... # type: ignore[override]
@overload
def __le__(self: datetime[_TzInfo], value: datetime[None], /) -> NoReturn: ... # type: ignore[override]
@overload
def __le__(self: datetime[None], value: datetime[_TzInfo], /) -> NoReturn: ... # type: ignore[override]
@overload
def __le__(self: datetime[Any], value: datetime[Any], /) -> bool: ... # type: ignore[override]
@overload
def __lt__(self: datetime[_TzInfo], value: datetime[None], /) -> NoReturn: ... # type: ignore[override]
@overload
def __lt__(self: datetime[None], value: datetime[_TzInfo], /) -> NoReturn: ... # type: ignore[override]
@overload
def __lt__(self: datetime[Any], value: datetime[Any], /) -> bool: ... # type: ignore[override]
@overload
def __ge__(self: datetime[_TzInfo], value: datetime[None], /) -> NoReturn: ... # type: ignore[override]
@overload
def __ge__(self: datetime[None], value: datetime[_TzInfo], /) -> NoReturn: ... # type: ignore[override]
@overload
def __ge__(self: datetime[Any], value: datetime[Any], /) -> bool: ... # type: ignore[override]
@overload
def __gt__(self: datetime[_TzInfo], value: datetime[None], /) -> NoReturn: ... # type: ignore[override]
@overload
def __gt__(self: datetime[None], value: datetime[_TzInfo], /) -> NoReturn: ... # type: ignore[override]
@overload
def __gt__(self: datetime[Any], value: datetime[Any], /) -> bool: ... # type: ignore[override]
def __eq__(self, value: object, /) -> bool: ...
def __hash__(self) -> int: ...
@overload # type: ignore[override]
def __sub__(self, value: Self, /) -> timedelta: ...
def __sub__(self: datetime[_TzInfo], value: datetime[None], /) -> NoReturn: ...
@overload # type: ignore[override]
def __sub__(self: datetime[None], value: datetime[_TzInfo], /) -> NoReturn: ...
@overload # type: ignore[override]
def __sub__(self: datetime[Any], value: datetime[Any], /) -> timedelta: ...
@overload
def __sub__(self, value: timedelta, /) -> Self: ...
49 changes: 37 additions & 12 deletions 49 test_cases/stdlib/check_datetime.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

from datetime import UTC, date, datetime, time, tzinfo
from typing import assert_type
from datetime import UTC, date, datetime, time, timedelta, tzinfo
from typing import Never, assert_type

dt_wo: datetime[None]
dt_none: datetime[None]
dt_tz: datetime[tzinfo]
dt_any: datetime[tzinfo | None]
dt_both: datetime[tzinfo | None]


# Constructors
Expand All @@ -26,6 +26,31 @@

assert_type(datetime.fromisoformat("2000-01-01"), datetime[tzinfo | None])

# Comparisons

assert_type(dt_none < dt_none, bool)
assert_type(dt_tz < dt_tz, bool)
assert_type(dt_both < dt_both, bool)

assert_type(dt_none < dt_tz, Never)
assert_type(dt_tz < dt_none, Never)
assert_type(dt_both < dt_none, bool)
assert_type(dt_both < dt_tz, bool)
assert_type(dt_none < dt_both, bool)

# Sub

assert_type(dt_none - dt_none, timedelta)
assert_type(dt_tz - dt_tz, timedelta)
assert_type(dt_both - dt_both, timedelta)

assert_type(dt_none - dt_tz, Never)
assert_type(dt_tz - dt_none, Never)
assert_type(dt_both - dt_none, timedelta)
assert_type(dt_both - dt_tz, timedelta)
assert_type(dt_none - dt_both, timedelta)
assert_type(dt_tz - dt_both, timedelta)

# Combine

assert_type(datetime.combine(date(2000, 1, 1), time(12, 0)), datetime[None])
Expand All @@ -34,18 +59,18 @@

# Replace

assert_type(dt_wo.replace(year=2001), datetime[None])
assert_type(dt_wo.replace(year=2001, tzinfo=None), datetime[None])
assert_type(dt_wo.replace(year=2001, tzinfo=UTC), datetime[tzinfo])
assert_type(dt_none.replace(year=2001), datetime[None])
assert_type(dt_none.replace(year=2001, tzinfo=None), datetime[None])
assert_type(dt_none.replace(year=2001, tzinfo=UTC), datetime[tzinfo])
assert_type(dt_tz.replace(year=2001), datetime[tzinfo])
assert_type(dt_tz.replace(year=2001, tzinfo=None), datetime[None])
assert_type(dt_tz.replace(year=2001, tzinfo=UTC), datetime[tzinfo])
assert_type(dt_any.replace(year=2001), datetime[tzinfo | None])
assert_type(dt_any.replace(year=2001, tzinfo=None), datetime[None])
assert_type(dt_any.replace(year=2001, tzinfo=UTC), datetime[tzinfo])
assert_type(dt_both.replace(year=2001), datetime[tzinfo | None])
assert_type(dt_both.replace(year=2001, tzinfo=None), datetime[None])
assert_type(dt_both.replace(year=2001, tzinfo=UTC), datetime[tzinfo])

# Attributes

assert_type(dt_wo.tzinfo, None)
assert_type(dt_none.tzinfo, None)
assert_type(dt_tz.tzinfo, tzinfo)
assert_type(dt_any.tzinfo, tzinfo | None)
assert_type(dt_both.tzinfo, tzinfo | None)
4 changes: 2 additions & 2 deletions 4 test_cases/stdlib/check_unittest.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

case.assertAlmostEqual(2.4, 2.41, places=9, delta=0.02) # type: ignore
case.assertAlmostEqual("foo", "bar") # type: ignore
case.assertAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1)) # type: ignore
case.assertAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1))
case.assertAlmostEqual(Decimal("0.4"), Fraction(1, 2)) # type: ignore
case.assertAlmostEqual(complex(2, 3), Decimal("0.9")) # type: ignore

Expand All @@ -44,7 +44,7 @@

case.assertNotAlmostEqual(2.4, 2.41, places=9, delta=0.02) # type: ignore
case.assertNotAlmostEqual("foo", "bar") # type: ignore
case.assertNotAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1)) # type: ignore
case.assertNotAlmostEqual(datetime(1999, 1, 2), datetime(1999, 1, 2, microsecond=1))
case.assertNotAlmostEqual(Decimal("0.4"), Fraction(1, 2)) # type: ignore
case.assertNotAlmostEqual(complex(2, 3), Decimal("0.9")) # type: ignore

Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.