python / python/typeshed

Safer types for datetime

Open
#14,999 3 comments 1 reaction 0 assignees View on GitHub

@srittau is already working on this.

Since Apr 29, 2024.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

At Dropbox we are currently debating what to do with the deprecation of datetime.utcnow from cpython upstream.

This deprecation is problematic because adjusting a single datetime to timezone-aware breaks its interoperability with other datetimes across the codebase. We have several thousand calls to datetime.utcnow and I am sure we are not the only ones in this situation.

It seems to me that in order to use timezone aware datetimes there is a need for these two different categories of datetimes to be separated on the type level. Perhaps this could be a generic argument with two possible parameters. I thought about suggesting it to the mypy repository, but I think perhaps it makes more sense to discuss this at typeshed, but I could be wrong. On the other hand, making the type generic is also a large breaking change in itself, which may not be desirable.

Without such type level distinction, I don't see how any large codebase can properly remove utcnow - the other option we are considering is to mass replace every callsite and use replace(tzinfo=None) which just makes the codebase worse than before - and perhaps dangerous if anyone starts creating timezone aware datetimes once utcnow is gone.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with the datetime.utcnow deprecation discussion and linked pull request #11844. Clarify whether typeshed should represent naive and timezone-aware datetimes as distinct types, then define the affected stubs and type-checking behavior before implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.