Skip to content

Raise a clear error converting a truncated TimePoint to a date - #286

Open
gaoflow wants to merge 2 commits into
metomi:masterfrom
gaoflow:fix-265-truncated-calendar-date
Open

Raise a clear error converting a truncated TimePoint to a date#286
gaoflow wants to merge 2 commits into
metomi:masterfrom
gaoflow:fix-265-truncated-calendar-date

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 16, 2026

Copy link
Copy Markdown

Fixes #265.

Converting a truncated TimePoint to a calendar or ordinal date raised an obscure TypeError instead of a clear error:

from metomi.isodatetime.data import TimePoint
a = TimePoint(year=1, week_of_year=2, truncated=True,
              truncated_property='year_of_decade')
a.to_calendar_date()
# TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

Cause

to_calendar_date() / to_ordinal_date() delegate to get_calendar_date() / get_ordinal_date(). For a truncated week-date point these reach get_calendar_date_from_week_date(year, week_of_year, day_of_week) with day_of_week unset, so (week_of_year - 1) * 7 + day_of_week - 1 evaluates int + None and raises the unhelpful TypeError. A truncated TimePoint has no fully-determined year or day, so it fundamentally cannot be converted to an absolute calendar or ordinal date.

Fix

Guard get_calendar_date() and get_ordinal_date(): when the point is truncated, raise a clear ValueError, mirroring the existing truncated-comparison guard ("Cannot compare truncated to non-truncated TimePoint: ..."). The to_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.

gaoflow added 2 commits June 16, 2026 02:39
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.
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.

Traceback when converting truncated week date, with year of decade, to calendar date

1 participant