Repository navigation
Fix DateTime.is_dst() returning True for naive datetimes - #1108
Open
Yasser-Ameur wants to merge 1 commit into
Open
Yasser-Ameur wants to merge 1 commit into
Yasser-Ameur wants to merge 1 commit into
Conversation
is_dst() reported daylight saving time for every naive DateTime, in January as well as July. For a naive datetime, dst() returns None, and None != timedelta() is True, so the comparison in is_dst() always succeeded. Use bool(self.dst()) instead. None and timedelta(0) are both falsy, so naive and standard-time values return False, and a nonzero DST offset still returns True. Add a naive case to the is_dst tests.
Yasser-Ameur
marked this pull request as ready for review
December 5, 2026 01:45
This branch has not been deployed
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.
DateTime.is_dst()returnsTruefor every naiveDateTime, in January as well as July. The standard library givesNonefromdst()for a naive datetime, and aware values already work.Cause
is_dst()insrc/pendulum/datetime.pyisself.dst() != datetime.timedelta(). For a naive datetimedst()isNone, andNone != timedelta()isTrue.Change
It now returns
bool(self.dst()).Noneandtimedelta(0)are both falsy and a nonzero DST offset is still truthy, so aware values behave as before. One line in the library.Tests
test_is_dst_naiveintests/datetime/test_getters.pychecks a naive January and a naive July value. It fails on master withassert not True.Run locally on Python 3.14 against the worktree sources:
pytest -q testsgives 1 failed, 1868 passed, 5 skipped, 1 warning. The one failure istests/test_main.py::test_instance_with_aware_datetime_any_tzinfo, which depends on the host timezone and fails without this change too (open PR #1072 covers it).mypy(strict, frompyproject.toml) reports no issues in 228 source files, andruff checkandruff format --checkare clean (ruff 0.14.11, the version pinned inprek.toml).The same suite with
PENDULUM_EXTENSIONS=0gives the same result.Not run: other Python versions and the benchmarks. I did not edit
CHANGELOG.md, since recent PRs do not touch it.Pull Request Check List
No documentation describes the old result. This is a bug fix, so it is based on
master.