Repository navigation
Raise ValueError for solar dates outside the supported range - #29
Merged
lidaobing merged 2 commits intoNov 29, 2026
Merged
Conversation
from_solar_date() returned bad dates instead of failing when the solar date was before 1900-01-31 or on/after 2100-02-09. For example from_solar_date(2100, 2, 9) gave LunarDate(2099, 1, 1, 0), and from_solar_date(1900, 1, 1) gave LunarDate(1900, 1, -29, 0). Adding a timedelta past the last day had the same problem.
lidaobing
reviewed
Nov 27, 2026
|
|
||
| offset = int(offset) | ||
|
|
||
| if offset < 0 or offset >= sum(YEAR_DAYS): |
Owner
There was a problem hiding this comment.
How about cache sum(YEAR_DAYS) & end_date
Contributor
Author
|
Good idea, done in c644b6d: |
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.
What is wrong
from_solar_date()does not fail for solar dates outside the table. It returns a wrong date.Expected: a
ValueError, liketo_solar_date()already raises for years outside[1900, 2100).The wrapped result is the worst case. It looks like a valid date, so the caller gets a wrong answer with no error.
The fix
_from_offset()now checks the offset against the table range first. If it is before 1900-01-31 or on/after 2100-02-09, it raises:Dates inside the range are not changed.
The test
New doctests in
from_solar_date: the last valid day (2100-02-08), one day before the first day, one day after the last day, and+ timedeltapast the end.python lunardate.py-> 3 failures.python lunardate.py -v-> 80 passed.mypy lunardate.py-> no issues. flake8 (CI selection) -> 0.