Skip to content

Commit 1781682

Browse files
committed
gh-148663: Document that IllegalMonthError inherits from both ValueError and IndexError
1 parent 2a07ff9 commit 1781682

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Doc/library/calendar.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,14 @@ The :mod:`!calendar` module defines the following exceptions:
580580

581581
.. exception:: IllegalMonthError(month)
582582

583-
A subclass of :exc:`ValueError`,
583+
A subclass of both :exc:`ValueError` and :exc:`IndexError`,
584584
raised when the given month number is outside of the range 1-12 (inclusive).
585+
The :exc:`IndexError` base class is preserved for backwards compatibility
586+
with code that caught :exc:`IndexError` for bad month numbers prior to
587+
Python 3.13.
588+
589+
.. versionchanged:: 3.12
590+
:exc:`IllegalMonthError` is now also a subclass of :exc:`IndexError`.
585591

586592
.. attribute:: month
587593

Lib/test/test_calendar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,17 @@ def test_formatmonth(self):
495495
calendar.TextCalendar().formatmonth(0, 2),
496496
result_0_02_text
497497
)
498+
498499
def test_formatmonth_with_invalid_month(self):
499500
with self.assertRaises(calendar.IllegalMonthError):
500501
calendar.TextCalendar().formatmonth(2017, 13)
501502
with self.assertRaises(calendar.IllegalMonthError):
502503
calendar.TextCalendar().formatmonth(2017, -1)
503504

505+
def test_illegal_month_error_bases(self):
506+
self.assertTrue(issubclass(calendar.IllegalMonthError, ValueError))
507+
self.assertTrue(issubclass(calendar.IllegalMonthError, IndexError))
508+
504509
def test_formatmonthname_with_year(self):
505510
self.assertEqual(
506511
calendar.HTMLCalendar().formatmonthname(2004, 1, withyear=True),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Document that :class:`calendar.IllegalMonthError` is a subclass of both
2+
:exc:`ValueError` and :exc:`IndexError` since Python 3.12.

0 commit comments

Comments
 (0)