Raise a clear error converting a truncated TimePoint to a date - #286
Open
gaoflow wants to merge 2 commits into
Open
Raise a clear error converting a truncated TimePoint to a date#286gaoflow wants to merge 2 commits into
gaoflow wants to merge 2 commits into
Conversation
Converting a truncated TimePoint to a calendar or ordinal date (for example a truncated week date) called get_calendar_date_from_week_date with a None day-of-week, raising an obscure "unsupported operand type(s) for +: 'int' and 'NoneType'" TypeError. A truncated TimePoint has no fully-determined year or day, so it cannot be converted to an absolute date; get_calendar_date and get_ordinal_date now raise a clear ValueError instead, mirroring the existing truncated-comparison guard. Fixes metomi#265.
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.
Fixes #265.
Converting a truncated
TimePointto a calendar or ordinal date raised an obscureTypeErrorinstead of a clear error:Cause
to_calendar_date()/to_ordinal_date()delegate toget_calendar_date()/get_ordinal_date(). For a truncated week-date point these reachget_calendar_date_from_week_date(year, week_of_year, day_of_week)withday_of_weekunset, so(week_of_year - 1) * 7 + day_of_week - 1evaluatesint + Noneand raises the unhelpfulTypeError. A truncatedTimePointhas no fully-determined year or day, so it fundamentally cannot be converted to an absolute calendar or ordinal date.Fix
Guard
get_calendar_date()andget_ordinal_date(): when the point is truncated, raise a clearValueError, mirroring the existing truncated-comparison guard ("Cannot compare truncated to non-truncated TimePoint: ..."). Theto_calendar_date()/to_ordinal_date()wrappers surface this directly, and non-truncated conversions are unchanged.Added a regression test (
test_timepoint_truncated_to_calendar_ordinal_date) covering both methods, and added my name to the Code Contributors list per CONTRIBUTING.md.Disclosure: I prepared this fix with AI assistance under my direction; I reviewed and verified the change and the test myself.