Skip to content

Remove pytz dependency in favour of stdlib (PP-3822)#3115

Merged
jonathangreen merged 2 commits intomainfrom
chore/remove-pytz-dependency
Mar 6, 2026
Merged

Remove pytz dependency in favour of stdlib (PP-3822)#3115
jonathangreen merged 2 commits intomainfrom
chore/remove-pytz-dependency

Conversation

@jonathangreen
Copy link
Member

@jonathangreen jonathangreen commented Mar 6, 2026

Description

Replace all pytz usage with Python standard library equivalents (datetime.timezone.utc and zoneinfo.ZoneInfo). Remove pytz and types-pytz from project dependencies.

Replacements made

pytz pattern stdlib replacement
pytz.UTC / pytz.utc datetime.UTC / datetime.timezone.utc
pytz.utc.localize(dt) to_utc(dt)
dt.astimezone(pytz.UTC) dt.astimezone(datetime.UTC)
pytz.timezone("US/Eastern") zoneinfo.ZoneInfo("US/Eastern")

Motivation and Context

pytz is a legacy timezone library. Since Python 3.9+, the standard library provides datetime.timezone (for UTC) and zoneinfo.ZoneInfo (for named timezones), making pytz unnecessary. This project targets Python 3.12+, so it can safely be removed, reducing the dependency footprint.

JIRA: PP-3822

How Has This Been Tested?

  • mypy & tests pass

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

Since Python 3.9+, the standard library provides datetime.timezone.utc
and zoneinfo.ZoneInfo, making pytz unnecessary. This project targets
Python 3.12+, so we can safely remove it and use the stdlib equivalents.
@tdilauro
Copy link
Contributor

tdilauro commented Mar 6, 2026

I made a ticket for this the other day, so you might want to reference it: PP-3822.

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.22%. Comparing base (450a2d6) to head (df53ac5).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3115      +/-   ##
==========================================
- Coverage   93.22%   93.22%   -0.01%     
==========================================
  Files         491      491              
  Lines       45364    45357       -7     
  Branches     6246     6246              
==========================================
- Hits        42292    42285       -7     
  Misses       1984     1984              
  Partials     1088     1088              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jonathangreen jonathangreen changed the title Remove pytz dependency in favor of stdlib Remove pytz dependency in favour of stdlib (PP-3822) Mar 6, 2026
@jonathangreen jonathangreen requested a review from a team March 6, 2026 13:46
Copy link
Contributor

@tdilauro tdilauro left a comment

Choose a reason for hiding this comment

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

Looks good! 🚀

I did note that we go back and forth between timezone.utc and datetime.UTC, but I'm assuming that's just to avoid importing datetime when we're already importing some of its members.

@jonathangreen jonathangreen merged commit 86baa03 into main Mar 6, 2026
20 checks passed
@jonathangreen jonathangreen deleted the chore/remove-pytz-dependency branch March 6, 2026 14:24
jonathangreen added a commit to ThePalaceProject/virtual-library-card that referenced this pull request Mar 11, 2026
## Description

Remove the `pytz` dependency. `pytz` is no longer recommended for Python
3.9+; the stdlib `zoneinfo` module and `django.utils.timezone` are the
preferred alternatives. This codebase already uses
`django.utils.timezone` exclusively with no direct imports of `pytz`, so
this is a clean removal.

## Motivation and Context

This is part of an effort to remove `pytz` across all Palace Project
repositories:
- ThePalaceProject/circulation#3115
- ThePalaceProject/library-registry#893
- ThePalaceProject/palace-tools#211

## How Has This Been Tested?

Confirmed there are no imports of `pytz` anywhere in the codebase. All
timezone handling already uses `django.utils.timezone`.

## Checklist

- [x] I have updated the documentation accordingly.
- [x] All new and existing tests 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.

2 participants