Fix #807: handle pytz.FixedOffset in pendulum.instance()#981
Open
apoorvdarshan wants to merge 1 commit into
Open
Fix #807: handle pytz.FixedOffset in pendulum.instance()#981apoorvdarshan wants to merge 1 commit into
apoorvdarshan wants to merge 1 commit into
Conversation
pendulum.instance() raised 'AttributeError: NoneType object has no attribute lower' for datetimes whose tzinfo is a pytz.FixedOffset. _safe_timezone() detects pytz zones via their localize() method and then uses their .zone name. Named pytz zones carry a zone string, but fixed-offset zones (pytz.FixedOffset) have zone = None, so that None was passed to timezone(), which crashed. Fall back to the tzinfo's UTC offset when there is no zone name, mirroring how other offset-only tzinfos are handled. Added a regression test (skipped when pytz is unavailable).
87af05f to
47718d7
Compare
Secrus
reviewed
Jul 6, 2026
| assert now.timezone_name == "+02:00" | ||
|
|
||
|
|
||
| def test_instance_with_pytz_fixed_offset() -> None: |
Collaborator
There was a problem hiding this comment.
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.
Collaborator
There was a problem hiding this comment.
Please drop the changelog entry - we don't add do "unreleased" in the changelog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #807.
pendulum.instance()raisedAttributeError: 'NoneType' object has no attribute 'lower'for adatetimewhosetzinfois apytz.FixedOffset:Root cause
_safe_timezone()detects pytz timezones by the presence of alocalize()method and then uses their.zonename:Named pytz zones (e.g.
America/New_York) expose azonestring, but fixed-offset zones (pytz.FixedOffset) havezone = None. ThatNonewas passed totimezone(None), which doesname.lower()and crashes.Fix
When a pytz zone has no
zonename, fall back to its UTC offset (the same approach already used for other offset-onlytzinfoobjects further down the same branch). Named zones are unaffected.Verification
test_instance_with_pytz_fixed_offset(positive and negative offsets), guarded bypytest.importorskip("pytz")since pytz is not a test dependency. It reproduces the exactAttributeErroronmasterand passes with this change.pytz.UTC,zoneinfo,dateutil, and stdlibdatetime.timezone.ruff check,ruff format --check, andmypy(strict) clean on the changed files.Disclosure: prepared with AI assistance; reviewed and verified locally against the test suite.