python / python/typing

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

Ouverte
#1,962 1 commentaire 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

topic: feature
Langage dominant
Python
Étoiles
1.8k
Forks
302
Merge moyen
23 h
PR mergées (30 j)
8

Description

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?

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par examiner les types proposés DatetimeWithTimezone et DatetimeWithoutTimezone, ainsi que les constructeurs et méthodes datetime listés. Déterminez si cette distinction peut être ajoutée de manière compatible avec Python typing et si elle relève de la bibliothèque datetime ; la finalisation nécessiterait de trancher ces questions de conception et de définir un périmètre convenu.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
developer-experience
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.