Distinguish between datetime.datetime objects with a timezone and those without
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 25/100
- Tipo de issue
- Funcionalidade
- Clareza
- Precisa de esclarecimento
- Status de atividade
- Estagnada
- Stack de tecnologia
- python
- Domínio
- developer-experience
Direção de pesquisa
Comece revisando os tipos propostos DatetimeWithTimezone e DatetimeWithoutTimezone, bem como os construtores e métodos datetime listados. Determine se a distinção pode ser adicionada de forma compatível ao Python typing e se ela pertence à biblioteca datetime; a conclusão exigiria resolver essas questões de design e definir um escopo acordado.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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?
- Linguagem predominante
- Python
- Estrelas
- 1.8k
- Forks
- 302
- Merge médio
- 23h
- PRs com merge (30d)
- 8
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/typing
-
topic: typing spec
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
topic: typing spec
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
-
topic: conformance tests topic: typing spec
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 72/100
Todas as issues de python/typing
Issues semelhantes
-
agent-ready documentation needs-triage
Dificuldade 1/5 1-3 horas Facilidade para iniciantes 88/100
-
documentation
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 91/100
-
workflow-status page template still says reusable workflows are "triggered only by workflow_call:" Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
-
instance instance add
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 72/100
searxng/searx-instances#939 · 1 comentário ·
-
area-deployment area-integrations triage:bot-seen
Dificuldade 2/5 Meio dia Facilidade para iniciantes 86/100