Skip to content

Make Timezone and FixedTimezone hashable - #1021

Merged
ashb merged 1 commit into
python-pendulum:masterfrom
Pebble32:fix-timezone-hash
Sep 29, 2026
Merged

ashb merged 1 commit into
python-pendulum:masterfrom
Pebble32:fix-timezone-hash

Conversation

@Pebble32

@Pebble32 Pebble32 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Timezone and FixedTimezone define __eq__ but not __hash__, so Python marks them unhashable and they cannot be used in sets or as dict keys:

>>> import pendulum
>>> {pendulum.timezone("Europe/Paris")}
TypeError: unhashable type: 'Timezone'

This adds __hash__ to both, hashing exactly what __eq__ compares (key for Timezone, _offset for FixedTimezone), so equal timezones hash equal. Hashing the string form would not be correct for FixedTimezone: its repr includes the name while equality ignores it.

Tests: test_hash and test_fixed_hash next to the existing equality tests, including two equal fixed timezones with different names collapsing to a single set entry.

Prompted by apache/airflow#73859, where Airflow currently works around this. Happy to add a changelog line if you want one.

Closed #1008, closed #1009 , closes #1019

Comment thread tests/tz/test_timezone.py
@Pebble32
Pebble32 requested a review from ashb September 29, 2026 12:22
@codspeed

codspeed Bot commented Sep 29, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 1 untouched benchmark


Comparing Pebble32:fix-timezone-hash (9c72264) with master (d3f44aa)

Open in CodSpeed

@ashb
ashb merged commit 7149d52 into python-pendulum:master Sep 29, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timezones are not hashable

2 participants