diff --git a/python/pydantic_core/_pydantic_core.pyi b/python/pydantic_core/_pydantic_core.pyi index 07b995761..875c99da8 100644 --- a/python/pydantic_core/_pydantic_core.pyi +++ b/python/pydantic_core/_pydantic_core.pyi @@ -993,7 +993,14 @@ def list_all_errors() -> list[ErrorTypeInfo]: class TzInfo(datetime.tzinfo): """An `pydantic-core` implementation of the abstract [`datetime.tzinfo`][] class.""" - # def __new__(cls, seconds: float) -> Self: ... + def __init__(self, seconds: float = 0.0) -> None: + """Initializes the `TzInfo`. + + Arguments: + seconds: The offset from UTC in seconds. Defaults to 0.0 (UTC). + """ + + def __new__(cls, seconds: float = 0.0) -> Self: ... # Docstrings for attributes sourced from the abstract base class, [`datetime.tzinfo`](https://docs.python.org/3/library/datetime.html#datetime.tzinfo). diff --git a/src/input/datetime.rs b/src/input/datetime.rs index 590d859b3..93139626a 100644 --- a/src/input/datetime.rs +++ b/src/input/datetime.rs @@ -643,6 +643,7 @@ pub struct TzInfo { #[pymethods] impl TzInfo { #[new] + #[pyo3(signature = (seconds = 0.0))] fn py_new(seconds: f32) -> PyResult { Self::try_from(seconds.trunc() as i32) } diff --git a/tests/test_tzinfo.py b/tests/test_tzinfo.py index 4d4279d22..170055690 100644 --- a/tests/test_tzinfo.py +++ b/tests/test_tzinfo.py @@ -213,6 +213,22 @@ def test_offset_boundaries(self): with self.assertRaises(ValueError): TzInfo(delta.total_seconds()) + def test_no_args_constructor(self): + # Test that TzInfo can be constructed without arguments + tz = TzInfo() + self.assertEqual(tz.utcoffset(None), timedelta(0)) + self.assertEqual(str(tz), 'UTC') + + def test_pickle(self): + # Test that TzInfo can be pickled and unpickled + for tz in self.ACDT, self.EST, self.UTC: + for pickler, unpickler, proto in pickle_choices: + with self.subTest(tz=tz, proto=proto): + pickled = pickler.dumps(tz, proto) + unpickled = unpickler.loads(pickled) + self.assertEqual(tz, unpickled) + self.assertEqual(tz.utcoffset(None), unpickled.utcoffset(None)) + def test_tzinfo_could_be_reused(): class Model: