Skip to content

Commit 68a1e7b

Browse files
authored
feat: make TzInfo instantiable without any arguments (#1777)
1 parent 1b3815c commit 68a1e7b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,14 @@ def list_all_errors() -> list[ErrorTypeInfo]:
993993
class TzInfo(datetime.tzinfo):
994994
"""An `pydantic-core` implementation of the abstract [`datetime.tzinfo`][] class."""
995995

996-
# def __new__(cls, seconds: float) -> Self: ...
996+
def __init__(self, seconds: float = 0.0) -> None:
997+
"""Initializes the `TzInfo`.
998+
999+
Arguments:
1000+
seconds: The offset from UTC in seconds. Defaults to 0.0 (UTC).
1001+
"""
1002+
1003+
def __new__(cls, seconds: float = 0.0) -> Self: ...
9971004

9981005
# Docstrings for attributes sourced from the abstract base class, [`datetime.tzinfo`](https://docs.python.org/3/library/datetime.html#datetime.tzinfo).
9991006

src/input/datetime.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ pub struct TzInfo {
643643
#[pymethods]
644644
impl TzInfo {
645645
#[new]
646+
#[pyo3(signature = (seconds = 0.0))]
646647
fn py_new(seconds: f32) -> PyResult<Self> {
647648
Self::try_from(seconds.trunc() as i32)
648649
}

tests/test_tzinfo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,22 @@ def test_offset_boundaries(self):
213213
with self.assertRaises(ValueError):
214214
TzInfo(delta.total_seconds())
215215

216+
def test_no_args_constructor(self):
217+
# Test that TzInfo can be constructed without arguments
218+
tz = TzInfo()
219+
self.assertEqual(tz.utcoffset(None), timedelta(0))
220+
self.assertEqual(str(tz), 'UTC')
221+
222+
def test_pickle(self):
223+
# Test that TzInfo can be pickled and unpickled
224+
for tz in self.ACDT, self.EST, self.UTC:
225+
for pickler, unpickler, proto in pickle_choices:
226+
with self.subTest(tz=tz, proto=proto):
227+
pickled = pickler.dumps(tz, proto)
228+
unpickled = unpickler.loads(pickled)
229+
self.assertEqual(tz, unpickled)
230+
self.assertEqual(tz.utcoffset(None), unpickled.utcoffset(None))
231+
216232

217233
def test_tzinfo_could_be_reused():
218234
class Model:

0 commit comments

Comments
 (0)