Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/pendulum/tz/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from __future__ import annotations

import datetime as _datetime
import io
import zoneinfo

from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
from typing import Any
from typing import TypeVar
from typing import cast

Expand All @@ -16,6 +18,9 @@


if TYPE_CHECKING:
from collections.abc import Callable
from zoneinfo._common import _IOBytes

from typing_extensions import Self

POST_TRANSITION = "post"
Expand Down Expand Up @@ -60,12 +65,39 @@ class Timezone(zoneinfo.ZoneInfo, PendulumTimezone):
>>> tz = Timezone('Europe/Paris')
"""

_file_bytes: bytes | None = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is written to, but never used...?

I'm not seeing how this does anything?

@juneja-varun juneja-varun Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the flag and payload __reduce__ needs: if set, it returns _from_pickled_file(data, key) instead of the default, rebuilding the instance from those bytes via from_file() again. Without it, pickling can't reconstruct a file-based instance and crashes - that's the bug this fixes.


def __new__(cls, key: str) -> Self:
try:
return super().__new__(cls, key)
except zoneinfo.ZoneInfoNotFoundError:
raise InvalidTimezone(key)

@classmethod
def from_file(cls, fobj: _IOBytes, /, key: str | None = None) -> Self:
# zoneinfo.ZoneInfo can't pickle this, so keep the bytes to rebuild it.
data = fobj.read(-1)
instance = cast("Self", super().from_file(io.BytesIO(data), key=key))
instance._file_bytes = data

return instance

def __reduce__(
self,
) -> (
tuple[Callable[[bytes, str | None], Self], tuple[bytes, str | None]]
| str
| tuple[Any, ...]
):
if self._file_bytes is None:
return super().__reduce__()

return self.__class__._from_pickled_file, (self._file_bytes, self.key)

@classmethod
def _from_pickled_file(cls, data: bytes, key: str | None) -> Self:
return cls.from_file(io.BytesIO(data), key=key)

def __eq__(self, other: object) -> bool:
return isinstance(other, Timezone) and self.key == other.key

Expand Down
27 changes: 27 additions & 0 deletions tests/tz/test_timezone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import pickle
import zoneinfo

from datetime import datetime
Expand Down Expand Up @@ -476,3 +477,29 @@ def test_repr():
tz = timezone("Europe/Paris")

assert repr(tz) == "Timezone('Europe/Paris')"


def _paris_tzif_bytes() -> bytes:
# tzdata works the same on every platform pendulum supports, unlike /usr/share/zoneinfo.
from importlib import resources

return resources.files("tzdata.zoneinfo").joinpath("Europe", "Paris").read_bytes()


def test_from_file_without_a_key_can_be_pickled():
# get_local_timezone()'s last-resort fallback builds a Timezone this same way, with no key.
from io import BytesIO

import pendulum.tz.timezone as timezone_module

tz = timezone_module.Timezone.from_file(BytesIO(_paris_tzif_bytes()))
Comment thread
ashb marked this conversation as resolved.

assert tz.key is None

unpickled = pickle.loads(pickle.dumps(tz))

assert unpickled.key is None
dt = datetime(2024, 7, 1, 12, tzinfo=unpickled)
assert dt.utcoffset() == timedelta(hours=2) # CEST, matches Europe/Paris in July
dt = datetime(2024, 1, 1, 12, tzinfo=unpickled)
assert dt.utcoffset() == timedelta(hours=1) # CET, matches Europe/Paris in January
Loading