Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from enum import IntEnum, global_enum
import locale as _locale
from itertools import repeat
import warnings

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


def __getattr__(name):
if name in ['January','February']:
warnings.warn(f"The '{name}' attribute is going to be deprecated use '{name.upper()}' instead",
DeprecationWarning,
stacklevel=2)
if name == 'January':
return JANUARY
else:
return FEBRUARY

raise AttributeError(f"module {__name__} has no attribute {name}")

# Constants for months
@global_enum
class Month(IntEnum):
Expand Down Expand Up @@ -71,6 +84,7 @@ class Day(IntEnum):




# Number of days per month (except for February in leap years)
mdays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import datetime
import os
import warnings

# From https://en.wikipedia.org/wiki/Leap_year_starting_on_Saturday
result_0_02_text = """\
Expand Down Expand Up @@ -490,6 +491,16 @@ def test_format(self):
self.assertEqual(out.getvalue().strip(), "1 2 3")

class CalendarTestCase(unittest.TestCase):

def test_deprecation_warning(self):
with warnings.catch_warnings(record=True) as w:
# Access the deprecated attribute
_ = calendar.January
# Check that a DeprecationWarning was issued
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertIn("The 'January' attribute is going to be deprecated use 'JANUARY' instead", str(w[0].message))

def test_isleap(self):
# Make sure that the return is right for a few years, and
# ensure that the return values are 1 or 0, not just true or
Expand Down