whenever-sqlalchemy 0.1.0b2__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- whenever_sqlalchemy-0.2.0/CHANGELOG.md +50 -0
- whenever_sqlalchemy-0.2.0/PKG-INFO +201 -0
- whenever_sqlalchemy-0.2.0/README.md +169 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/pyproject.toml +24 -23
- whenever_sqlalchemy-0.2.0/src/whenever_sqlalchemy/__init__.py +183 -0
- whenever_sqlalchemy-0.2.0/src/whenever_sqlalchemy.egg-info/PKG-INFO +201 -0
- whenever_sqlalchemy-0.2.0/src/whenever_sqlalchemy.egg-info/requires.txt +2 -0
- whenever_sqlalchemy-0.2.0/tests/test_whenever_sqlalchemy.py +346 -0
- whenever_sqlalchemy-0.1.0b2/CHANGELOG.md +0 -11
- whenever_sqlalchemy-0.1.0b2/PKG-INFO +0 -122
- whenever_sqlalchemy-0.1.0b2/README.md +0 -81
- whenever_sqlalchemy-0.1.0b2/src/whenever_sqlalchemy/__init__.py +0 -126
- whenever_sqlalchemy-0.1.0b2/src/whenever_sqlalchemy.egg-info/PKG-INFO +0 -122
- whenever_sqlalchemy-0.1.0b2/src/whenever_sqlalchemy.egg-info/requires.txt +0 -12
- whenever_sqlalchemy-0.1.0b2/tests/test_whenever_sqlalchemy.py +0 -356
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/LICENSE +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/MANIFEST.in +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/setup.cfg +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/src/whenever_sqlalchemy/py.typed +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/src/whenever_sqlalchemy.egg-info/SOURCES.txt +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/src/whenever_sqlalchemy.egg-info/dependency_links.txt +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/src/whenever_sqlalchemy.egg-info/top_level.txt +0 -0
- {whenever_sqlalchemy-0.1.0b2 → whenever_sqlalchemy-0.2.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.0 (2026-11-29)
|
|
4
|
+
|
|
5
|
+
**Breaking changes**
|
|
6
|
+
|
|
7
|
+
- Column types accept only their own `whenever` type and raise `TypeError`
|
|
8
|
+
for other values. Before, a `ZonedDateTime` or `OffsetDateTime` in an
|
|
9
|
+
`InstantType` column was stored without a `TypeError`. On databases that
|
|
10
|
+
store the column without a time zone, such as SQLite, the stored instant
|
|
11
|
+
was off by the UTC offset.
|
|
12
|
+
- `type_annotation_map` is read-only. Merge it into your own map with
|
|
13
|
+
`{**type_annotation_map, ...}` instead of changing it in place.
|
|
14
|
+
- `from whenever_sqlalchemy import *` imports only the column types and
|
|
15
|
+
`type_annotation_map`.
|
|
16
|
+
- `InstantType` raises `ValueError` when it reads a datetime without a time
|
|
17
|
+
zone on a database that stores time zones, such as PostgreSQL. Before, it
|
|
18
|
+
read the value as UTC, which is wrong for a `TIMESTAMP WITHOUT TIME ZONE`
|
|
19
|
+
column whose values aren't UTC. SQLite, MySQL, MariaDB, and Oracle store
|
|
20
|
+
the column without a time zone, so on those databases values without a time
|
|
21
|
+
zone are still read as UTC.
|
|
22
|
+
- Requires whenever 0.11 or later, SQLAlchemy 2.0 or later, and Python 3.11
|
|
23
|
+
or later.
|
|
24
|
+
- The `dev` extra is removed. Development dependencies are in the `dev`
|
|
25
|
+
dependency group: `pip install -e . --group dev`.
|
|
26
|
+
|
|
27
|
+
**Improvements**
|
|
28
|
+
|
|
29
|
+
- The column types are generic `TypeDecorator`s, so type checkers infer the
|
|
30
|
+
`whenever` type in expressions such as `cast(x, InstantType())`.
|
|
31
|
+
- Supports Python 3.15.
|
|
32
|
+
- The README shows how to store a `ZonedDateTime` so that you can decide
|
|
33
|
+
later how to handle time zone rule changes.
|
|
34
|
+
|
|
35
|
+
**Fixes**
|
|
36
|
+
|
|
37
|
+
- `python_type` returns the `whenever` class, for example `Instant`.
|
|
38
|
+
- The build requires setuptools 77 or later, which supports the `license`
|
|
39
|
+
field.
|
|
40
|
+
- The changelog link in the package metadata points to this repository.
|
|
41
|
+
|
|
42
|
+
## 0.1.0 (2026-04-05)
|
|
43
|
+
|
|
44
|
+
Initial release.
|
|
45
|
+
|
|
46
|
+
- `InstantType` — maps `Instant` to ``TIMESTAMP WITH TIME ZONE``
|
|
47
|
+
- `PlainDateTimeType` — maps `PlainDateTime` to ``TIMESTAMP``
|
|
48
|
+
- `DateType` — maps `Date` to ``DATE``
|
|
49
|
+
- `TimeType` — maps `Time` to ``TIME``
|
|
50
|
+
- `type_annotation_map` — ready-to-use dict for SQLAlchemy 2.x `Mapped[]` annotations
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: whenever-sqlalchemy
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: SQLAlchemy column types for the whenever datetime library
|
|
5
|
+
Author-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
6
|
+
Maintainer-email: Arie Bovenberg <a.c.bovenberg@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Repository, https://github.com/ariebovenberg/whenever-sqlalchemy
|
|
9
|
+
Project-URL: Issues, https://github.com/ariebovenberg/whenever-sqlalchemy/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/ariebovenberg/whenever-sqlalchemy/blob/main/CHANGELOG.md
|
|
11
|
+
Keywords: whenever,sqlalchemy,datetime,date,time,orm
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
21
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: PyPy
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Classifier: Topic :: Database
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Requires-Python: >=3.11
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: whenever>=0.11.0
|
|
30
|
+
Requires-Dist: sqlalchemy>=2.0
|
|
31
|
+
Dynamic: license-file
|
|
32
|
+
|
|
33
|
+
# whenever-sqlalchemy
|
|
34
|
+
|
|
35
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install whenever-sqlalchemy
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Type mapping
|
|
44
|
+
|
|
45
|
+
| whenever type | SQL type |
|
|
46
|
+
|------------------|-----------------------------|
|
|
47
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
48
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
49
|
+
| `Date` | `DATE` |
|
|
50
|
+
| `Time` | `TIME` |
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### ORM: `Mapped[]` annotations (recommended)
|
|
55
|
+
|
|
56
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
57
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
61
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
62
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
63
|
+
|
|
64
|
+
class Base(DeclarativeBase):
|
|
65
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
66
|
+
|
|
67
|
+
class Event(Base):
|
|
68
|
+
__tablename__ = "events"
|
|
69
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
70
|
+
timestamp: Mapped[Instant]
|
|
71
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
72
|
+
date: Mapped[Date | None]
|
|
73
|
+
time: Mapped[Time | None]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Core: explicit column types
|
|
77
|
+
|
|
78
|
+
Pass the column types to `Column()`, or to `mapped_column()` in the ORM:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from sqlalchemy import Column, Integer, MetaData, Table
|
|
82
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
83
|
+
|
|
84
|
+
events = Table(
|
|
85
|
+
"events",
|
|
86
|
+
MetaData(),
|
|
87
|
+
Column("id", Integer, primary_key=True),
|
|
88
|
+
Column("timestamp", InstantType, nullable=False),
|
|
89
|
+
Column("scheduled", PlainDateTimeType),
|
|
90
|
+
Column("date", DateType),
|
|
91
|
+
Column("time", TimeType),
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Store a `ZonedDateTime`
|
|
96
|
+
|
|
97
|
+
Most databases have no column type that keeps a time zone ID, and most don't
|
|
98
|
+
keep a UTC offset either: PostgreSQL's `TIMESTAMP WITH TIME ZONE` converts
|
|
99
|
+
values to UTC and discards the offset. Store the local date and time, the
|
|
100
|
+
offset, and the time zone ID in three columns:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
from whenever import PlainDateTime, TimeDelta, ZonedDateTime
|
|
104
|
+
|
|
105
|
+
class Meeting(Base):
|
|
106
|
+
__tablename__ = "meetings"
|
|
107
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
108
|
+
start_local: Mapped[PlainDateTime]
|
|
109
|
+
start_offset: Mapped[int] # seconds east of UTC
|
|
110
|
+
start_tz: Mapped[str]
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def start(self) -> ZonedDateTime:
|
|
114
|
+
offset = TimeDelta(seconds=self.start_offset)
|
|
115
|
+
return self.start_local.assume_fixed_offset(offset).assume_tz(
|
|
116
|
+
self.start_tz,
|
|
117
|
+
offset_mismatch="keep_local",
|
|
118
|
+
disambiguation="compatible",
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
@start.setter
|
|
122
|
+
def start(self, value: ZonedDateTime) -> None:
|
|
123
|
+
self.start_local = value.to_plain()
|
|
124
|
+
self.start_offset = int(value.offset.total("seconds"))
|
|
125
|
+
self.start_tz = value.tz_id
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
If the time zone's rules change between writing and reading a row, the stored
|
|
129
|
+
offset no longer matches the time zone. `offset_mismatch` decides what
|
|
130
|
+
happens then:
|
|
131
|
+
|
|
132
|
+
- `"keep_local"` keeps the local time and moves the instant. Use it for
|
|
133
|
+
events planned in local time, such as a meeting at 9:00.
|
|
134
|
+
- `"keep_instant"` keeps the instant and moves the local time. Use it for
|
|
135
|
+
events that already happened, such as a log entry.
|
|
136
|
+
- `"raise"` raises `InvalidOffsetError`.
|
|
137
|
+
|
|
138
|
+
With `"keep_local"`, the local time can fall in an hour that the new rules
|
|
139
|
+
repeat or skip. `disambiguation` then picks the instant.
|
|
140
|
+
|
|
141
|
+
The row keeps all three values, so you can change this choice later without
|
|
142
|
+
migrating data. To filter or sort rows by instant in SQL, add an
|
|
143
|
+
`InstantType` column as well.
|
|
144
|
+
|
|
145
|
+
## Notes
|
|
146
|
+
|
|
147
|
+
**Strict types** — each column type accepts only its own `whenever` type and
|
|
148
|
+
raises `TypeError` for anything else. To store a `ZonedDateTime` or
|
|
149
|
+
`OffsetDateTime` in an `InstantType` column, convert it with `.to_instant()`
|
|
150
|
+
first.
|
|
151
|
+
|
|
152
|
+
**Precision** — `whenever` types have nanosecond precision. Values reach the
|
|
153
|
+
database driver truncated to microseconds, matching `to_stdlib()`. The column
|
|
154
|
+
type that SQLAlchemy creates on your database can store less: check your
|
|
155
|
+
database's documentation.
|
|
156
|
+
|
|
157
|
+
An object that you add to a session keeps its nanoseconds until SQLAlchemy
|
|
158
|
+
loads it again from the database. To make both hold the same value, round it
|
|
159
|
+
before you store it, for example with
|
|
160
|
+
`instant.round("microsecond", mode="floor")`.
|
|
161
|
+
|
|
162
|
+
To use a more precise column type, subclass and override `impl`:
|
|
163
|
+
|
|
164
|
+
```python
|
|
165
|
+
import sqlalchemy as sa
|
|
166
|
+
from sqlalchemy.dialects import mysql
|
|
167
|
+
from whenever_sqlalchemy import InstantType
|
|
168
|
+
|
|
169
|
+
class PreciseInstantType(InstantType):
|
|
170
|
+
impl = sa.DateTime(timezone=True).with_variant(
|
|
171
|
+
mysql.DATETIME(fsp=6), "mysql"
|
|
172
|
+
)
|
|
173
|
+
cache_ok = True
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Don't call `InstantType().with_variant(...)`: on the variant's database,
|
|
177
|
+
SQLAlchemy uses the variant type instead of `InstantType`, so values skip the
|
|
178
|
+
conversion to and from `whenever` types.
|
|
179
|
+
|
|
180
|
+
**Time zones** — on PostgreSQL, `InstantType` creates a
|
|
181
|
+
`TIMESTAMP WITH TIME ZONE` column. On SQL Server, it creates `DATETIMEOFFSET`.
|
|
182
|
+
SQLite, MySQL, MariaDB, and Oracle store the column without a time zone:
|
|
183
|
+
`InstantType` writes UTC and reads values without a time zone as UTC.
|
|
184
|
+
|
|
185
|
+
On any other database, `InstantType` raises `ValueError` when it reads a
|
|
186
|
+
value without a time zone. This usually means that the column has no time
|
|
187
|
+
zone, for example a `TIMESTAMP WITHOUT TIME ZONE` column in an existing
|
|
188
|
+
PostgreSQL schema. Its values aren't necessarily UTC, so `InstantType`
|
|
189
|
+
doesn't guess.
|
|
190
|
+
|
|
191
|
+
**`server_default`** — the database sets the value and SQLAlchemy converts it
|
|
192
|
+
to the `whenever` type when it reads the row. With `InstantType`, the value
|
|
193
|
+
must be in UTC on databases that store the column without a time zone. MySQL's
|
|
194
|
+
`NOW()` returns the session time zone's local time, so it gives wrong
|
|
195
|
+
instants unless the session time zone is UTC.
|
|
196
|
+
|
|
197
|
+
## Compatibility
|
|
198
|
+
|
|
199
|
+
- Python 3.11+
|
|
200
|
+
- whenever 0.11+
|
|
201
|
+
- SQLAlchemy 2.0+
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# whenever-sqlalchemy
|
|
2
|
+
|
|
3
|
+
SQLAlchemy column types for the [whenever](https://github.com/ariebovenberg/whenever) datetime library.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install whenever-sqlalchemy
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Type mapping
|
|
12
|
+
|
|
13
|
+
| whenever type | SQL type |
|
|
14
|
+
|------------------|-----------------------------|
|
|
15
|
+
| `Instant` | `TIMESTAMP WITH TIME ZONE` |
|
|
16
|
+
| `PlainDateTime` | `TIMESTAMP` (no timezone) |
|
|
17
|
+
| `Date` | `DATE` |
|
|
18
|
+
| `Time` | `TIME` |
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### ORM: `Mapped[]` annotations (recommended)
|
|
23
|
+
|
|
24
|
+
Register `type_annotation_map` on your `DeclarativeBase` once, then use
|
|
25
|
+
`Mapped[Instant]` exactly like you would use `Mapped[datetime]`:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
29
|
+
from whenever import Date, Instant, PlainDateTime, Time
|
|
30
|
+
from whenever_sqlalchemy import type_annotation_map
|
|
31
|
+
|
|
32
|
+
class Base(DeclarativeBase):
|
|
33
|
+
type_annotation_map = type_annotation_map # merge with your own if needed
|
|
34
|
+
|
|
35
|
+
class Event(Base):
|
|
36
|
+
__tablename__ = "events"
|
|
37
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
38
|
+
timestamp: Mapped[Instant]
|
|
39
|
+
scheduled: Mapped[PlainDateTime | None]
|
|
40
|
+
date: Mapped[Date | None]
|
|
41
|
+
time: Mapped[Time | None]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Core: explicit column types
|
|
45
|
+
|
|
46
|
+
Pass the column types to `Column()`, or to `mapped_column()` in the ORM:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from sqlalchemy import Column, Integer, MetaData, Table
|
|
50
|
+
from whenever_sqlalchemy import DateType, InstantType, PlainDateTimeType, TimeType
|
|
51
|
+
|
|
52
|
+
events = Table(
|
|
53
|
+
"events",
|
|
54
|
+
MetaData(),
|
|
55
|
+
Column("id", Integer, primary_key=True),
|
|
56
|
+
Column("timestamp", InstantType, nullable=False),
|
|
57
|
+
Column("scheduled", PlainDateTimeType),
|
|
58
|
+
Column("date", DateType),
|
|
59
|
+
Column("time", TimeType),
|
|
60
|
+
)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Store a `ZonedDateTime`
|
|
64
|
+
|
|
65
|
+
Most databases have no column type that keeps a time zone ID, and most don't
|
|
66
|
+
keep a UTC offset either: PostgreSQL's `TIMESTAMP WITH TIME ZONE` converts
|
|
67
|
+
values to UTC and discards the offset. Store the local date and time, the
|
|
68
|
+
offset, and the time zone ID in three columns:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from whenever import PlainDateTime, TimeDelta, ZonedDateTime
|
|
72
|
+
|
|
73
|
+
class Meeting(Base):
|
|
74
|
+
__tablename__ = "meetings"
|
|
75
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
76
|
+
start_local: Mapped[PlainDateTime]
|
|
77
|
+
start_offset: Mapped[int] # seconds east of UTC
|
|
78
|
+
start_tz: Mapped[str]
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def start(self) -> ZonedDateTime:
|
|
82
|
+
offset = TimeDelta(seconds=self.start_offset)
|
|
83
|
+
return self.start_local.assume_fixed_offset(offset).assume_tz(
|
|
84
|
+
self.start_tz,
|
|
85
|
+
offset_mismatch="keep_local",
|
|
86
|
+
disambiguation="compatible",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
@start.setter
|
|
90
|
+
def start(self, value: ZonedDateTime) -> None:
|
|
91
|
+
self.start_local = value.to_plain()
|
|
92
|
+
self.start_offset = int(value.offset.total("seconds"))
|
|
93
|
+
self.start_tz = value.tz_id
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
If the time zone's rules change between writing and reading a row, the stored
|
|
97
|
+
offset no longer matches the time zone. `offset_mismatch` decides what
|
|
98
|
+
happens then:
|
|
99
|
+
|
|
100
|
+
- `"keep_local"` keeps the local time and moves the instant. Use it for
|
|
101
|
+
events planned in local time, such as a meeting at 9:00.
|
|
102
|
+
- `"keep_instant"` keeps the instant and moves the local time. Use it for
|
|
103
|
+
events that already happened, such as a log entry.
|
|
104
|
+
- `"raise"` raises `InvalidOffsetError`.
|
|
105
|
+
|
|
106
|
+
With `"keep_local"`, the local time can fall in an hour that the new rules
|
|
107
|
+
repeat or skip. `disambiguation` then picks the instant.
|
|
108
|
+
|
|
109
|
+
The row keeps all three values, so you can change this choice later without
|
|
110
|
+
migrating data. To filter or sort rows by instant in SQL, add an
|
|
111
|
+
`InstantType` column as well.
|
|
112
|
+
|
|
113
|
+
## Notes
|
|
114
|
+
|
|
115
|
+
**Strict types** — each column type accepts only its own `whenever` type and
|
|
116
|
+
raises `TypeError` for anything else. To store a `ZonedDateTime` or
|
|
117
|
+
`OffsetDateTime` in an `InstantType` column, convert it with `.to_instant()`
|
|
118
|
+
first.
|
|
119
|
+
|
|
120
|
+
**Precision** — `whenever` types have nanosecond precision. Values reach the
|
|
121
|
+
database driver truncated to microseconds, matching `to_stdlib()`. The column
|
|
122
|
+
type that SQLAlchemy creates on your database can store less: check your
|
|
123
|
+
database's documentation.
|
|
124
|
+
|
|
125
|
+
An object that you add to a session keeps its nanoseconds until SQLAlchemy
|
|
126
|
+
loads it again from the database. To make both hold the same value, round it
|
|
127
|
+
before you store it, for example with
|
|
128
|
+
`instant.round("microsecond", mode="floor")`.
|
|
129
|
+
|
|
130
|
+
To use a more precise column type, subclass and override `impl`:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
import sqlalchemy as sa
|
|
134
|
+
from sqlalchemy.dialects import mysql
|
|
135
|
+
from whenever_sqlalchemy import InstantType
|
|
136
|
+
|
|
137
|
+
class PreciseInstantType(InstantType):
|
|
138
|
+
impl = sa.DateTime(timezone=True).with_variant(
|
|
139
|
+
mysql.DATETIME(fsp=6), "mysql"
|
|
140
|
+
)
|
|
141
|
+
cache_ok = True
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Don't call `InstantType().with_variant(...)`: on the variant's database,
|
|
145
|
+
SQLAlchemy uses the variant type instead of `InstantType`, so values skip the
|
|
146
|
+
conversion to and from `whenever` types.
|
|
147
|
+
|
|
148
|
+
**Time zones** — on PostgreSQL, `InstantType` creates a
|
|
149
|
+
`TIMESTAMP WITH TIME ZONE` column. On SQL Server, it creates `DATETIMEOFFSET`.
|
|
150
|
+
SQLite, MySQL, MariaDB, and Oracle store the column without a time zone:
|
|
151
|
+
`InstantType` writes UTC and reads values without a time zone as UTC.
|
|
152
|
+
|
|
153
|
+
On any other database, `InstantType` raises `ValueError` when it reads a
|
|
154
|
+
value without a time zone. This usually means that the column has no time
|
|
155
|
+
zone, for example a `TIMESTAMP WITHOUT TIME ZONE` column in an existing
|
|
156
|
+
PostgreSQL schema. Its values aren't necessarily UTC, so `InstantType`
|
|
157
|
+
doesn't guess.
|
|
158
|
+
|
|
159
|
+
**`server_default`** — the database sets the value and SQLAlchemy converts it
|
|
160
|
+
to the `whenever` type when it reads the row. With `InstantType`, the value
|
|
161
|
+
must be in UTC on databases that store the column without a time zone. MySQL's
|
|
162
|
+
`NOW()` returns the session time zone's local time, so it gives wrong
|
|
163
|
+
instants unless the session time zone is UTC.
|
|
164
|
+
|
|
165
|
+
## Compatibility
|
|
166
|
+
|
|
167
|
+
- Python 3.11+
|
|
168
|
+
- whenever 0.11+
|
|
169
|
+
- SQLAlchemy 2.0+
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "whenever-sqlalchemy"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.2.0"
|
|
4
4
|
authors = [
|
|
5
5
|
{name = "Arie Bovenberg", email = "a.c.bovenberg@gmail.com"},
|
|
6
6
|
]
|
|
@@ -10,17 +10,17 @@ maintainers = [
|
|
|
10
10
|
readme = "README.md"
|
|
11
11
|
license = "MIT"
|
|
12
12
|
description = "SQLAlchemy column types for the whenever datetime library"
|
|
13
|
-
requires-python = ">=3.
|
|
13
|
+
requires-python = ">=3.11"
|
|
14
14
|
classifiers = [
|
|
15
15
|
"Development Status :: 4 - Beta",
|
|
16
16
|
"Intended Audience :: Developers",
|
|
17
17
|
"Operating System :: OS Independent",
|
|
18
18
|
"Programming Language :: Python :: 3",
|
|
19
|
-
"Programming Language :: Python :: 3.10",
|
|
20
19
|
"Programming Language :: Python :: 3.11",
|
|
21
20
|
"Programming Language :: Python :: 3.12",
|
|
22
21
|
"Programming Language :: Python :: 3.13",
|
|
23
22
|
"Programming Language :: Python :: 3.14",
|
|
23
|
+
"Programming Language :: Python :: 3.15",
|
|
24
24
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
25
25
|
"Programming Language :: Python :: Implementation :: PyPy",
|
|
26
26
|
"Typing :: Typed",
|
|
@@ -28,43 +28,44 @@ classifiers = [
|
|
|
28
28
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
29
29
|
]
|
|
30
30
|
dependencies = [
|
|
31
|
-
"whenever>=0.
|
|
32
|
-
"sqlalchemy>=
|
|
31
|
+
"whenever>=0.11.0",
|
|
32
|
+
"sqlalchemy>=2.0",
|
|
33
33
|
]
|
|
34
34
|
keywords = ["whenever", "sqlalchemy", "datetime", "date", "time", "orm"]
|
|
35
35
|
|
|
36
36
|
[project.urls]
|
|
37
37
|
Repository = "https://github.com/ariebovenberg/whenever-sqlalchemy"
|
|
38
38
|
Issues = "https://github.com/ariebovenberg/whenever-sqlalchemy/issues"
|
|
39
|
-
Changelog = "https://github.com/ariebovenberg/whenever/blob/main/
|
|
39
|
+
Changelog = "https://github.com/ariebovenberg/whenever-sqlalchemy/blob/main/CHANGELOG.md"
|
|
40
40
|
|
|
41
|
-
[
|
|
41
|
+
[dependency-groups]
|
|
42
42
|
dev = [
|
|
43
|
-
"pytest>=
|
|
44
|
-
"pytest-cov>=7,<8",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"twine>=6.2.0",
|
|
43
|
+
"pytest>=9.2,<10",
|
|
44
|
+
"pytest-cov>=7.1,<8",
|
|
45
|
+
"psycopg[binary]>=3.3,<4",
|
|
46
|
+
"ruff>=0.17.6,<0.18",
|
|
47
|
+
"mypy>=2.4,<2.5",
|
|
48
|
+
"build>=1.6",
|
|
49
|
+
"twine>=7",
|
|
51
50
|
]
|
|
52
51
|
|
|
53
52
|
[tool.setuptools.packages.find]
|
|
54
53
|
where = ["src"]
|
|
55
54
|
|
|
56
55
|
[build-system]
|
|
57
|
-
requires = ["setuptools>=
|
|
56
|
+
requires = ["setuptools>=77"]
|
|
58
57
|
build-backend = "setuptools.build_meta"
|
|
59
58
|
|
|
60
|
-
[tool.
|
|
59
|
+
[tool.ruff]
|
|
61
60
|
line-length = 79
|
|
62
|
-
|
|
61
|
+
src = ["src"]
|
|
63
62
|
|
|
64
|
-
[tool.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
63
|
+
[tool.ruff.lint]
|
|
64
|
+
# pycodestyle errors and warnings, pyflakes, and isort
|
|
65
|
+
select = ["E", "W", "F", "I"]
|
|
66
|
+
|
|
67
|
+
[tool.ruff.lint.isort]
|
|
68
|
+
combine-as-imports = true
|
|
68
69
|
|
|
69
70
|
[tool.mypy]
|
|
70
71
|
warn_unused_ignores = true
|
|
@@ -73,5 +74,5 @@ disallow_untyped_defs = true
|
|
|
73
74
|
|
|
74
75
|
[[tool.mypy.overrides]]
|
|
75
76
|
module = ["tests.*"]
|
|
76
|
-
disable_error_code = ["no-untyped-def"]
|
|
77
|
+
disable_error_code = ["no-untyped-def", "no-untyped-call"]
|
|
77
78
|
check_untyped_defs = false
|