Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please drop the changelog entry - we don't add do "unreleased" in the changelog.

Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [Unreleased]

### Changed
- Fixed `pendulum.instance()` raising `AttributeError` for datetimes with a `pytz.FixedOffset` timezone [#981](https://github.com/python-pendulum/pendulum/pull/981)

## [3.2.0] - 2026-01-30

### Added
Expand Down
12 changes: 11 additions & 1 deletion src/pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@ def _safe_timezone(
obj = obj.key
# pytz
elif hasattr(obj, "localize"):
obj = obj.zone # type: ignore[attr-defined]
# Named pytz zones expose a ``zone`` name, but fixed-offset zones
# (``pytz.FixedOffset``) have ``zone = None``; use their offset.
if obj.zone is not None: # type: ignore[attr-defined]
obj = obj.zone # type: ignore[attr-defined]
else:
offset = obj.utcoffset(dt)

if offset is None:
offset = _datetime.timedelta(0)

obj = int(offset.total_seconds())
elif obj.tzname(None) == "UTC":
return UTC
else:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from datetime import date
from datetime import datetime
from datetime import time
from datetime import timedelta

import pytest

from dateutil import tz

Expand Down Expand Up @@ -36,6 +39,18 @@ def test_instance_with_aware_datetime_any_tzinfo() -> None:
assert now.timezone_name == "+02:00"


def test_instance_with_pytz_fixed_offset() -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If I understand it correctly, without pytz, this test will never execute. We are not installing pytz in our tests/pipelines. Hence, this test is pointless.

# ``pytz.FixedOffset`` has a ``localize`` method but no ``zone`` name,
# which used to raise ``AttributeError`` in ``_safe_timezone`` (#807).
pytz = pytest.importorskip("pytz")

dt = pendulum.instance(datetime(2021, 2, 3, tzinfo=pytz.FixedOffset(60)))
assert dt.utcoffset() == timedelta(minutes=60)

dt = pendulum.instance(datetime(2021, 2, 3, tzinfo=pytz.FixedOffset(-330)))
assert dt.utcoffset() == timedelta(minutes=-330)


def test_instance_with_date() -> None:
dt = pendulum.instance(date(2022, 12, 23))

Expand Down
Loading