Skip to content

Commit 84e7d0f

Browse files
Agent-Hellboyhugovkmerwokblurb-it[bot]
authored
gh-103636: issue warning for deprecated calendar constants (#103833)
Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Éric <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent ed29f52 commit 84e7d0f

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

Doc/library/calendar.rst

+52
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,58 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
2828
2 BC, and so on.
2929

3030

31+
.. class:: Day
32+
33+
Enumeration defining the days of the week as integer constants, from 0 to 6.
34+
35+
.. attribute:: MONDAY
36+
37+
.. attribute:: TUESDAY
38+
39+
.. attribute:: WEDNESDAY
40+
41+
.. attribute:: THURSDAY
42+
43+
.. attribute:: FRIDAY
44+
45+
.. attribute:: SATURDAY
46+
47+
.. attribute:: SUNDAY
48+
49+
.. versionadded:: 3.12
50+
51+
52+
.. class:: Month
53+
54+
Enumeration defining months of the year as integer constants, from 1 to 12.
55+
56+
.. attribute:: JANUARY
57+
58+
.. attribute:: FEBRUARY
59+
60+
.. attribute:: MARCH
61+
62+
.. attribute:: APRIL
63+
64+
.. attribute:: MAY
65+
66+
.. attribute:: JUNE
67+
68+
.. attribute:: JULY
69+
70+
.. attribute:: AUGUST
71+
72+
.. attribute:: SEPTEMBER
73+
74+
.. attribute:: OCTOBER
75+
76+
.. attribute:: NOVEMBER
77+
78+
.. attribute:: DECEMBER
79+
80+
.. versionadded:: 3.12
81+
82+
3183
.. class:: Calendar(firstweekday=0)
3284

3385
Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the

Doc/whatsnew/3.12.rst

+9
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,12 @@ asyncio
300300
yielding tasks.
301301
(Contributed by Kumar Aditya in :gh:`78530`.)
302302

303+
calendar
304+
--------
305+
306+
* Add enums :data:`~calendar.Month` and :data:`~calendar.Day`.
307+
(Contributed by Prince Roshan in :gh:`103636`.)
308+
303309
csv
304310
---
305311

@@ -692,6 +698,9 @@ Deprecated
692698
Python 3.14, when ``'data'`` filter will become the default.
693699
See :ref:`tarfile-extraction-filter` for details.
694700

701+
* ``calendar.January`` and ``calendar.February`` constants are deprecated and
702+
replaced by :data:`calendar.Month.JANUARY` and :data:`calendar.Month.FEBRUARY`.
703+
(Contributed by Prince Roshan in :gh:`103636`.)
695704

696705
Pending Removal in Python 3.13
697706
------------------------------

Lib/calendar.py

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from enum import IntEnum, global_enum
1111
import locale as _locale
1212
from itertools import repeat
13+
import warnings
1314

1415
__all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
1516
"firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -41,6 +42,18 @@ def __str__(self):
4142
return "bad weekday number %r; must be 0 (Monday) to 6 (Sunday)" % self.weekday
4243

4344

45+
def __getattr__(name):
46+
if name in ('January', 'February'):
47+
warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
48+
DeprecationWarning, stacklevel=2)
49+
if name == 'January':
50+
return 1
51+
else:
52+
return 2
53+
54+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
55+
56+
4457
# Constants for months
4558
@global_enum
4659
class Month(IntEnum):

Lib/test/test_calendar.py

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import datetime
1010
import os
11+
import warnings
1112

1213
# From https://en.wikipedia.org/wiki/Leap_year_starting_on_Saturday
1314
result_0_02_text = """\
@@ -490,6 +491,14 @@ def test_format(self):
490491
self.assertEqual(out.getvalue().strip(), "1 2 3")
491492

492493
class CalendarTestCase(unittest.TestCase):
494+
495+
def test_deprecation_warning(self):
496+
with warnings.catch_warnings(record=True) as w:
497+
calendar.January
498+
self.assertEqual(len(w), 1)
499+
self.assertEqual(w[0].category, DeprecationWarning)
500+
self.assertIn("The 'January' attribute is deprecated, use 'JANUARY' instead", str(w[0].message))
501+
493502
def test_isleap(self):
494503
# Make sure that the return is right for a few years, and
495504
# ensure that the return values are 1 or 0, not just true or
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Module-level attributes ``January`` and ``February`` are deprecated from :mod:`calendar`.

0 commit comments

Comments
 (0)