Distinguish between datetime.datetime objects with a timezone and those without
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 25/100
- Issue 類型
- 功能
- 描述清晰度
- 需要釐清
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
首先檢視提議的 DatetimeWithTimezone 和 DatetimeWithoutTimezone 型別,以及列出的 datetime 建構函式和方法。判斷是否能以相容於 Python typing 的方式加入這項區分,以及它是否屬於 datetime 函式庫;完成這項工作需要解決這些設計問題,並定義一個達成共識的範圍。
由索引模型根據 Issue 內容生成。
描述
datetime.datetime objects created with timezone information cannot be compared to datetime objects without timezone information. This error comes up
TypeError: can't compare offset-naive and offset-aware datetimes
I believe it is possible for the type system to handle distinguishing between these two objects.
I have a proof of concept at my Company that looks something like this. The idea is to distinguish between these two object types and forbid their corresponding comparisons.
class DatetimeWithTimezone(datetime_lib.datetime):
"""Datetime with timezone."""
@overload
def replace(
self,
year: SupportsIndex = ...,
month: SupportsIndex = ...,
day: SupportsIndex = ...,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: _TzInfo = ...,
*,
fold: int = ...,
) -> Self: ...
@overload
def replace(
self,
year: SupportsIndex = ...,
month: SupportsIndex = ...,
day: SupportsIndex = ...,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: None = ...,
*,
fold: int = ...,
) -> DatetimeWithoutTimezone: ...
@overload
def astimezone(self, tz: _TzInfo) -> Self: ...
@overload
def astimezone(self, tz: None) -> DatetimeWithoutTimezone: ...
def utcoffset(self) -> timedelta: ...
def tzname(self) -> str: ...
def dst(self) -> timedelta: ...
def __le__(self, value: DatetimeWithTimezone, /) -> bool: ... # type: ignore[override]
def __lt__(self, value: DatetimeWithTimezone, /) -> bool: ... # type: ignore[override]
def __ge__(self, value: DatetimeWithTimezone, /) -> bool: ... # type: ignore[override]
def __gt__(self, value: DatetimeWithTimezone, /) -> bool: ... # type: ignore[override]
@overload # type: ignore[override]
def __sub__(self, value: Self, /) -> timedelta: ...
@overload
def __sub__(self, value: timedelta, /) -> Self: ...
class DatetimeWithoutTimezone(datetime_lib.datetime):
"""Datetime without timezone."""
@overload
def replace(
self,
year: SupportsIndex = ...,
month: SupportsIndex = ...,
day: SupportsIndex = ...,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: _TzInfo = ...,
*,
fold: int = ...,
) -> DatetimeWithTimezone: ...
@overload
def replace(
self,
year: SupportsIndex = ...,
month: SupportsIndex = ...,
day: SupportsIndex = ...,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: None = ...,
*,
fold: int = ...,
) -> Self: ...
@overload
def astimezone(self, tz: _TzInfo) -> DatetimeWithoutTimezone: ...
@overload
def astimezone(self, tz: None) -> Self: ...
def utcoffset(self) -> None: ...
def tzname(self) -> None: ...
def dst(self) -> None: ...
def __le__(self, value: DatetimeWithoutTimezone, /) -> bool: ... # type: ignore[override]
def __lt__(self, value: DatetimeWithoutTimezone, /) -> bool: ... # type: ignore[override]
def __ge__(self, value: DatetimeWithoutTimezone, /) -> bool: ... # type: ignore[override]
def __gt__(self, value: DatetimeWithoutTimezone, /) -> bool: ... # type: ignore[override]
@overload # type: ignore[override]
def __sub__(self, value: Self, /) -> timedelta: ...
@overload
def __sub__(self, value: timedelta, /) -> Self: ...
class datetime(datetime_lib.datetime):
@overload
def __new__(
cls,
year: SupportsIndex,
month: SupportsIndex,
day: SupportsIndex,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: _TzInfo = ...,
*,
fold: int = ...,
) -> DatetimeWithTimezone: ...
@overload
def __new__(
cls,
year: SupportsIndex,
month: SupportsIndex,
day: SupportsIndex,
hour: SupportsIndex = ...,
minute: SupportsIndex = ...,
second: SupportsIndex = ...,
microsecond: SupportsIndex = ...,
tzinfo: None = ...,
*,
fold: int = ...,
) -> DatetimeWithoutTimezone: ...
# On <3.12, the name of the first parameter in the pure-Python implementation
# didn't match the name in the C implementation,
# meaning it is only *safe* to pass it as a keyword argument on 3.12+
# Assume are on 3.12+
@overload
@classmethod
def fromtimestamp(
cls, timestamp: float, tz: None = ...
) -> DatetimeWithoutTimezone: ...
@overload
@classmethod
def fromtimestamp(
cls, timestamp: float, tz: _TzInfo
) -> DatetimeWithTimezone: ...
@overload
@classmethod
def now(cls, tz: _TzInfo) -> DatetimeWithTimezone: ...
@overload
@classmethod
def now(cls, tz: None = ...) -> DatetimeWithoutTimezone: ...
@overload
@classmethod
def combine(
cls, date: _Date, time: _Time, tzinfo: None = ...
) -> DatetimeWithoutTimezone: ...
@overload
@classmethod
def combine(
cls, date: _Date, time: _Time, tzinfo: _TzInfo
) -> DatetimeWithTimezone: ...
@classmethod
def strptime(
cls, date_string: str, format: str, /
) -> DatetimeWithTimezone | DatetimeWithoutTimezone: ...
I have some questions before I'm certain this could be possible for everyone
- Is it possible to make this backwards compatible?
- Is this something others are interested in?
- Should this actually be solved in the datetime library directly?
- 主要語言
- Python
- 星號
- 1.8k
- 分支
- 302
- 平均合併
- 4 小時 38 分鐘
- 30 天內合併 PR
- 6
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/typing 的其他 Issue
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 72/100
-
topic: typing spec
難度 2/5 1-3 小時 新手友好度 75/100
-
Update reference/generics.html with PEP695 type parameter syntax可能已有人在做 @BHUVANSH855 於 182 天前認領。 未關閉topic: documentation
難度 2/5 1-3 小時 新手友好度 76/100
-
topic: documentation
難度 2/5 1-3 小時 新手友好度 65/100
-
Finals and Literals: Spec contradiction可能已有人在做 @srittau 於 35 天前認領。 未關閉topic: conformance tests topic: typing spec
難度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
維護者通常 3 天內回覆
-
難度 2/5 1-3 小時 新手友好度 88/100
modelcontextprotocol/python-sdk#3648 ·
維護者通常 1 天內回覆
-
docs good first issue
難度 2/5 1-3 小時 新手友好度 78/100
VenetoStato/giorgio#6 ·
-
難度 2/5 1-3 小時 新手友好度 74/100
維護者通常 1 天內回覆
-
難度 1/5 1 小時以內 新手友好度 70/100
EclipseFdn/open-vsx.org#13831 ·
維護者通常 1 天內回覆