Repository navigation
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.
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
+273
−29
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
4c32d0c
Make datetime generic over tzinfo
srittau e2a3800
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 814df90
Ignore stubtest error
srittau a5492ba
Overload operators
srittau d176a1d
Update some type ignores
srittau 040dbcb
Ignore some mypy errors
srittau 7ce96ea
Make pyright a bit happier
srittau ce3ee77
Add another type ignore
srittau c59db10
Make pyright mostly happy
srittau c0d5cc1
Set default to `Any`
srittau eb4f7d8
Make both, pyright and mypy happy
srittau 14aa5d2
Annotate datetime.today()
srittau d8d9d41
Use explicit datetime annotation in test
srittau f656dbb
Revert bound, use covariance
srittau 25a3e48
Annotate datetime.strptime()
srittau ce3c470
Reshuffle annotations; use Any as default again
srittau b148970
Add some type ignores
srittau f9d5875
Merge branch 'main' into datetime-tzinfo
srittau 605f598
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 510c87f
Revert unittest tests
srittau 2b2c314
Fix a test
srittau cbf66a8
Add another type ignore
srittau 4974026
Restore changes to check_unittest
srittau d7b69b1
Fix grouping
srittau 9b8d191
Merge branch 'main' into datetime-tzinfo
srittau a903506
Merge branch 'main' into datetime-tzinfo
srittau c57ace3
Merge branch 'main' into datetime-tzinfo
srittau 94cd215
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ae727f4
Fix merge problems
srittau 99c7433
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2683f1b
Change `_TzInfoT` default to `tzinfo | None`
srittau 86b5ced
Use `datetime[None]` for `datetime.{min,max}`
srittau 73d99dc
Remove datetime.fromisoformat() override
srittau 147cfa8
Return `datetime[_TzInfo]` from `astimezone()`
srittau 2df8134
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e2efb0e
Revert `Final` change
srittau 79e966e
Use `datetime[_TzInfo] | datetime[None]` as strptime() return type
srittau 9d1f57a
Use `Any` default for `_TzInfoT`
srittau 15cd3aa
Return `datetime` from `fromisoformat`
srittau 45db0fb
Split fromisoformat datetime union
srittau d95f348
Update a test
srittau 0e3099c
Remove special casing of datetime.strptime()
srittau ebf7984
Restore explicit datetime.strptime with changed return type
srittau d515efb
Fix missing import
srittau 8b0f3a4
Add an extra overload to datetime.fromtimestamp
srittau 637848c
Fix date.fromisoformat
srittau 8656ee9
Remove obsolete comment
srittau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import date, datetime, time, timedelta, timezone, tzinfo | ||
| from typing import Any, Union, cast | ||
| from typing_extensions import Never, assert_type | ||
|
|
||
| UTC: timezone = timezone.utc | ||
|
|
||
| dt_none = cast(datetime[None], None) | ||
| dt_tz = cast(datetime[tzinfo], None) | ||
| dt_both = cast(datetime[Union[tzinfo, None]], None) | ||
|
|
||
| # Constructors | ||
|
|
||
| assert_type(datetime(2000, 1, 1), datetime[None]) | ||
| assert_type(datetime(2000, 1, 1, tzinfo=None), datetime[None]) | ||
| assert_type(datetime(2000, 1, 1, tzinfo=UTC), datetime[tzinfo]) | ||
|
|
||
| assert_type(datetime.fromtimestamp(0), datetime[None]) | ||
| assert_type(datetime.fromtimestamp(0, None), datetime[None]) | ||
| assert_type(datetime.fromtimestamp(0, UTC), datetime[tzinfo]) | ||
| assert_type(datetime.utcfromtimestamp(0), datetime[None]) # pyright: ignore[reportDeprecated] | ||
|
|
||
| assert_type(datetime.now(), datetime[None]) | ||
| assert_type(datetime.now(None), datetime[None]) | ||
| assert_type(datetime.now(UTC), datetime[tzinfo]) | ||
| assert_type(datetime.today(), datetime[None]) | ||
| assert_type(datetime.utcnow(), datetime[None]) # pyright: ignore[reportDeprecated] | ||
|
|
||
| assert_type(datetime.fromisoformat("2000-01-01"), Union[datetime[tzinfo], datetime[None]]) | ||
|
|
||
| assert_type(datetime.strptime("", ""), datetime[Any]) | ||
|
|
||
| # 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]) | ||
| assert_type(datetime.combine(date(2000, 1, 1), time(12, 0), tzinfo=None), datetime[None]) | ||
| assert_type(datetime.combine(date(2000, 1, 1), time(12, 0), tzinfo=UTC), datetime[tzinfo]) | ||
|
|
||
| # Replace | ||
|
|
||
| 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_both.replace(year=2001), datetime[Union[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_none.tzinfo, None) | ||
| assert_type(dt_tz.tzinfo, tzinfo) | ||
| assert_type(dt_both.tzinfo, Union[tzinfo, None]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.