Typed counterparts for built-in `datetime` module classes
Project description
datetypes
from datetime import date, time, datetime, timezone
from datetypes import Date, Time, DateTime, NaiveDateTime
t = Time(12, 0)
d = Date(2025, 2, 1)
dt = DateTime.combine(d, t)
# at type-checking, timezone info is carried via generics
assert_type(dt, DateTime[None])
# or, using an alias
assert_type(dt, NaiveDateTime)
# detect timezone incompatible comparisons
Time(12, 0, tzinfo=timezone.utc) < t # mypy error! cannot compare Time[timezone] with Time[None]
# DateTime doesn't subclass Date
def message(d: Date):
print(f"Hello, date is: {d}")
message(dt) # mypy error! DateTime is not compatible with Date
# at runtime, built-in types are used, zero overhead
type(d) is date
type(t) is time
type(dt) is datetime
An effort to provide type annotated and timezone-aware versions of the built-in
datetime module, only for static typing. At runtime the built-in classes from
datetime will always be used with zero overhead, including when evaluating
type annotations at runtime (e.g. Pydantic, FastAPI, etc.).
Greatly inspired by datetype, but
re-imagined to provide no runtime behaviour changes (the typed symbols are
simply aliases over the built-in types) + clean type information subclassing
types from datetime, so that typed counterparts can be used in places where
original types are expected (e.g. existing libraries) without having to manually
cast (as compared to datetype.concrete()).
Caveats
Since at runtime the typed versions are simply aliases to the built-in types,
generics don't work when used in runtime contexts! i.e. don't do
DateTime[ZoneInfo] in an expression that can be evaluated at runtime, it will
fail without being caught by your type linter (as the type stubs make this use
legit!). I currently don't have any non-invasive workaround for this, but I
expect this library to be mainly used for static type annotations.
Documentation
WIP, but for examples simply refer to tests/test_basic.py and other test files.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datetypes-0.1.0.tar.gz.
File metadata
- Download URL: datetypes-0.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.24.0 CPython/3.13.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fbd68f74ac61f6a0d1c60d052fd3c6ec14870433744eff9439af835f7bc21ec5
|
|
| MD5 |
696b619d1d101991376dac09e6a7300b
|
|
| BLAKE2b-256 |
a706850fb0d7d51379383332c35d8ff7141b689d701eb734767cbb10805a7775
|
File details
Details for the file datetypes-0.1.0-py3-none-any.whl.
File metadata
- Download URL: datetypes-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.24.0 CPython/3.13.3 Linux/6.8.0-1021-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4842aad5e67cb899652a3800fbd0f79bf4bf397d3b1a82bcdab74dc29f313461
|
|
| MD5 |
f2a88a767b8e5d8d4aa3332c37457b1a
|
|
| BLAKE2b-256 |
b3439729f71cfe6b33a534544cc080c29cbd17a925b2b208737c7d955d7dc78c
|