Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-103636: issue warning for deprecated calendar constants #103833

Merged
merged 21 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from 12 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
86 changes: 86 additions & 0 deletions Doc/library/calendar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,92 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
2 BC, and so on.


.. class:: Day

This enumeration defines the days of the week as integer constants.

.. attribute:: MONDAY

The constant for Monday, with a value of 0.

.. attribute:: TUESDAY

The constant for Tuesday, with a value of 1.

.. attribute:: WEDNESDAY

The constant for Wednesday, with a value of 2.

.. attribute:: THURSDAY

The constant for Thursday, with a value of 3.

.. attribute:: FRIDAY

The constant for Friday, with a value of 4.

.. attribute:: SATURDAY

The constant for Saturday, with a value of 5.

.. attribute:: SUNDAY
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved

The constant for Sunday, with a value of 6.


.. class:: Month

This enumeration defines the months of the year as integer constants.
merwok marked this conversation as resolved.
Show resolved Hide resolved

.. attribute:: JANUARY

The constant for January, with a value of 1.

.. attribute:: FEBRUARY

The constant for February, with a value of 2.

.. attribute:: MARCH

The constant for March, with a value of 3.

.. attribute:: APRIL

The constant for April, with a value of 4.

.. attribute:: MAY

The constant for May, with a value of 5.

.. attribute:: JUNE

The constant for June, with a value of 6.

.. attribute:: JULY

The constant for July, with a value of 7.

.. attribute:: AUGUST

The constant for August, with a value of 8.

.. attribute:: SEPTEMBER

The constant for September, with a value of 9.

.. attribute:: OCTOBER

The constant for October, with a value of 10.

.. attribute:: NOVEMBER

The constant for November, with a value of 11.

.. attribute:: DECEMBER
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved

The constant for December, with a value of 12.


.. class:: Calendar(firstweekday=0)

Creates a :class:`Calendar` object. *firstweekday* is an integer specifying the
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,11 @@ sys
exception instance, rather than to a ``(typ, exc, tb)`` tuple.
(Contributed by Irit Katriel in :gh:`103176`.)

calendar
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
--------

* Added Enum for Month and Day
Agent-Hellboy marked this conversation as resolved.
Show resolved Hide resolved
(Contributed by Prince Roshan in :gh:`103636`.)

Optimizations
=============
Expand Down Expand Up @@ -653,6 +658,8 @@ Deprecated
Python 3.14, when ``'data'`` filter will become the default.
See :ref:`tarfile-extraction-filter` for details.

* ``January`` and ``February`` is deprated from :mod:`calendar`
use ``JANUARY`` and ``FEBRUARY`` instead
merwok marked this conversation as resolved.
Show resolved Hide resolved

Pending Removal in Python 3.13
------------------------------
Expand Down
13 changes: 13 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 deprecated, use '{name.upper()}' instead",
DeprecationWarning, stacklevel=2)
if name == 'January':
return 1
else:
return 2

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


# Constants for months
@global_enum
class Month(IntEnum):
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
merwok marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertIn("The 'January' attribute is 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module-level attribute January and February is deprecated
merwok marked this conversation as resolved.
Show resolved Hide resolved