python / python/typing

Distinguish between datetime.datetime objects with a timezone and those without

Aperta
#1,962 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

topic: feature
Lingua principale
Python
Stelle
1.8k
Fork
302
Merge medio
23h
PR unite (30g)
8

Descrizione

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

  1. Is it possible to make this backwards compatible?
  2. Is this something others are interested in?
  3. Should this actually be solved in the datetime library directly?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Iniziare esaminando i tipi proposti DatetimeWithTimezone e DatetimeWithoutTimezone, nonché i costruttori e i metodi datetime elencati. Determinare se la distinzione può essere aggiunta in modo compatibile con Python typing e se rientra nella libreria datetime; il completamento richiederebbe di risolvere queste questioni di progettazione e definire un ambito concordato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
developer-experience
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.